Skip to content

Commit

Permalink
Optimize HtmlNode.WriteAttribute
Browse files Browse the repository at this point in the history
  • Loading branch information
lahma committed Jun 29, 2024
1 parent 8efd5da commit aa058eb
Showing 1 changed file with 39 additions and 9 deletions.
48 changes: 39 additions & 9 deletions src/HtmlAgilityPack.Shared/HtmlNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2337,6 +2337,8 @@ internal void SetId(string id)
SetChanged();
}

private static readonly char[] optimizeAttributesCheckedChars = new char[] { (char) 10, (char) 13, (char) 9, ' ' };

internal void WriteAttribute(TextWriter outText, HtmlAttribute att)
{
if (att.Value == null)
Expand Down Expand Up @@ -2364,11 +2366,17 @@ internal void WriteAttribute(TextWriter outText, HtmlAttribute att)

if (!isWithoutValue)
{
outText.Write(" " + name + "=" + quote + HtmlDocument.HtmlEncodeWithCompatibility(att.XmlValue, _ownerdocument.BackwardCompatibility) + quote);
outText.Write(" ");
outText.Write(name);
outText.Write("=");
outText.Write(quote);
outText.Write(HtmlDocument.HtmlEncodeWithCompatibility(att.XmlValue, _ownerdocument.BackwardCompatibility));
outText.Write(quote);
}
else
{
outText.Write(" " + name);
outText.Write(" ");
outText.Write(name);
}
}
else
Expand All @@ -2381,7 +2389,8 @@ internal void WriteAttribute(TextWriter outText, HtmlAttribute att)
if ((att.Name[0] == '<') && (att.Name[1] == '%') &&
(att.Name[att.Name.Length - 1] == '>') && (att.Name[att.Name.Length - 2] == '%'))
{
outText.Write(" " + name);
outText.Write(" ");
outText.Write(name);
return;
}
}
Expand All @@ -2391,18 +2400,39 @@ internal void WriteAttribute(TextWriter outText, HtmlAttribute att)
var value = quoteType == AttributeValueQuote.DoubleQuote ? !att.Value.StartsWith("@") ? att.Value.Replace("\"", "&quot;") :
att.Value : quoteType == AttributeValueQuote.SingleQuote ? att.Value.Replace("'", "&#39;") : att.Value;
if (_ownerdocument.OptionOutputOptimizeAttributeValues)
if (att.Value.IndexOfAny(new char[] {(char) 10, (char) 13, (char) 9, ' '}) < 0)
outText.Write(" " + name + "=" + att.Value);
{
if (att.Value.IndexOfAny(optimizeAttributesCheckedChars) < 0)
{
outText.Write(" ");
outText.Write(name);
outText.Write("=");
outText.Write(att.Value);
}
else
outText.Write(" " + name + "=" + quote + value + quote);
{
outText.Write(" ");
outText.Write(name);
outText.Write("=");
outText.Write(quote);
outText.Write(value);
outText.Write(quote);
}
}
else
outText.Write(" " + name + "=" + quote + value + quote);
{
outText.Write(" ");
outText.Write(name);
outText.Write("=");
outText.Write(quote);
outText.Write(value);
outText.Write(quote);
}
}
else
{
outText.Write(" " + name);
outText.Write(" ");
outText.Write(name);
}

}
}

Expand Down

0 comments on commit aa058eb

Please sign in to comment.