是否可以检测 <img>-tag 是否位于 html 文档中的正确位置?



我想知道是否可以检测<img>标签在HTML文档中是否正确定位。我做了一些研究,但我只得到了有关如何在实际显示的网站上定位它的信息。我正在寻找的是HTML文档代码中的位置(在源代码中(。我想在 C# 单元测试中测试某个 <img> -tag 是否正确定位。

编辑:<img> -tag 还没有特殊的标识符,所以我必须在之前实现它,如果甚至可以根据 id 检测标签位置。

编辑 2:这是 HTML 文档通常的样子:

<div>
    <p class="MsoNormal"><span style='color:#1F497D;mso-fareast-language:DE'>disclaimer should be here<o:p></o:p></span></p>
</div>
<p class="MsoNormal"><span style='color:#1F497D'><o:p>&nbsp;</o:p></span></p>
<div>
    <div>
        <p>
            <span style="font-family: Calibri;">
                <span style="font-size: 8.5pt; font-family: Calibri;"><br>
                </span>
            </span>
        </p>
        <p>
            <span style="font-family: Calibri;">
                <span style="font-size: 11px;"><img src="cid:__Image_00000020" alt="" title="" width="499pxpx">
                </span>
            </span>
        </p>
    </div>
</div>

如果可能的话,我很想知道可以用来测试这个。提前感谢!

所以我

解决问题的方法是检查 <img> -tag 是否在<div class="WordSection1">内。如果不是这种情况,我继续检查每个标签位置的<html>,并使用DFS算法列出这些位置。然后我继续查找节点位置并比较它们的索引:

public static DisclaimerPosition GetPositioning(HtmlNode tag, HtmlNode disclaimer, HtmlNode wordSection)
{
    if (tag == null) throw new NullReferenceException("Tag is null");
    if (disclaimer == null) throw new NullReferenceException("Tag is null");
    if (wordSection == null) throw new NullReferenceException("Tag is null");
    if (IsDisclaimerInWordSection1(disclaimer)) return DisclaimerPosition.InWordSection;
    if (tag.Name == "img" || tag.Name == "div" || tag.Attributes.FirstOrDefault(attribute => attribute.Name == "class")?.Value == "WordSection1" || !tag.HasChildNodes)
    {
        throw new ArgumentException("Tag is invalid, it's value matches disclaimer or wordSection or it has no children");
    }
    var list = GetNodeWithChildren(tag);
    var disclaimerIndex = list.IndexOf(disclaimer);
    var wordSectionIndex = list.IndexOf(wordSection);
    if (disclaimerIndex == -1) throw new ArgumentException("Disclaimer is -1");
    if (wordSectionIndex == -1) throw new ArgumentException("WordSection is -1");
    list.ForEach(node => Console.WriteLine(node.Name + " " + node.Attributes.Where(attribute => attribute.Name == "class").Select(attribute => attribute.Value).FirstOrDefault()));
    return disclaimerIndex < wordSectionIndex ? DisclaimerPosition.AboveWordSection : DisclaimerPosition.UnderneathWordSection;
}
private static List<HtmlNode> GetNodeWithChildren(HtmlNode node)
{
    var nodes = new List<HtmlNode> { node };
    nodes.AddRange(node
            .ChildNodes
            .Where(child => child.NodeType != HtmlNodeType.Text)
            .SelectMany(GetNodeWithChildren)
            .ToList());
    return nodes;
}

public static bool IsDisclaimerInWordSection1(HtmlNode disclaimerTag)
{
    var tag = disclaimerTag;
    while (tag != null)
    {
        if (tag.Name == "div" &&
            tag.HasAttributes &&
            tag.Attributes.Any(tagAttribute => tagAttribute.Name == "class" && tagAttribute.Value == "WordSection1"))
        {
            return true;
        }
        tag = tag.ParentNode;
    }
    return false;
}

相关内容

最新更新