[bcl] Add API diff targets (#4406)
[mono-project.git] / docs / HtmlAgilityPack / HtmlAttribute.cs
blob7a3de047bea9d050237ea93b0072856f5e7085ec
1 // HtmlAgilityPack V1.0 - Simon Mourier <simon underscore mourier at hotmail dot com>
3 #region
5 using System;
6 using System.Diagnostics;
8 #endregion
10 namespace HtmlAgilityPack
12 /// <summary>
13 /// Represents an HTML attribute.
14 /// </summary>
15 [DebuggerDisplay("Name: {OriginalName}, Value: {Value}")]
16 public class HtmlAttribute : IComparable
18 #region Fields
20 private int _line;
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;
33 #endregion
35 #region Constructors
37 internal HtmlAttribute(HtmlDocument ownerdocument)
39 _ownerdocument = ownerdocument;
42 #endregion
44 #region Properties
46 /// <summary>
47 /// Gets the line number of this attribute in the document.
48 /// </summary>
49 public int Line
51 get { return _line; }
52 internal set { _line = value; }
55 /// <summary>
56 /// Gets the column number of this attribute in the document.
57 /// </summary>
58 public int LinePosition
60 get { return _lineposition; }
63 /// <summary>
64 /// Gets the qualified name of the attribute.
65 /// </summary>
66 public string Name
68 get
70 if (_name == null)
72 _name = _ownerdocument._text.Substring(_namestartindex, _namelength);
74 return _name.ToLower();
76 set
78 if (value == null)
80 throw new ArgumentNullException("value");
82 _name = value;
83 if (_ownernode != null)
85 _ownernode._innerchanged = true;
86 _ownernode._outerchanged = true;
91 /// <summary>
92 /// Name of attribute with original case
93 /// </summary>
94 public string OriginalName
96 get { return _name; }
99 /// <summary>
100 /// Gets the HTML document to which this attribute belongs.
101 /// </summary>
102 public HtmlDocument OwnerDocument
104 get { return _ownerdocument; }
107 /// <summary>
108 /// Gets the HTML node to which this attribute belongs.
109 /// </summary>
110 public HtmlNode OwnerNode
112 get { return _ownernode; }
115 /// <summary>
116 /// Specifies what type of quote the data should be wrapped in
117 /// </summary>
118 public AttributeValueQuote QuoteType
120 get { return _quoteType; }
121 set { _quoteType = value; }
124 /// <summary>
125 /// Gets the stream position of this attribute in the document, relative to the start of the document.
126 /// </summary>
127 public int StreamPosition
129 get { return _streamposition; }
132 /// <summary>
133 /// Gets or sets the value of the attribute.
134 /// </summary>
135 public string Value
139 if (_value == null)
141 _value = _ownerdocument._text.Substring(_valuestartindex, _valuelength);
143 return _value;
147 _value = value;
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; }
166 /// <summary>
167 /// Gets a valid XPath string that points to this Attribute
168 /// </summary>
169 public string XPath
173 string basePath = (OwnerNode == null) ? "/" : OwnerNode.XPath + "/";
174 return basePath + GetRelativeXpath();
178 #endregion
180 #region IComparable Members
182 /// <summary>
183 /// Compares the current instance with another attribute. Comparison is based on attributes' name.
184 /// </summary>
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;
190 if (att == null)
192 throw new ArgumentException("obj");
194 return Name.CompareTo(att.Name);
197 #endregion
199 #region Public Methods
201 /// <summary>
202 /// Creates a duplicate of this attribute.
203 /// </summary>
204 /// <returns>The cloned attribute.</returns>
205 public HtmlAttribute Clone()
207 HtmlAttribute att = new HtmlAttribute(_ownerdocument);
208 att.Name = Name;
209 att.Value = Value;
210 return att;
213 /// <summary>
214 /// Removes this attribute from it's parents collection
215 /// </summary>
216 public void Remove()
218 _ownernode.Attributes.Remove(this);
221 #endregion
223 #region Private Methods
225 private string GetRelativeXpath()
227 if (OwnerNode == null)
228 return Name;
230 int i = 1;
231 foreach (HtmlAttribute node in OwnerNode.Attributes)
233 if (node.Name != Name) continue;
235 if (node == this)
236 break;
238 i++;
240 return "@" + Name + "[" + i + "]";
243 #endregion
246 /// <summary>
247 /// An Enum representing different types of Quotes used for surrounding attribute values
248 /// </summary>
249 public enum AttributeValueQuote
251 /// <summary>
252 /// A single quote mark '
253 /// </summary>
254 SingleQuote,
255 /// <summary>
256 /// A double quote mark "
257 /// </summary>
258 DoubleQuote