Merge pull request #5668 from kumpera/wasm-work-p4
[mono.git] / docs / HtmlAgilityPack / HtmlAttributeCollection.cs
1 // HtmlAgilityPack V1.0 - Simon Mourier <simon underscore mourier at hotmail dot com>\r
2 using System;\r
3 using System.Collections;\r
4 using System.Collections.Generic;\r
5 \r
6 namespace HtmlAgilityPack\r
7 {\r
8     /// <summary>\r
9     /// Represents a combined list and collection of HTML nodes.\r
10     /// </summary>\r
11     public class HtmlAttributeCollection : IList<HtmlAttribute>\r
12     {\r
13         #region Fields\r
14 \r
15         internal Dictionary<string, HtmlAttribute> Hashitems = new Dictionary<string, HtmlAttribute>();\r
16         private HtmlNode _ownernode;\r
17         private List<HtmlAttribute> items = new List<HtmlAttribute>();\r
18 \r
19         #endregion\r
20 \r
21         #region Constructors\r
22 \r
23         internal HtmlAttributeCollection(HtmlNode ownernode)\r
24         {\r
25             _ownernode = ownernode;\r
26         }\r
27 \r
28         #endregion\r
29 \r
30         #region Properties\r
31 \r
32         /// <summary>\r
33         /// Gets a given attribute from the list using its name.\r
34         /// </summary>\r
35         public HtmlAttribute this[string name]\r
36         {\r
37             get\r
38             {\r
39                 if (name == null)\r
40                 {\r
41                     throw new ArgumentNullException("name");\r
42                 }\r
43                 return Hashitems.ContainsKey(name.ToLower()) ? Hashitems[name.ToLower()] : null;\r
44             }\r
45             set { Append(value); }\r
46         }\r
47 \r
48         #endregion\r
49 \r
50         #region IList<HtmlAttribute> Members\r
51 \r
52         /// <summary>\r
53         /// Gets the number of elements actually contained in the list.\r
54         /// </summary>\r
55         public int Count\r
56         {\r
57             get { return items.Count; }\r
58         }\r
59 \r
60         /// <summary>\r
61         /// Gets readonly status of colelction\r
62         /// </summary>\r
63         public bool IsReadOnly\r
64         {\r
65             get { return false; }\r
66         }\r
67 \r
68         /// <summary>\r
69         /// Gets the attribute at the specified index.\r
70         /// </summary>\r
71         public HtmlAttribute this[int index]\r
72         {\r
73             get { return items[index]; }\r
74             set { items[index] = value; }\r
75         }\r
76 \r
77         /// <summary>\r
78         /// Adds supplied item to collection\r
79         /// </summary>\r
80         /// <param name="item"></param>\r
81         public void Add(HtmlAttribute item)\r
82         {\r
83             Append(item);\r
84         }\r
85 \r
86         /// <summary>\r
87         /// Explicit clear\r
88         /// </summary>\r
89         void ICollection<HtmlAttribute>.Clear()\r
90         {\r
91             items.Clear();\r
92         }\r
93 \r
94         /// <summary>\r
95         /// Retreives existence of supplied item\r
96         /// </summary>\r
97         /// <param name="item"></param>\r
98         /// <returns></returns>\r
99         public bool Contains(HtmlAttribute item)\r
100         {\r
101             return items.Contains(item);\r
102         }\r
103 \r
104         /// <summary>\r
105         /// Copies collection to array\r
106         /// </summary>\r
107         /// <param name="array"></param>\r
108         /// <param name="arrayIndex"></param>\r
109         public void CopyTo(HtmlAttribute[] array, int arrayIndex)\r
110         {\r
111             items.CopyTo(array, arrayIndex);\r
112         }\r
113 \r
114         /// <summary>\r
115         /// Get Explicit enumerator\r
116         /// </summary>\r
117         /// <returns></returns>\r
118         IEnumerator<HtmlAttribute> IEnumerable<HtmlAttribute>.GetEnumerator()\r
119         {\r
120             return items.GetEnumerator();\r
121         }\r
122 \r
123         /// <summary>\r
124         /// Explicit non-generic enumerator\r
125         /// </summary>\r
126         /// <returns></returns>\r
127         IEnumerator IEnumerable.GetEnumerator()\r
128         {\r
129             return items.GetEnumerator();\r
130         }\r
131 \r
132         /// <summary>\r
133         /// Retrieves the index for the supplied item, -1 if not found\r
134         /// </summary>\r
135         /// <param name="item"></param>\r
136         /// <returns></returns>\r
137         public int IndexOf(HtmlAttribute item)\r
138         {\r
139             return items.IndexOf(item);\r
140         }\r
141 \r
142         /// <summary>\r
143         /// Inserts given item into collection at supplied index\r
144         /// </summary>\r
145         /// <param name="index"></param>\r
146         /// <param name="item"></param>\r
147         public void Insert(int index, HtmlAttribute item)\r
148         {\r
149             if (item == null)\r
150             {\r
151                 throw new ArgumentNullException("item");\r
152             }\r
153 \r
154             Hashitems[item.Name] = item;\r
155             item._ownernode = _ownernode;\r
156             items.Insert(index, item);\r
157 \r
158             _ownernode._innerchanged = true;\r
159             _ownernode._outerchanged = true;\r
160         }\r
161 \r
162         /// <summary>\r
163         /// Explicit collection remove\r
164         /// </summary>\r
165         /// <param name="item"></param>\r
166         /// <returns></returns>\r
167         bool ICollection<HtmlAttribute>.Remove(HtmlAttribute item)\r
168         {\r
169             return items.Remove(item);\r
170         }\r
171 \r
172         /// <summary>\r
173         /// Removes the attribute at the specified index.\r
174         /// </summary>\r
175         /// <param name="index">The index of the attribute to remove.</param>\r
176         public void RemoveAt(int index)\r
177         {\r
178             HtmlAttribute att = items[index];\r
179             Hashitems.Remove(att.Name);\r
180             items.RemoveAt(index);\r
181 \r
182             _ownernode._innerchanged = true;\r
183             _ownernode._outerchanged = true;\r
184         }\r
185 \r
186         #endregion\r
187 \r
188         #region Public Methods\r
189 \r
190         /// <summary>\r
191         /// Adds a new attribute to the collection with the given values\r
192         /// </summary>\r
193         /// <param name="name"></param>\r
194         /// <param name="value"></param>\r
195         public void Add(string name, string value)\r
196         {\r
197             Append(name, value);\r
198         }\r
199 \r
200         /// <summary>\r
201         /// Inserts the specified attribute as the last attribute in the collection.\r
202         /// </summary>\r
203         /// <param name="newAttribute">The attribute to insert. May not be null.</param>\r
204         /// <returns>The appended attribute.</returns>\r
205         public HtmlAttribute Append(HtmlAttribute newAttribute)\r
206         {\r
207             if (newAttribute == null)\r
208             {\r
209                 throw new ArgumentNullException("newAttribute");\r
210             }\r
211 \r
212             Hashitems[newAttribute.Name] = newAttribute;\r
213             newAttribute._ownernode = _ownernode;\r
214             items.Add(newAttribute);\r
215 \r
216             _ownernode._innerchanged = true;\r
217             _ownernode._outerchanged = true;\r
218             return newAttribute;\r
219         }\r
220 \r
221         /// <summary>\r
222         /// Creates and inserts a new attribute as the last attribute in the collection.\r
223         /// </summary>\r
224         /// <param name="name">The name of the attribute to insert.</param>\r
225         /// <returns>The appended attribute.</returns>\r
226         public HtmlAttribute Append(string name)\r
227         {\r
228             HtmlAttribute att = _ownernode._ownerdocument.CreateAttribute(name);\r
229             return Append(att);\r
230         }\r
231 \r
232         /// <summary>\r
233         /// Creates and inserts a new attribute as the last attribute in the collection.\r
234         /// </summary>\r
235         /// <param name="name">The name of the attribute to insert.</param>\r
236         /// <param name="value">The value of the attribute to insert.</param>\r
237         /// <returns>The appended attribute.</returns>\r
238         public HtmlAttribute Append(string name, string value)\r
239         {\r
240             HtmlAttribute att = _ownernode._ownerdocument.CreateAttribute(name, value);\r
241             return Append(att);\r
242         }\r
243 \r
244         /// <summary>\r
245         /// Checks for existance of attribute with given name\r
246         /// </summary>\r
247         /// <param name="name"></param>\r
248         /// <returns></returns>\r
249         public bool Contains(string name)\r
250         {\r
251             for (int i = 0; i < items.Count; i++)\r
252             {\r
253                 if (items[i].Name.Equals(name.ToLower()))\r
254                     return true;\r
255             }\r
256             return false;\r
257         }\r
258 \r
259         /// <summary>\r
260         /// Inserts the specified attribute as the first node in the collection.\r
261         /// </summary>\r
262         /// <param name="newAttribute">The attribute to insert. May not be null.</param>\r
263         /// <returns>The prepended attribute.</returns>\r
264         public HtmlAttribute Prepend(HtmlAttribute newAttribute)\r
265         {\r
266             Insert(0, newAttribute);\r
267             return newAttribute;\r
268         }\r
269 \r
270         /// <summary>\r
271         /// Removes a given attribute from the list.\r
272         /// </summary>\r
273         /// <param name="attribute">The attribute to remove. May not be null.</param>\r
274         public void Remove(HtmlAttribute attribute)\r
275         {\r
276             if (attribute == null)\r
277             {\r
278                 throw new ArgumentNullException("attribute");\r
279             }\r
280             int index = GetAttributeIndex(attribute);\r
281             if (index == -1)\r
282             {\r
283                 throw new IndexOutOfRangeException();\r
284             }\r
285             RemoveAt(index);\r
286         }\r
287 \r
288         /// <summary>\r
289         /// Removes an attribute from the list, using its name. If there are more than one attributes with this name, they will all be removed.\r
290         /// </summary>\r
291         /// <param name="name">The attribute's name. May not be null.</param>\r
292         public void Remove(string name)\r
293         {\r
294             if (name == null)\r
295             {\r
296                 throw new ArgumentNullException("name");\r
297             }\r
298 \r
299             string lname = name.ToLower();\r
300             for (int i = 0; i < items.Count; i++)\r
301             {\r
302                 HtmlAttribute att = items[i];\r
303                 if (att.Name == lname)\r
304                 {\r
305                     RemoveAt(i);\r
306                 }\r
307             }\r
308         }\r
309 \r
310         /// <summary>\r
311         /// Remove all attributes in the list.\r
312         /// </summary>\r
313         public void RemoveAll()\r
314         {\r
315             Hashitems.Clear();\r
316             items.Clear();\r
317 \r
318             _ownernode._innerchanged = true;\r
319             _ownernode._outerchanged = true;\r
320         }\r
321 \r
322         #endregion\r
323 \r
324         #region LINQ Methods\r
325 \r
326         /// <summary>\r
327         /// Returns all attributes with specified name. Handles case insentivity\r
328         /// </summary>\r
329         /// <param name="attributeName">Name of the attribute</param>\r
330         /// <returns></returns>\r
331         public IEnumerable<HtmlAttribute> AttributesWithName(string attributeName)\r
332         {\r
333             attributeName = attributeName.ToLower();\r
334             for (int i = 0; i < items.Count; i++)\r
335             {\r
336                 if (items[i].Name.Equals(attributeName))\r
337                     yield return items[i];\r
338             }\r
339         }\r
340 \r
341         /// <summary>\r
342         /// Removes all attributes from the collection\r
343         /// </summary>\r
344         public void Remove()\r
345         {\r
346             foreach (HtmlAttribute item in items)\r
347                 item.Remove();\r
348         }\r
349 \r
350         #endregion\r
351 \r
352         #region Internal Methods\r
353 \r
354         /// <summary>\r
355         /// Clears the attribute collection\r
356         /// </summary>\r
357         internal void Clear()\r
358         {\r
359             Hashitems.Clear();\r
360             items.Clear();\r
361         }\r
362 \r
363         internal int GetAttributeIndex(HtmlAttribute attribute)\r
364         {\r
365             if (attribute == null)\r
366             {\r
367                 throw new ArgumentNullException("attribute");\r
368             }\r
369             for (int i = 0; i < items.Count; i++)\r
370             {\r
371                 if ((items[i]) == attribute)\r
372                     return i;\r
373             }\r
374             return -1;\r
375         }\r
376 \r
377         internal int GetAttributeIndex(string name)\r
378         {\r
379             if (name == null)\r
380             {\r
381                 throw new ArgumentNullException("name");\r
382             }\r
383             string lname = name.ToLower();\r
384             for (int i = 0; i < items.Count; i++)\r
385             {\r
386                 if ((items[i]).Name == lname)\r
387                     return i;\r
388             }\r
389             return -1;\r
390         }\r
391 \r
392         #endregion\r
393     }\r
394 }