Skip to content

Commit

Permalink
update source
Browse files Browse the repository at this point in the history
update source
  • Loading branch information
JonathanMagnan committed Jul 25, 2019
1 parent 0c2e574 commit f9e8f3a
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 14 deletions.
8 changes: 7 additions & 1 deletion src/HtmlAgilityPack.Shared/HtmlDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1716,8 +1716,14 @@ private void Parse()
script._lineposition = _currentnode.LinePosition + _currentnode._namelength + 2;

_currentnode.AppendChild(script);
_currentnode._isPcData = true;

// https://www.w3schools.com/jsref/prop_node_innertext.asp
// textContent returns the text content of all elements, while innerText returns the content of all elements, except for <script> and <style> elements.
// innerText will not return the text of elements that are hidden with CSS (textContent will). ==> The parser do not support that.
if (_currentnode.Name.ToLowerInvariant().Equals("script") || _currentnode.Name.ToLowerInvariant().Equals("style"))
{
_currentnode._isHideInnerText = true;
}

PushNodeStart(HtmlNodeType.Element, _index - 1, _lineposition -1);
PushNodeNameStart(false, _index - 1 + 2);
Expand Down
47 changes: 34 additions & 13 deletions src/HtmlAgilityPack.Shared/HtmlNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public partial class HtmlNode
internal bool _starttag;
internal int _streamposition;
internal bool _isImplicitEnd;
internal bool _isPcData;
internal bool _isHideInnerText;

#endregion

Expand All @@ -81,7 +81,7 @@ public partial class HtmlNode
/// Gets a collection of flags that define specific behaviors for specific element nodes.
/// The table contains a DictionaryEntry list with the lowercase tag name as the Key, and a combination of HtmlElementFlags as the Value.
/// </summary>
public static Dictionary<string, HtmlElementFlag> ElementsFlags;
public static Dictionary<string, HtmlElementFlag> ElementsFlags;

#endregion

Expand Down Expand Up @@ -377,18 +377,40 @@ public virtual string InnerText
{
get
{
string result;
string name = this.Name;

if (name != null)
{
name = name.ToLowerInvariant();

bool isDisplayScriptingText = (name == "head" || name == "script" || name == "style");

result = InternalInnerText(isDisplayScriptingText);
}
else
{
result = InternalInnerText(false);
}

return result;
}
}

internal virtual string InternalInnerText(bool isDisplayScriptingText)
{
if (!_ownerdocument.BackwardCompatibility)
{
if (HasChildNodes)
{
StringBuilder sb = new StringBuilder();
AppendInnerText(sb);
AppendInnerText(sb, isDisplayScriptingText);
return sb.ToString();
}

return GetCurrentNodeText();
}

if (_nodetype == HtmlNodeType.Text)
return ((HtmlTextNode) this).Text;

Expand All @@ -398,18 +420,17 @@ public virtual string InnerText

// note: right now, this method is *slow*, because we recompute everything.
// it could be optimized like innerhtml
if (!HasChildNodes || _isPcData)
if (!HasChildNodes || ( _isHideInnerText && !isDisplayScriptingText))
return string.Empty;

string s = null;
foreach (HtmlNode node in ChildNodes)
s += node.InnerText;
s += node.InternalInnerText(isDisplayScriptingText);
return s;
}
}
}

/// <summary>Gets direct inner text.</summary>
/// <returns>The direct inner text.</returns>
/// <summary>Gets direct inner text.</summary>
/// <returns>The direct inner text.</returns>
public virtual string GetDirectInnerText()
{
if (!_ownerdocument.BackwardCompatibility)
Expand Down Expand Up @@ -481,18 +502,18 @@ internal void AppendDirectInnerText(StringBuilder sb)
return;
}

internal void AppendInnerText(StringBuilder sb)
internal void AppendInnerText(StringBuilder sb, bool isShowHideInnerText)
{
if (_nodetype == HtmlNodeType.Text)
{
sb.Append(GetCurrentNodeText());
}

if (!HasChildNodes || _isPcData) return;
if (!HasChildNodes || (_isHideInnerText && !isShowHideInnerText)) return;

foreach (HtmlNode node in ChildNodes)
{
node.AppendInnerText(sb);
node.AppendInnerText(sb, isShowHideInnerText);
}
}

Expand Down

0 comments on commit f9e8f3a

Please sign in to comment.