是否可以检测 <img> 标签是否位于 html 文档中的正确位置?
Is it possible to detect if an <img>-tag is positioned at the correct spot in an html document?
我想知道是否可以检测 <img>
标签在 HTML 文档中的位置是否正确。我做了一些研究,但我只得到了关于如何在实际显示的网站上定位它的信息。我正在寻找的是 HTML 文档(在源代码中)的代码内的位置。我想在 C# 单元测试中测试某个 <img>
标签是否正确定位。
编辑:<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> </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>
标签是否在 <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;
}
我想知道是否可以检测 <img>
标签在 HTML 文档中的位置是否正确。我做了一些研究,但我只得到了关于如何在实际显示的网站上定位它的信息。我正在寻找的是 HTML 文档(在源代码中)的代码内的位置。我想在 C# 单元测试中测试某个 <img>
标签是否正确定位。
编辑:<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> </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>
标签是否在 <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;
}