1 // HtmlAgilityPack V1.0 - Simon Mourier <simon underscore mourier at hotmail dot com>
6 using System
.Diagnostics
;
10 namespace HtmlAgilityPack
13 /// Represents an HTML attribute.
15 [DebuggerDisplay("Name: {OriginalName}, Value: {Value}")]
16 public class HtmlAttribute
: IComparable
21 internal int _lineposition
;
22 internal string _name
;
23 internal int _namelength
;
24 internal int _namestartindex
;
25 internal HtmlDocument _ownerdocument
; // attribute can exists without a node
26 internal HtmlNode _ownernode
;
27 private AttributeValueQuote _quoteType
= AttributeValueQuote
.DoubleQuote
;
28 internal int _streamposition
;
29 internal string _value
;
30 internal int _valuelength
;
31 internal int _valuestartindex
;
37 internal HtmlAttribute(HtmlDocument ownerdocument
)
39 _ownerdocument
= ownerdocument
;
47 /// Gets the line number of this attribute in the document.
52 internal set { _line = value; }
56 /// Gets the column number of this attribute in the document.
58 public int LinePosition
60 get { return _lineposition; }
64 /// Gets the qualified name of the attribute.
72 _name
= _ownerdocument
._text
.Substring(_namestartindex
, _namelength
);
74 return _name
.ToLower();
80 throw new ArgumentNullException("value");
83 if (_ownernode
!= null)
85 _ownernode
._innerchanged
= true;
86 _ownernode
._outerchanged
= true;
92 /// Name of attribute with original case
94 public string OriginalName
100 /// Gets the HTML document to which this attribute belongs.
102 public HtmlDocument OwnerDocument
104 get { return _ownerdocument; }
108 /// Gets the HTML node to which this attribute belongs.
110 public HtmlNode OwnerNode
112 get { return _ownernode; }
116 /// Specifies what type of quote the data should be wrapped in
118 public AttributeValueQuote QuoteType
120 get { return _quoteType; }
121 set { _quoteType = value; }
125 /// Gets the stream position of this attribute in the document, relative to the start of the document.
127 public int StreamPosition
129 get { return _streamposition; }
133 /// Gets or sets the value of the attribute.
141 _value
= _ownerdocument
._text
.Substring(_valuestartindex
, _valuelength
);
148 if (_ownernode
!= null)
150 _ownernode
._innerchanged
= true;
151 _ownernode
._outerchanged
= true;
156 internal string XmlName
158 get { return HtmlDocument.GetXmlName(Name); }
161 internal string XmlValue
163 get { return Value; }
167 /// Gets a valid XPath string that points to this Attribute
173 string basePath
= (OwnerNode
== null) ? "/" : OwnerNode
.XPath
+ "/";
174 return basePath
+ GetRelativeXpath();
180 #region IComparable Members
183 /// Compares the current instance with another attribute. Comparison is based on attributes' name.
185 /// <param name="obj">An attribute to compare with this instance.</param>
186 /// <returns>A 32-bit signed integer that indicates the relative order of the names comparison.</returns>
187 public int CompareTo(object obj
)
189 HtmlAttribute att
= obj
as HtmlAttribute
;
192 throw new ArgumentException("obj");
194 return Name
.CompareTo(att
.Name
);
199 #region Public Methods
202 /// Creates a duplicate of this attribute.
204 /// <returns>The cloned attribute.</returns>
205 public HtmlAttribute
Clone()
207 HtmlAttribute att
= new HtmlAttribute(_ownerdocument
);
214 /// Removes this attribute from it's parents collection
218 _ownernode
.Attributes
.Remove(this);
223 #region Private Methods
225 private string GetRelativeXpath()
227 if (OwnerNode
== null)
231 foreach (HtmlAttribute node
in OwnerNode
.Attributes
)
233 if (node
.Name
!= Name
) continue;
240 return "@" + Name
+ "[" + i
+ "]";
247 /// An Enum representing different types of Quotes used for surrounding attribute values
249 public enum AttributeValueQuote
252 /// A single quote mark '
256 /// A double quote mark "