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

Fix quotetype and withoutvalue behavior #576

Closed
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
2 changes: 1 addition & 1 deletion src/HtmlAgilityPack.Shared/HtmlDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1867,7 +1867,7 @@ private void PushAttributeNameStart(int index, int lineposition)
_currentattribute.Line = _line;
_currentattribute._lineposition = lineposition;
_currentattribute._streamposition = index;
_currentattribute.InternalQuoteType = AttributeValueQuote.WithoutValue;
//_currentattribute.InternalQuoteType = AttributeValueQuote.WithoutValue;
}

private void PushAttributeValueEnd(int index)
Expand Down
55 changes: 37 additions & 18 deletions src/HtmlAgilityPack.Shared/HtmlNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2415,7 +2415,24 @@ internal void WriteAttribute(TextWriter outText, HtmlAttribute att)
var isWithoutValue = quoteType == AttributeValueQuote.WithoutValue;

string name;
string quote = quoteType == AttributeValueQuote.DoubleQuote ? "\"" : quoteType == AttributeValueQuote.SingleQuote ? "'" : "";
string quote;
switch (quoteType)
{
case AttributeValueQuote.DoubleQuote:
case AttributeValueQuote.Initial:
case AttributeValueQuote.WithoutValue:
quote = "\"";
break;
case AttributeValueQuote.SingleQuote:
quote = "'";
break;
case AttributeValueQuote.None:
default:
quote = "";
break;
}


if (_ownerdocument.OptionOutputAsXml)
{
if(quoteType != AttributeValueQuote.DoubleQuote && quoteType != AttributeValueQuote.SingleQuote)
Expand Down Expand Up @@ -2457,24 +2474,26 @@ internal void WriteAttribute(TextWriter outText, HtmlAttribute att)
}
}

if (!isWithoutValue)
{
var value = quoteType == AttributeValueQuote.DoubleQuote ? !att.Value.StartsWith("@") ? att.Value.Replace("\"", """) :
att.Value : quoteType == AttributeValueQuote.SingleQuote ? att.Value.Replace("'", "'") : att.Value;
if (_ownerdocument.OptionOutputOptimizeAttributeValues)
if (att.Value.IndexOfAny(new char[] {(char) 10, (char) 13, (char) 9, ' '}) < 0)
outText.Write(" " + name + "=" + att.Value);
else
outText.Write(" " + name + "=" + quote + value + quote);
else
outText.Write(" " + name + "=" + quote + value + quote);
}
else
if (isWithoutValue)
{
outText.Write(" " + name);
}

}
outText.Write(" " + name);
}
else
{
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);
else
outText.Write(" " + name + "=" + quote + value + quote);
else
outText.Write(" " + name + "=" + quote + value + quote);
}
}
}

internal void WriteAttributes(TextWriter outText, bool closing)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,31 @@ public void PreserveClonedEmptyAttributesTest()
Assert.Equal(@"<list-counter formErrorsCounter></list-counter>", cloned.OuterHtml);
}

[Fact]
public void PreserveEmptyAttributesWithInitialTest()
{
var d = new HtmlDocument();
d.LoadHtml("<bar ng-app class='message'></bar>");

var node = d.DocumentNode.SelectSingleNode("//bar");
var outer = node.OuterHtml;

Assert.Equal("<bar ng-app=\"\" class='message'></bar>", outer);
}

[Fact]
public void PreserveEmptyAttributes()
{
var d = new HtmlDocument { GlobalAttributeValueQuote = AttributeValueQuote.Initial };
d.LoadHtml("<li ng>Nothing to show</li>");

var node = d.DocumentNode.SelectSingleNode("//li");
var outer = node.OuterHtml;

Assert.Equal($"<li ng=\"\">Nothing to show</li>", outer);
}


[Fact]
public void PreserveQuoteTypeForLoadedAttributes()
{
Expand All @@ -173,5 +198,30 @@ public void PreserveQuoteTypeForLoadedAttributes()
// Result is: QuoteType: WithoutValue
Assert.Equal(AttributeValueQuote.WithoutValue, checkedAttribute.QuoteType);
}

[Fact]
public void PreserveQuoteTypeForLoadedAttributes2()
{
var d = new HtmlDocument { GlobalAttributeValueQuote = AttributeValueQuote.Initial };
d.LoadHtml(@"<bar ng-app ng-app2='message'></bar>");

var node = d.DocumentNode.SelectSingleNode("//bar");
var outer = node.OuterHtml;

Assert.Equal($"<bar ng-app=\"\" ng-app2='message'></bar>", outer);
}

[Fact]
public void PreserveQuoteTypeForLoadedAttributes3()
{
var input = HtmlNode.CreateNode(@"<bar ng-app ng-app2='message'></bar>");
var firstAttribute = input.Attributes.First();
var secondAttribute = input.Attributes.LastOrDefault();

Assert.Equal("", firstAttribute.Value);
Assert.Equal(AttributeValueQuote.DoubleQuote, firstAttribute.QuoteType);
Assert.Equal("message", secondAttribute.Value);
Assert.Equal(AttributeValueQuote.SingleQuote, secondAttribute.QuoteType);
}
}
}