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

Slipt quotetype and withoutvalue; #577

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
18 changes: 7 additions & 11 deletions src/HtmlAgilityPack.Shared/HtmlAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class HtmlAttribute : IComparable
internal int _streamposition;
internal string _value;
internal int _valuelength;
internal int _valuestartindex;
internal int _valuestartindex;
private bool? _localUseOriginalName;

#endregion
Expand Down Expand Up @@ -92,11 +92,11 @@ public bool UseOriginalName
{
var useOriginalName = false;
if (this._localUseOriginalName.HasValue)
{
{
useOriginalName = this._localUseOriginalName.Value;
}
else if (this.OwnerDocument != null)
{
{
useOriginalName = this.OwnerDocument.OptionDefaultUseOriginalName;
}

Expand All @@ -105,7 +105,7 @@ public bool UseOriginalName
set
{
this._localUseOriginalName = value;
}
}
}

/// <summary>
Expand All @@ -120,8 +120,8 @@ public string Name
_name = _ownerdocument.Text.Substring(_namestartindex, _namelength);
}

return UseOriginalName ? _name : _name.ToLowerInvariant();
}
return UseOriginalName ? _name : _name.ToLowerInvariant();
}
set
{
if (value == null)
Expand Down Expand Up @@ -211,7 +211,7 @@ public string Value
set
{
_value = value;
if (!string.IsNullOrEmpty(_value) && (this.QuoteType == AttributeValueQuote.WithoutValue || this.QuoteType == AttributeValueQuote.None))
if (!string.IsNullOrEmpty(_value) && this.QuoteType == AttributeValueQuote.None)
{
this.InternalQuoteType = this.OwnerDocument.GlobalAttributeValueQuote != AttributeValueQuote.Initial ?
(this.OwnerDocument.GlobalAttributeValueQuote ?? AttributeValueQuote.DoubleQuote)
Expand Down Expand Up @@ -348,10 +348,6 @@ public enum AttributeValueQuote
/// </summary>
None,


/// <summary>Without the value such as '&lt;span readonly&gt;'</summary>
WithoutValue,

/// <summary>
/// The initial value (current value)
/// </summary>
Expand Down
4 changes: 3 additions & 1 deletion src/HtmlAgilityPack.Shared/HtmlDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,9 @@ public static bool DisableBehaviorTagP
/// <summary>Defines the global attribute value quote. When specified, it will always win.</summary>
public AttributeValueQuote? GlobalAttributeValueQuote;

/// <summary>Without the value such as '&lt;span readonly&gt;'</summary>
public bool AttributeWithoutValue;

/// <summary>
/// Defines if name must be output with it's original case. Useful for asp.net tags and attributes. Default is false.
/// </summary>
Expand Down Expand Up @@ -1867,7 +1870,6 @@ private void PushAttributeNameStart(int index, int lineposition)
_currentattribute.Line = _line;
_currentattribute._lineposition = lineposition;
_currentattribute._streamposition = index;
_currentattribute.InternalQuoteType = AttributeValueQuote.WithoutValue;
}

private void PushAttributeValueEnd(int index)
Expand Down
39 changes: 20 additions & 19 deletions src/HtmlAgilityPack.Shared/HtmlNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2412,10 +2412,12 @@ internal void WriteAttribute(TextWriter outText, HtmlAttribute att)
quoteType = att.QuoteType;
}

var isWithoutValue = quoteType == AttributeValueQuote.WithoutValue;

string name;
string quote = quoteType == AttributeValueQuote.DoubleQuote ? "\"" : quoteType == AttributeValueQuote.SingleQuote ? "'" : "";
var quote = quoteType == AttributeValueQuote.DoubleQuote ? "\"" : quoteType == AttributeValueQuote.SingleQuote ? "'" : "";

if (quoteType == AttributeValueQuote.Initial && quote == "")
quote = "\"";

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

if (!isWithoutValue)
{
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);
}
else
if (string.IsNullOrEmpty(att.Value) && _ownerdocument.AttributeWithoutValue)
{
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);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public void PreserveQuoteTypeForLoadedAttributes()
Assert.AreEqual("", checkedAttribute.Value);

// Result is: QuoteType: WithoutValue
Assert.AreEqual(AttributeValueQuote.WithoutValue, checkedAttribute.QuoteType);
Assert.AreEqual(AttributeValueQuote.DoubleQuote, checkedAttribute.QuoteType);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public void PreserveEmptyAttributesTest()
d.OptionEmptyCollection = true;

d.GlobalAttributeValueQuote = AttributeValueQuote.Initial;
d.AttributeWithoutValue = true;

d.OptionDefaultUseOriginalName = true;
//d.OptionPreserveEmptyAttributes = true;
Expand Down Expand Up @@ -66,6 +67,7 @@ public void PreserveOriginalCasingTest()
d.OptionEmptyCollection = true;

d.GlobalAttributeValueQuote = AttributeValueQuote.Initial;
d.AttributeWithoutValue = true;

d.OptionDefaultUseOriginalName = true;
//d.OptionPreserveEmptyAttributes = true;
Expand Down Expand Up @@ -118,6 +120,7 @@ public void PreserveOriginalQuoteTest()
d.OptionEmptyCollection = true;

d.GlobalAttributeValueQuote = AttributeValueQuote.Initial;
d.AttributeWithoutValue = true;
d.OptionDefaultUseOriginalName = true;

var filePath = Path.Combine(contentDirectory, "attr_quote.html");
Expand All @@ -144,23 +147,46 @@ public void PreserveOriginalQuoteTest()
Assert.Equal(expectedOuterNode, clone.OuterHtml);

}



[Fact]
public void PreserveClonedEmptyAttributesTest()
{
var d = new HtmlDocument();
d.GlobalAttributeValueQuote = AttributeValueQuote.Initial;
d.OptionDefaultUseOriginalName = true;
d.AttributeWithoutValue = true;

var html = @"<list-counter formErrorsCounter></list-counter>";
d.LoadHtml(html);

var cloned = d.DocumentNode.CloneNode(true);

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 @@ -171,7 +197,32 @@ public void PreserveQuoteTypeForLoadedAttributes()
Assert.Equal("", checkedAttribute.Value);

// Result is: QuoteType: WithoutValue
Assert.Equal(AttributeValueQuote.WithoutValue, checkedAttribute.QuoteType);
Assert.Equal(AttributeValueQuote.DoubleQuote, 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);
}
}
}