Htmlagilitypack: How To Check If An Element Is Visible?
I am doing some HTML parsing and I am using HtmlAgilityPack and I am trying to check if a node element would be visible if the html was rendered in a browser. By visible, I am prob
Solution 1:
OK, so I have managed to do this, at least for my needs. Please be warned however, as other comments have spoke of, this doesn't allow you to check if an element will be visible (on screen) for the end user.
The approach I have taken simple checks some basic rules: An element is "not visible" if the style attribute for the element contains display:none
or visibility:hidden
, OR an ancestor element has the same style rules.
With that in mind, here is my code that does the job for me:
privatestaticboolIsNodeVisible(HtmlAgilityPack.HtmlNode node)
{
var attribute = node.Attributes["style"];
bool thisVisible = false;
if (attribute == null || CheckStyleVisibility(attribute.Value))
thisVisible = true;
if (thisVisible && node.ParentNode != null)
return IsNodeVisible(node.ParentNode);
return thisVisible;
}
privatestaticboolCheckStyleVisibility(string style)
{
if (string.IsNullOrWhiteSpace(style))
returntrue;
var keys = ParseHtmlStyleString(style);
if (keys.Keys.Contains("display"))
{
string display = keys["display"];
if (display != null && display == "none")
returnfalse;
}
if (keys.Keys.Contains("visibility"))
{
string visibility = keys["visibility"];
if (visibility != null && visibility == "hidden")
returnfalse;
}
returntrue;
}
publicstatic Dictionary<string, string> ParseHtmlStyleString(string style)
{
Dictionary<string, string> result = new Dictionary<string, string>();
style = style.Replace(" ", "").ToLowerInvariant();
string[] settings = style.Split(newchar[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
foreach (string s in settings)
{
if (!s.Contains(':'))
continue;
string[] data = s.Split(':');
result.Add(data[0], data[1]);
}
return result;
}
The entry point for this is IsNodeVisible
and will check the visibility of the HtmlNode
passed to it.
Post a Comment for "Htmlagilitypack: How To Check If An Element Is Visible?"