Fix eglib targets and filters.
[mono.git] / docs / HtmlAgilityPack / HtmlAttribute.cs
1 // HtmlAgilityPack V1.0 - Simon Mourier <simon underscore mourier at hotmail dot com>\r
2 \r
3 #region\r
4 \r
5 using System;\r
6 using System.Diagnostics;\r
7 \r
8 #endregion\r
9 \r
10 namespace HtmlAgilityPack\r
11 {\r
12     /// <summary>\r
13     /// Represents an HTML attribute.\r
14     /// </summary>\r
15     [DebuggerDisplay("Name: {OriginalName}, Value: {Value}")]\r
16     public class HtmlAttribute : IComparable\r
17     {\r
18         #region Fields\r
19 \r
20         private int _line;\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
32 \r
33         #endregion\r
34 \r
35         #region Constructors\r
36 \r
37         internal HtmlAttribute(HtmlDocument ownerdocument)\r
38         {\r
39             _ownerdocument = ownerdocument;\r
40         }\r
41 \r
42         #endregion\r
43 \r
44         #region Properties\r
45 \r
46         /// <summary>\r
47         /// Gets the line number of this attribute in the document.\r
48         /// </summary>\r
49         public int Line\r
50         {\r
51             get { return _line; }\r
52             internal set { _line = value; }\r
53         }\r
54 \r
55         /// <summary>\r
56         /// Gets the column number of this attribute in the document.\r
57         /// </summary>\r
58         public int LinePosition\r
59         {\r
60             get { return _lineposition; }\r
61         }\r
62 \r
63         /// <summary>\r
64         /// Gets the qualified name of the attribute.\r
65         /// </summary>\r
66         public string Name\r
67         {\r
68             get\r
69             {\r
70                 if (_name == null)\r
71                 {\r
72                     _name = _ownerdocument._text.Substring(_namestartindex, _namelength);\r
73                 }\r
74                 return _name.ToLower();\r
75             }\r
76             set\r
77             {\r
78                 if (value == null)\r
79                 {\r
80                     throw new ArgumentNullException("value");\r
81                 }\r
82                 _name = value;\r
83                 if (_ownernode != null)\r
84                 {\r
85                     _ownernode._innerchanged = true;\r
86                     _ownernode._outerchanged = true;\r
87                 }\r
88             }\r
89         }\r
90 \r
91         /// <summary>\r
92         /// Name of attribute with original case\r
93         /// </summary>\r
94         public string OriginalName\r
95         {\r
96             get { return _name; }\r
97         }\r
98 \r
99         /// <summary>\r
100         /// Gets the HTML document to which this attribute belongs.\r
101         /// </summary>\r
102         public HtmlDocument OwnerDocument\r
103         {\r
104             get { return _ownerdocument; }\r
105         }\r
106 \r
107         /// <summary>\r
108         /// Gets the HTML node to which this attribute belongs.\r
109         /// </summary>\r
110         public HtmlNode OwnerNode\r
111         {\r
112             get { return _ownernode; }\r
113         }\r
114 \r
115         /// <summary>\r
116         /// Specifies what type of quote the data should be wrapped in\r
117         /// </summary>\r
118         public AttributeValueQuote QuoteType\r
119         {\r
120             get { return _quoteType; }\r
121             set { _quoteType = value; }\r
122         }\r
123 \r
124         /// <summary>\r
125         /// Gets the stream position of this attribute in the document, relative to the start of the document.\r
126         /// </summary>\r
127         public int StreamPosition\r
128         {\r
129             get { return _streamposition; }\r
130         }\r
131 \r
132         /// <summary>\r
133         /// Gets or sets the value of the attribute.\r
134         /// </summary>\r
135         public string Value\r
136         {\r
137             get\r
138             {\r
139                 if (_value == null)\r
140                 {\r
141                     _value = _ownerdocument._text.Substring(_valuestartindex, _valuelength);\r
142                 }\r
143                 return _value;\r
144             }\r
145             set\r
146             {\r
147                 _value = value;\r
148                 if (_ownernode != null)\r
149                 {\r
150                     _ownernode._innerchanged = true;\r
151                     _ownernode._outerchanged = true;\r
152                 }\r
153             }\r
154         }\r
155 \r
156         internal string XmlName\r
157         {\r
158             get { return HtmlDocument.GetXmlName(Name); }\r
159         }\r
160 \r
161         internal string XmlValue\r
162         {\r
163             get { return Value; }\r
164         }\r
165 \r
166         /// <summary>\r
167         /// Gets a valid XPath string that points to this Attribute\r
168         /// </summary>\r
169         public string XPath\r
170         {\r
171             get\r
172             {\r
173                 string basePath = (OwnerNode == null) ? "/" : OwnerNode.XPath + "/";\r
174                 return basePath + GetRelativeXpath();\r
175             }\r
176         }\r
177 \r
178         #endregion\r
179 \r
180         #region IComparable Members\r
181 \r
182         /// <summary>\r
183         /// Compares the current instance with another attribute. Comparison is based on attributes' name.\r
184         /// </summary>\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
188         {\r
189             HtmlAttribute att = obj as HtmlAttribute;\r
190             if (att == null)\r
191             {\r
192                 throw new ArgumentException("obj");\r
193             }\r
194             return Name.CompareTo(att.Name);\r
195         }\r
196 \r
197         #endregion\r
198 \r
199         #region Public Methods\r
200 \r
201         /// <summary>\r
202         /// Creates a duplicate of this attribute.\r
203         /// </summary>\r
204         /// <returns>The cloned attribute.</returns>\r
205         public HtmlAttribute Clone()\r
206         {\r
207             HtmlAttribute att = new HtmlAttribute(_ownerdocument);\r
208             att.Name = Name;\r
209             att.Value = Value;\r
210             return att;\r
211         }\r
212 \r
213         /// <summary>\r
214         /// Removes this attribute from it's parents collection\r
215         /// </summary>\r
216         public void Remove()\r
217         {\r
218             _ownernode.Attributes.Remove(this);\r
219         }\r
220 \r
221         #endregion\r
222 \r
223         #region Private Methods\r
224 \r
225         private string GetRelativeXpath()\r
226         {\r
227             if (OwnerNode == null)\r
228                 return Name;\r
229 \r
230             int i = 1;\r
231             foreach (HtmlAttribute node in OwnerNode.Attributes)\r
232             {\r
233                 if (node.Name != Name) continue;\r
234 \r
235                 if (node == this)\r
236                     break;\r
237 \r
238                 i++;\r
239             }\r
240             return "@" + Name + "[" + i + "]";\r
241         }\r
242 \r
243         #endregion\r
244     }\r
245 \r
246     /// <summary>\r
247     /// An Enum representing different types of Quotes used for surrounding attribute values\r
248     /// </summary>\r
249     public enum AttributeValueQuote\r
250     {\r
251         /// <summary>\r
252         /// A single quote mark '\r
253         /// </summary>\r
254         SingleQuote,\r
255         /// <summary>\r
256         /// A double quote mark "\r
257         /// </summary>\r
258         DoubleQuote\r
259     }\r
260 }