Skip to content

Commit 70be67f

Browse files
committed
Reduce need to call string.ToLower with proper comparisons
* mark methods static
1 parent 9703643 commit 70be67f

File tree

5 files changed

+31
-29
lines changed

5 files changed

+31
-29
lines changed

src/HtmlAgilityPack.Shared/HtmlDocument.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1816,7 +1816,7 @@ private void Parse()
18161816
// https://www.w3schools.com/jsref/prop_node_innertext.asp
18171817
// textContent returns the text content of all elements, while innerText returns the content of all elements, except for <script> and <style> elements.
18181818
// innerText will not return the text of elements that are hidden with CSS (textContent will). ==> The parser do not support that.
1819-
if (_currentnode.Name.ToLowerInvariant().Equals("script") || _currentnode.Name.ToLowerInvariant().Equals("style"))
1819+
if (_currentnode.Name.Equals("script", StringComparison.OrdinalIgnoreCase) || _currentnode.Name.Equals("style", StringComparison.OrdinalIgnoreCase))
18201820
{
18211821
_currentnode._isHideInnerText = true;
18221822
}
@@ -2171,7 +2171,7 @@ private bool PushNodeEnd(int index, bool close)
21712171
if ((close) || (!_currentnode._starttag))
21722172
{
21732173
if ((OptionStopperNodeName != null) && (_remainder == null) &&
2174-
(string.Compare(_currentnode.Name, OptionStopperNodeName, StringComparison.OrdinalIgnoreCase) == 0))
2174+
string.Equals(_currentnode.Name, OptionStopperNodeName, StringComparison.OrdinalIgnoreCase))
21752175
{
21762176
_remainderOffset = index;
21772177
_remainder = Text.Substring(_remainderOffset);
@@ -2224,7 +2224,7 @@ private void ReadDocumentEncoding(HtmlNode node)
22242224
HtmlAttribute att = node.Attributes["http-equiv"];
22252225
if (att != null)
22262226
{
2227-
if (string.Compare(att.Value, "content-type", StringComparison.OrdinalIgnoreCase) != 0)
2227+
if (!string.Equals(att.Value, "content-type", StringComparison.OrdinalIgnoreCase))
22282228
return;
22292229
HtmlAttribute content = node.Attributes["content"];
22302230
if (content != null)

src/HtmlAgilityPack.Shared/HtmlNameTable.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
namespace HtmlAgilityPack
1111
{
12-
internal class HtmlNameTable : XmlNameTable
12+
internal sealed class HtmlNameTable : XmlNameTable
1313
{
1414
#region Fields
1515

src/HtmlAgilityPack.Shared/HtmlNode.cs

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -382,13 +382,13 @@ public virtual string InnerText
382382
var sb = new StringBuilder();
383383
int depthLevel = 0;
384384
string name = this.Name;
385-
385+
386386
if (name != null)
387387
{
388-
name = name.ToLowerInvariant();
388+
bool isDisplayScriptingText = string.Equals(name, "head", StringComparison.OrdinalIgnoreCase)
389+
|| string.Equals(name, "script", StringComparison.OrdinalIgnoreCase)
390+
|| string.Equals(name, "style", StringComparison.OrdinalIgnoreCase);
389391

390-
bool isDisplayScriptingText = (name == "head" || name == "script" || name == "style");
391-
392392
InternalInnerText(sb, isDisplayScriptingText, depthLevel);
393393
}
394394
else
@@ -1997,7 +1997,7 @@ public virtual void WriteTo(TextWriter outText, int level = 0)
19971997
if (_ownerdocument.OptionOutputAsXml)
19981998
{
19991999
var commentNode = (HtmlCommentNode) this;
2000-
if (!_ownerdocument.BackwardCompatibility && commentNode.Comment.ToLowerInvariant().StartsWith("<!doctype"))
2000+
if (!_ownerdocument.BackwardCompatibility && commentNode.Comment.StartsWith("<!doctype", StringComparison.OrdinalIgnoreCase))
20012001
{
20022002
outText.Write(commentNode.Comment);
20032003
}
@@ -2636,6 +2636,8 @@ public void AddClass(string name)
26362636
AddClass(name, false);
26372637
}
26382638

2639+
private static readonly char[] spaceSeparator = { ' ' };
2640+
26392641
/// <summary>
26402642
/// Adds one or more classes to this node.
26412643
/// </summary>
@@ -2644,26 +2646,26 @@ public void AddClass(string name)
26442646
public void AddClass(string name, bool throwError)
26452647
{
26462648
var classAttributes = Attributes.AttributesWithName("class");
2649+
var isEmpty = true;
26472650

2648-
if (!IsEmpty(classAttributes))
2651+
foreach (HtmlAttribute att in classAttributes)
26492652
{
2650-
foreach (HtmlAttribute att in classAttributes)
2651-
{
2652-
// Check class solo, check class in First with other class, check Class no first.
2653-
if (att.Value != null && att.Value.Split(' ').ToList().Any(x => x.Equals(name)))
2654-
{
2655-
if (throwError)
2656-
{
2657-
throw new Exception(HtmlDocument.HtmlExceptionClassExists);
2658-
}
2659-
}
2660-
else
2653+
isEmpty = false;
2654+
// Check class solo, check class in First with other class, check Class no first.
2655+
if (att.Value != null && Array.IndexOf(att.Value.Split(spaceSeparator), name) != -1)
2656+
{
2657+
if (throwError)
26612658
{
2662-
SetAttributeValue(att.Name, att.Value + " " + name);
2659+
throw new Exception(HtmlDocument.HtmlExceptionClassExists);
26632660
}
26642661
}
2662+
else
2663+
{
2664+
SetAttributeValue(att.Name, att.Value + " " + name);
2665+
}
26652666
}
2666-
else
2667+
2668+
if (isEmpty)
26672669
{
26682670
HtmlAttribute attribute = _ownerdocument.CreateAttribute("class", name);
26692671
Attributes.Append(attribute);
@@ -2731,7 +2733,7 @@ public void RemoveClass(string name, bool throwError)
27312733
{
27322734
Attributes.Remove(att);
27332735
}
2734-
else if (att.Value != null && att.Value.Split(' ').ToList().Any(x => x.Equals(name)))
2736+
else if (att.Value != null && att.Value.Split(' ').Contains(name))
27352737
{
27362738
string[] classNames = att.Value.Split(' ');
27372739

src/HtmlAgilityPack.Shared/HtmlWeb.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2129,14 +2129,14 @@ private bool IsCacheHtmlContent(string path)
21292129
}
21302130
#endif
21312131

2132-
private bool IsHtmlContent(string contentType)
2132+
private static bool IsHtmlContent(string contentType)
21332133
{
2134-
return contentType.ToLowerInvariant().StartsWith("text/html");
2134+
return contentType.StartsWith("text/html", StringComparison.OrdinalIgnoreCase);
21352135
}
21362136

2137-
private bool IsGZipEncoding(string contentEncoding)
2137+
private static bool IsGZipEncoding(string contentEncoding)
21382138
{
2139-
return contentEncoding.ToLowerInvariant().StartsWith("gzip");
2139+
return contentEncoding.StartsWith("gzip", StringComparison.OrdinalIgnoreCase);
21402140
}
21412141

21422142
#if !(NETSTANDARD1_3 || NETSTANDARD1_6)

src/HtmlAgilityPack.Shared/MixedCodeDocumentFragmentList.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class MixedCodeDocumentFragmentList : IEnumerable
2121
#region Fields
2222

2323
private MixedCodeDocument _doc;
24-
private IList<MixedCodeDocumentFragment> _items = new List<MixedCodeDocumentFragment>();
24+
private List<MixedCodeDocumentFragment> _items = new List<MixedCodeDocumentFragment>();
2525

2626
#endregion
2727

0 commit comments

Comments
 (0)