Skip to content

Commit 84cea56

Browse files
Merge pull request #559 from lahma/replace-to-lower
Reduce need to call string.ToLower with proper comparisons
2 parents 9758885 + 70be67f commit 84cea56

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
@@ -369,13 +369,13 @@ public virtual string InnerText
369369
var sb = new StringBuilder();
370370
int depthLevel = 0;
371371
string name = this.Name;
372-
372+
373373
if (name != null)
374374
{
375-
name = name.ToLowerInvariant();
375+
bool isDisplayScriptingText = string.Equals(name, "head", StringComparison.OrdinalIgnoreCase)
376+
|| string.Equals(name, "script", StringComparison.OrdinalIgnoreCase)
377+
|| string.Equals(name, "style", StringComparison.OrdinalIgnoreCase);
376378

377-
bool isDisplayScriptingText = (name == "head" || name == "script" || name == "style");
378-
379379
InternalInnerText(sb, isDisplayScriptingText, depthLevel);
380380
}
381381
else
@@ -1984,7 +1984,7 @@ public virtual void WriteTo(TextWriter outText, int level = 0)
19841984
if (_ownerdocument.OptionOutputAsXml)
19851985
{
19861986
var commentNode = (HtmlCommentNode) this;
1987-
if (!_ownerdocument.BackwardCompatibility && commentNode.Comment.ToLowerInvariant().StartsWith("<!doctype"))
1987+
if (!_ownerdocument.BackwardCompatibility && commentNode.Comment.StartsWith("<!doctype", StringComparison.OrdinalIgnoreCase))
19881988
{
19891989
outText.Write(commentNode.Comment);
19901990
}
@@ -2623,6 +2623,8 @@ public void AddClass(string name)
26232623
AddClass(name, false);
26242624
}
26252625

2626+
private static readonly char[] spaceSeparator = { ' ' };
2627+
26262628
/// <summary>
26272629
/// Adds one or more classes to this node.
26282630
/// </summary>
@@ -2631,26 +2633,26 @@ public void AddClass(string name)
26312633
public void AddClass(string name, bool throwError)
26322634
{
26332635
var classAttributes = Attributes.AttributesWithName("class");
2636+
var isEmpty = true;
26342637

2635-
if (!IsEmpty(classAttributes))
2638+
foreach (HtmlAttribute att in classAttributes)
26362639
{
2637-
foreach (HtmlAttribute att in classAttributes)
2638-
{
2639-
// Check class solo, check class in First with other class, check Class no first.
2640-
if (att.Value != null && att.Value.Split(' ').ToList().Any(x => x.Equals(name)))
2641-
{
2642-
if (throwError)
2643-
{
2644-
throw new Exception(HtmlDocument.HtmlExceptionClassExists);
2645-
}
2646-
}
2647-
else
2640+
isEmpty = false;
2641+
// Check class solo, check class in First with other class, check Class no first.
2642+
if (att.Value != null && Array.IndexOf(att.Value.Split(spaceSeparator), name) != -1)
2643+
{
2644+
if (throwError)
26482645
{
2649-
SetAttributeValue(att.Name, att.Value + " " + name);
2646+
throw new Exception(HtmlDocument.HtmlExceptionClassExists);
26502647
}
26512648
}
2649+
else
2650+
{
2651+
SetAttributeValue(att.Name, att.Value + " " + name);
2652+
}
26522653
}
2653-
else
2654+
2655+
if (isEmpty)
26542656
{
26552657
HtmlAttribute attribute = _ownerdocument.CreateAttribute("class", name);
26562658
Attributes.Append(attribute);
@@ -2718,7 +2720,7 @@ public void RemoveClass(string name, bool throwError)
27182720
{
27192721
Attributes.Remove(att);
27202722
}
2721-
else if (att.Value != null && att.Value.Split(' ').ToList().Any(x => x.Equals(name)))
2723+
else if (att.Value != null && att.Value.Split(' ').Contains(name))
27222724
{
27232725
string[] classNames = att.Value.Split(' ');
27242726

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)