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