Search code examples
c#htmltagsvs-unit-testing-framework

Is it possible to detect if an <img>-tag is positioned at the correct spot in an html document?


I would like to know if it is possible to detect if an <img>-tag is positioned correctly in an HTML document. I did some research but I only got information on how to position it on the actual displayed site. What I'm looking for is the position inside the code of an HTML document (in the source code). I would like to test in C# Unit Tests whether a certain <img>-tag is positioned correctly.

Edit: The <img>-tag has no special identifier (yet) so I'll have to implement that in before, if it is even possible to detect a tags position based off the id.

Edit 2: This is what a the HTML document generally looks like:

<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>

If that is possible, I'd love to know what can be used to test this. Thanks in advance!


Solution

  • So the way I fixed my problem was by checking if the <img>-tag was inside the <div class="WordSection1">. If that wasn't the case I went ahead and checked the <html> for every tags position and listing those by using a DFS-Algorithm. Then I went ahead, looked up the nodes positions and compared their indexes:

    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;
    }