Skip to content

Commit 814af63

Browse files
Merge pull request #554 from POFerro/quote_type
Review Attribute QuoteType Behavior vs InternalQuoteType fixes #552, #516
2 parents 8efd5da + 045421a commit 814af63

File tree

7 files changed

+47
-25
lines changed

7 files changed

+47
-25
lines changed

src/HtmlAgilityPack.Shared/HtmlAttribute.cs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ public class HtmlAttribute : IComparable
3131
internal int _namestartindex;
3232
internal HtmlDocument _ownerdocument; // attribute can exists without a node
3333
internal HtmlNode _ownernode;
34-
private AttributeValueQuote _quoteType = AttributeValueQuote.DoubleQuote;
34+
private AttributeValueQuote? _quoteType;
3535
internal int _streamposition;
3636
internal string _value;
3737
internal int _valuelength;
3838
internal int _valuestartindex;
39-
internal bool _isFromParse;
40-
internal bool _hasEqual;
39+
//internal bool _isFromParse;
40+
//internal bool _hasEqual;
4141
private bool? _localUseOriginalName;
4242

4343
#endregion
@@ -168,14 +168,14 @@ public HtmlNode OwnerNode
168168
/// </summary>
169169
public AttributeValueQuote QuoteType
170170
{
171-
get { return _quoteType; }
172-
set { _quoteType = value; }
171+
get { return _quoteType ?? this.InternalQuoteType ?? this.OwnerDocument.GlobalAttributeValueQuote ?? AttributeValueQuote.DoubleQuote; }
172+
set { _quoteType = value != AttributeValueQuote.Initial ? (AttributeValueQuote?)value : null; }
173173
}
174174

175175
/// <summary>
176176
/// Specifies what type of quote the data should be wrapped in (internal to keep backward compatibility)
177177
/// </summary>
178-
internal AttributeValueQuote InternalQuoteType { get; set; }
178+
internal AttributeValueQuote? InternalQuoteType { get; set; }
179179

180180
/// <summary>
181181
/// Gets the stream position of this attribute in the document, relative to the start of the document.
@@ -213,6 +213,10 @@ public string Value
213213
set
214214
{
215215
_value = value;
216+
if (!string.IsNullOrEmpty(_value) && this.QuoteType == AttributeValueQuote.WithoutValue)
217+
{
218+
this.InternalQuoteType = this.OwnerDocument.GlobalAttributeValueQuote ?? AttributeValueQuote.DoubleQuote;
219+
}
216220

217221
if (_ownernode != null)
218222
{
@@ -284,11 +288,11 @@ public HtmlAttribute Clone()
284288
HtmlAttribute att = new HtmlAttribute(_ownerdocument);
285289
att.Name = OriginalName;
286290
att.Value = Value;
287-
att.QuoteType = QuoteType;
291+
att._quoteType = _quoteType;
288292
att.InternalQuoteType = InternalQuoteType;
289293

290-
att._isFromParse = _isFromParse;
291-
att._hasEqual = _hasEqual;
294+
//att._isFromParse = _isFromParse;
295+
//att._hasEqual = _hasEqual;
292296
return att;
293297
}
294298

src/HtmlAgilityPack.Shared/HtmlDocument.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1503,7 +1503,7 @@ private void Parse()
15031503
if (NewCheck())
15041504
continue;
15051505

1506-
_currentattribute._isFromParse = true;
1506+
//_currentattribute._isFromParse = true;
15071507

15081508

15091509
// Add !,?,% and other special?
@@ -1525,7 +1525,7 @@ private void Parse()
15251525
if (_c == '=')
15261526
{
15271527
PushAttributeNameEnd(_index - 1);
1528-
_currentattribute._hasEqual = true;
1528+
//_currentattribute._hasEqual = true;
15291529
_state = ParseState.AttributeAfterEquals;
15301530
continue;
15311531
}
@@ -1573,7 +1573,7 @@ private void Parse()
15731573

15741574
if (_c == '=')
15751575
{
1576-
_currentattribute._hasEqual = true;
1576+
//_currentattribute._hasEqual = true;
15771577
_state = ParseState.AttributeAfterEquals;
15781578
continue;
15791579
}
@@ -1822,6 +1822,7 @@ private void PushAttributeNameStart(int index, int lineposition)
18221822
_currentattribute.Line = _line;
18231823
_currentattribute._lineposition = lineposition;
18241824
_currentattribute._streamposition = index;
1825+
_currentattribute.InternalQuoteType = AttributeValueQuote.WithoutValue;
18251826
}
18261827

18271828
private void PushAttributeValueEnd(int index)
@@ -2044,11 +2045,13 @@ private void PushAttributeValueStart(int index, int quote)
20442045
_currentattribute._valuestartindex = index;
20452046
if (quote == '\'')
20462047
{
2047-
_currentattribute.QuoteType = AttributeValueQuote.SingleQuote;
2048+
_currentattribute.InternalQuoteType = AttributeValueQuote.SingleQuote;
2049+
}
2050+
if (quote == '"')
2051+
{
2052+
_currentattribute.InternalQuoteType = AttributeValueQuote.DoubleQuote;
20482053
}
20492054

2050-
_currentattribute.InternalQuoteType = _currentattribute.QuoteType;
2051-
20522055
if (quote == 0)
20532056
{
20542057
_currentattribute.InternalQuoteType = AttributeValueQuote.None;

src/HtmlAgilityPack.Shared/HtmlNode.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2346,15 +2346,16 @@ internal void WriteAttribute(TextWriter outText, HtmlAttribute att)
23462346
}
23472347

23482348
var quoteType = OwnerDocument.GlobalAttributeValueQuote ?? att.QuoteType;
2349-
var isWithoutValue = quoteType == AttributeValueQuote.WithoutValue
2350-
|| (quoteType == AttributeValueQuote.Initial && att._isFromParse && !att._hasEqual && string.IsNullOrEmpty(att.XmlValue));
2349+
//var isWithoutValue = quoteType == AttributeValueQuote.WithoutValue
2350+
// || (quoteType == AttributeValueQuote.Initial && att._isFromParse && !att._hasEqual && string.IsNullOrEmpty(att.XmlValue));
23512351

2352-
if (quoteType == AttributeValueQuote.Initial && !(att._isFromParse && !att._hasEqual && string.IsNullOrEmpty(att.XmlValue)))
2352+
if (quoteType == AttributeValueQuote.Initial/* && !(att._isFromParse && !att._hasEqual && string.IsNullOrEmpty(att.XmlValue))*/)
23532353
{
2354-
quoteType = att.InternalQuoteType;
2354+
quoteType = att.QuoteType;
23552355
}
2356+
var isWithoutValue = quoteType == AttributeValueQuote.WithoutValue;
23562357

2357-
string name;
2358+
string name;
23582359
string quote = quoteType == AttributeValueQuote.DoubleQuote ? "\"" : quoteType == AttributeValueQuote.SingleQuote ? "'" : "";
23592360
if (_ownerdocument.OptionOutputAsXml)
23602361
{

src/Tests/HtmlAgilityPack.Tests.Net45/HtmlDocument.PreserveOriginalTest.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
using NUnit.Framework;
22
using System;
33
using System.IO;
4+
using System.Linq;
45
using System.Xml.XPath;
56

6-
namespace HtmlAgilityPack.Tests.fx._4._5
7+
namespace HtmlAgilityPack.Tests
78
{
89
[TestFixture]
910
public class HtmlDocumentPreserveOriginalTest
@@ -168,5 +169,18 @@ public void PreserveClonedEmptyAttributesTest()
168169

169170
Assert.AreEqual(@"<list-counter formErrorsCounter></list-counter>", cloned.OuterHtml);
170171
}
172+
173+
[Test]
174+
public void PreserveQuoteTypeForLoadedAttributes()
175+
{
176+
var input = HtmlNode.CreateNode("<input checked></input>");
177+
var checkedAttribute = input.Attributes.First();
178+
179+
// Result is: Value: '' (empty string)
180+
Assert.AreEqual("", checkedAttribute.Value);
181+
182+
// Result is: QuoteType: WithoutValue
183+
Assert.AreEqual(AttributeValueQuote.WithoutValue, checkedAttribute.QuoteType);
184+
}
171185
}
172186
}

src/Tests/HtmlAgilityPack.Tests.Net45/UnitTest1.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System;
22
using Microsoft.VisualStudio.TestTools.UnitTesting;
33

4-
namespace HtmlAgilityPack.Tests.fx._4._5
4+
namespace HtmlAgilityPack.Tests
55
{
66
[TestClass]
77
public class UnitTest1

src/Tests/HtmlAgilityPack.Tests.Net45/files/attr_quote.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
 <form xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
1+
<form xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
22
<page>
33

44
<page-body>

src/Tests/HtmlAgilityPack.Tests.Net45/files/attr_quote_expected.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<form xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
1+
<form xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
22
<page>
33

44
<page-body>
@@ -22,7 +22,7 @@
2222
{{ dimension.code }} - {{ dimension.description }}
2323
</mat-option>
2424
</mat-select>
25-
<span readOnlyValue> {{ simulationRequest.categoriaBemId | commonData:lea546CATBEM$:'id':'code-desc' }} </span>
25+
<span readOnlyValue>{{ simulationRequest.categoriaBemId | commonData:lea546CATBEM$:'id':'code-desc' }}</span>
2626
<field-error-alert matSuffix></field-error-alert>
2727
<mat-error></mat-error>
2828
</mat-form-field>

0 commit comments

Comments
 (0)