Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce need to call string.ToLower with proper comparisons #559

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/HtmlAgilityPack.Shared/HtmlDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,7 @@ public HtmlNode GetElementbyId(string id)
throw new Exception(HtmlExceptionUseIdAttributeFalse);
}

return Nodesid.ContainsKey(id) ? Nodesid[id] : null;
return Nodesid.TryGetValue(id, out var value) ? value : null;
}

/// <summary>
Expand Down Expand Up @@ -1769,7 +1769,7 @@ private void Parse()
// 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"))
if (_currentnode.Name.Equals("script", StringComparison.InvariantCultureIgnoreCase) || _currentnode.Name.Equals("style", StringComparison.InvariantCultureIgnoreCase))
{
_currentnode._isHideInnerText = true;
}
Expand Down Expand Up @@ -2115,7 +2115,7 @@ private bool PushNodeEnd(int index, bool close)
if ((close) || (!_currentnode._starttag))
{
if ((OptionStopperNodeName != null) && (_remainder == null) &&
(string.Compare(_currentnode.Name, OptionStopperNodeName, StringComparison.OrdinalIgnoreCase) == 0))
string.Equals(_currentnode.Name, OptionStopperNodeName, StringComparison.OrdinalIgnoreCase))
{
_remainderOffset = index;
_remainder = Text.Substring(_remainderOffset);
Expand Down Expand Up @@ -2168,7 +2168,7 @@ private void ReadDocumentEncoding(HtmlNode node)
HtmlAttribute att = node.Attributes["http-equiv"];
if (att != null)
{
if (string.Compare(att.Value, "content-type", StringComparison.OrdinalIgnoreCase) != 0)
if (!string.Equals(att.Value, "content-type", StringComparison.OrdinalIgnoreCase))
return;
HtmlAttribute content = node.Attributes["content"];
if (content != null)
Expand Down
2 changes: 1 addition & 1 deletion src/HtmlAgilityPack.Shared/HtmlNameTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace HtmlAgilityPack
{
internal class HtmlNameTable : XmlNameTable
internal sealed class HtmlNameTable : XmlNameTable
{
#region Fields

Expand Down
42 changes: 22 additions & 20 deletions src/HtmlAgilityPack.Shared/HtmlNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -382,13 +382,13 @@ public virtual string InnerText
var sb = new StringBuilder();
int depthLevel = 0;
string name = this.Name;

if (name != null)
{
name = name.ToLowerInvariant();
bool isDisplayScriptingText = string.Equals(name, "head", StringComparison.InvariantCultureIgnoreCase)
|| string.Equals(name, "script", StringComparison.InvariantCultureIgnoreCase)
|| string.Equals(name, "style", StringComparison.InvariantCultureIgnoreCase);

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

InternalInnerText(sb, isDisplayScriptingText, depthLevel);
}
else
Expand Down Expand Up @@ -1934,7 +1934,7 @@ public virtual void WriteTo(TextWriter outText, int level = 0)
if (_ownerdocument.OptionOutputAsXml)
{
var commentNode = (HtmlCommentNode) this;
if (!_ownerdocument.BackwardCompatibility && commentNode.Comment.ToLowerInvariant().StartsWith("<!doctype"))
if (!_ownerdocument.BackwardCompatibility && commentNode.Comment.StartsWith("<!doctype", StringComparison.InvariantCultureIgnoreCase))
{
outText.Write(commentNode.Comment);
}
Expand Down Expand Up @@ -2512,6 +2512,8 @@ public void AddClass(string name)
AddClass(name, false);
}

private static readonly char[] spaceSeparator = { ' ' };

/// <summary>
/// Adds one or more classes to this node.
/// </summary>
Expand All @@ -2520,26 +2522,26 @@ public void AddClass(string name)
public void AddClass(string name, bool throwError)
{
var classAttributes = Attributes.AttributesWithName("class");
var isEmpty = true;

if (!IsEmpty(classAttributes))
foreach (HtmlAttribute att in classAttributes)
{
foreach (HtmlAttribute att in classAttributes)
{
// Check class solo, check class in First with other class, check Class no first.
if (att.Value != null && att.Value.Split(' ').ToList().Any(x => x.Equals(name)))
{
if (throwError)
{
throw new Exception(HtmlDocument.HtmlExceptionClassExists);
}
}
else
isEmpty = false;
// Check class solo, check class in First with other class, check Class no first.
if (att.Value != null && Array.IndexOf(att.Value.Split(spaceSeparator), name) != -1)
{
if (throwError)
{
SetAttributeValue(att.Name, att.Value + " " + name);
throw new Exception(HtmlDocument.HtmlExceptionClassExists);
}
}
else
{
SetAttributeValue(att.Name, att.Value + " " + name);
}
}
else

if (isEmpty)
{
HtmlAttribute attribute = _ownerdocument.CreateAttribute("class", name);
Attributes.Append(attribute);
Expand Down Expand Up @@ -2607,7 +2609,7 @@ public void RemoveClass(string name, bool throwError)
{
Attributes.Remove(att);
}
else if (att.Value != null && att.Value.Split(' ').ToList().Any(x => x.Equals(name)))
else if (att.Value != null && att.Value.Split(' ').Contains(name))
{
string[] classNames = att.Value.Split(' ');

Expand Down
8 changes: 4 additions & 4 deletions src/HtmlAgilityPack.Shared/HtmlWeb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2128,14 +2128,14 @@ private bool IsCacheHtmlContent(string path)
}
#endif

private bool IsHtmlContent(string contentType)
private static bool IsHtmlContent(string contentType)
{
return contentType.ToLowerInvariant().StartsWith("text/html");
return contentType.StartsWith("text/html", StringComparison.InvariantCultureIgnoreCase);
}

private bool IsGZipEncoding(string contentEncoding)
private static bool IsGZipEncoding(string contentEncoding)
{
return contentEncoding.ToLowerInvariant().StartsWith("gzip");
return contentEncoding.StartsWith("gzip", StringComparison.InvariantCultureIgnoreCase);
}

#if !(NETSTANDARD1_3 || NETSTANDARD1_6)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class MixedCodeDocumentFragmentList : IEnumerable
#region Fields

private MixedCodeDocument _doc;
private IList<MixedCodeDocumentFragment> _items = new List<MixedCodeDocumentFragment>();
private List<MixedCodeDocumentFragment> _items = new List<MixedCodeDocumentFragment>();

#endregion

Expand Down
2 changes: 1 addition & 1 deletion src/HtmlAgilityPack.Shared/NameValuePairList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ internal List<KeyValuePair<string, string>> GetNameValuePairs(string name)
{
if (name == null)
return _allPairs;
return _pairsWithName.ContainsKey(name) ? _pairsWithName[name] : new List<KeyValuePair<string, string>>();
return _pairsWithName.TryGetValue(name, out var value) ? value : new List<KeyValuePair<string, string>>();
}

internal string GetNameValuePairValue(string name)
Expand Down