[Mono.Security]: Add the new certificate store.
[mono-project.git] / docs / HtmlAgilityPack / HtmlAttributeCollection.cs
blob07af9979ddb025593ac824e639070dcd70ee4331
1 // HtmlAgilityPack V1.0 - Simon Mourier <simon underscore mourier at hotmail dot com>
2 using System;
3 using System.Collections;
4 using System.Collections.Generic;
6 namespace HtmlAgilityPack
8 /// <summary>
9 /// Represents a combined list and collection of HTML nodes.
10 /// </summary>
11 public class HtmlAttributeCollection : IList<HtmlAttribute>
13 #region Fields
15 internal Dictionary<string, HtmlAttribute> Hashitems = new Dictionary<string, HtmlAttribute>();
16 private HtmlNode _ownernode;
17 private List<HtmlAttribute> items = new List<HtmlAttribute>();
19 #endregion
21 #region Constructors
23 internal HtmlAttributeCollection(HtmlNode ownernode)
25 _ownernode = ownernode;
28 #endregion
30 #region Properties
32 /// <summary>
33 /// Gets a given attribute from the list using its name.
34 /// </summary>
35 public HtmlAttribute this[string name]
37 get
39 if (name == null)
41 throw new ArgumentNullException("name");
43 return Hashitems.ContainsKey(name.ToLower()) ? Hashitems[name.ToLower()] : null;
45 set { Append(value); }
48 #endregion
50 #region IList<HtmlAttribute> Members
52 /// <summary>
53 /// Gets the number of elements actually contained in the list.
54 /// </summary>
55 public int Count
57 get { return items.Count; }
60 /// <summary>
61 /// Gets readonly status of colelction
62 /// </summary>
63 public bool IsReadOnly
65 get { return false; }
68 /// <summary>
69 /// Gets the attribute at the specified index.
70 /// </summary>
71 public HtmlAttribute this[int index]
73 get { return items[index]; }
74 set { items[index] = value; }
77 /// <summary>
78 /// Adds supplied item to collection
79 /// </summary>
80 /// <param name="item"></param>
81 public void Add(HtmlAttribute item)
83 Append(item);
86 /// <summary>
87 /// Explicit clear
88 /// </summary>
89 void ICollection<HtmlAttribute>.Clear()
91 items.Clear();
94 /// <summary>
95 /// Retreives existence of supplied item
96 /// </summary>
97 /// <param name="item"></param>
98 /// <returns></returns>
99 public bool Contains(HtmlAttribute item)
101 return items.Contains(item);
104 /// <summary>
105 /// Copies collection to array
106 /// </summary>
107 /// <param name="array"></param>
108 /// <param name="arrayIndex"></param>
109 public void CopyTo(HtmlAttribute[] array, int arrayIndex)
111 items.CopyTo(array, arrayIndex);
114 /// <summary>
115 /// Get Explicit enumerator
116 /// </summary>
117 /// <returns></returns>
118 IEnumerator<HtmlAttribute> IEnumerable<HtmlAttribute>.GetEnumerator()
120 return items.GetEnumerator();
123 /// <summary>
124 /// Explicit non-generic enumerator
125 /// </summary>
126 /// <returns></returns>
127 IEnumerator IEnumerable.GetEnumerator()
129 return items.GetEnumerator();
132 /// <summary>
133 /// Retrieves the index for the supplied item, -1 if not found
134 /// </summary>
135 /// <param name="item"></param>
136 /// <returns></returns>
137 public int IndexOf(HtmlAttribute item)
139 return items.IndexOf(item);
142 /// <summary>
143 /// Inserts given item into collection at supplied index
144 /// </summary>
145 /// <param name="index"></param>
146 /// <param name="item"></param>
147 public void Insert(int index, HtmlAttribute item)
149 if (item == null)
151 throw new ArgumentNullException("item");
154 Hashitems[item.Name] = item;
155 item._ownernode = _ownernode;
156 items.Insert(index, item);
158 _ownernode._innerchanged = true;
159 _ownernode._outerchanged = true;
162 /// <summary>
163 /// Explicit collection remove
164 /// </summary>
165 /// <param name="item"></param>
166 /// <returns></returns>
167 bool ICollection<HtmlAttribute>.Remove(HtmlAttribute item)
169 return items.Remove(item);
172 /// <summary>
173 /// Removes the attribute at the specified index.
174 /// </summary>
175 /// <param name="index">The index of the attribute to remove.</param>
176 public void RemoveAt(int index)
178 HtmlAttribute att = items[index];
179 Hashitems.Remove(att.Name);
180 items.RemoveAt(index);
182 _ownernode._innerchanged = true;
183 _ownernode._outerchanged = true;
186 #endregion
188 #region Public Methods
190 /// <summary>
191 /// Adds a new attribute to the collection with the given values
192 /// </summary>
193 /// <param name="name"></param>
194 /// <param name="value"></param>
195 public void Add(string name, string value)
197 Append(name, value);
200 /// <summary>
201 /// Inserts the specified attribute as the last attribute in the collection.
202 /// </summary>
203 /// <param name="newAttribute">The attribute to insert. May not be null.</param>
204 /// <returns>The appended attribute.</returns>
205 public HtmlAttribute Append(HtmlAttribute newAttribute)
207 if (newAttribute == null)
209 throw new ArgumentNullException("newAttribute");
212 Hashitems[newAttribute.Name] = newAttribute;
213 newAttribute._ownernode = _ownernode;
214 items.Add(newAttribute);
216 _ownernode._innerchanged = true;
217 _ownernode._outerchanged = true;
218 return newAttribute;
221 /// <summary>
222 /// Creates and inserts a new attribute as the last attribute in the collection.
223 /// </summary>
224 /// <param name="name">The name of the attribute to insert.</param>
225 /// <returns>The appended attribute.</returns>
226 public HtmlAttribute Append(string name)
228 HtmlAttribute att = _ownernode._ownerdocument.CreateAttribute(name);
229 return Append(att);
232 /// <summary>
233 /// Creates and inserts a new attribute as the last attribute in the collection.
234 /// </summary>
235 /// <param name="name">The name of the attribute to insert.</param>
236 /// <param name="value">The value of the attribute to insert.</param>
237 /// <returns>The appended attribute.</returns>
238 public HtmlAttribute Append(string name, string value)
240 HtmlAttribute att = _ownernode._ownerdocument.CreateAttribute(name, value);
241 return Append(att);
244 /// <summary>
245 /// Checks for existance of attribute with given name
246 /// </summary>
247 /// <param name="name"></param>
248 /// <returns></returns>
249 public bool Contains(string name)
251 for (int i = 0; i < items.Count; i++)
253 if (items[i].Name.Equals(name.ToLower()))
254 return true;
256 return false;
259 /// <summary>
260 /// Inserts the specified attribute as the first node in the collection.
261 /// </summary>
262 /// <param name="newAttribute">The attribute to insert. May not be null.</param>
263 /// <returns>The prepended attribute.</returns>
264 public HtmlAttribute Prepend(HtmlAttribute newAttribute)
266 Insert(0, newAttribute);
267 return newAttribute;
270 /// <summary>
271 /// Removes a given attribute from the list.
272 /// </summary>
273 /// <param name="attribute">The attribute to remove. May not be null.</param>
274 public void Remove(HtmlAttribute attribute)
276 if (attribute == null)
278 throw new ArgumentNullException("attribute");
280 int index = GetAttributeIndex(attribute);
281 if (index == -1)
283 throw new IndexOutOfRangeException();
285 RemoveAt(index);
288 /// <summary>
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.
290 /// </summary>
291 /// <param name="name">The attribute's name. May not be null.</param>
292 public void Remove(string name)
294 if (name == null)
296 throw new ArgumentNullException("name");
299 string lname = name.ToLower();
300 for (int i = 0; i < items.Count; i++)
302 HtmlAttribute att = items[i];
303 if (att.Name == lname)
305 RemoveAt(i);
310 /// <summary>
311 /// Remove all attributes in the list.
312 /// </summary>
313 public void RemoveAll()
315 Hashitems.Clear();
316 items.Clear();
318 _ownernode._innerchanged = true;
319 _ownernode._outerchanged = true;
322 #endregion
324 #region LINQ Methods
326 /// <summary>
327 /// Returns all attributes with specified name. Handles case insentivity
328 /// </summary>
329 /// <param name="attributeName">Name of the attribute</param>
330 /// <returns></returns>
331 public IEnumerable<HtmlAttribute> AttributesWithName(string attributeName)
333 attributeName = attributeName.ToLower();
334 for (int i = 0; i < items.Count; i++)
336 if (items[i].Name.Equals(attributeName))
337 yield return items[i];
341 /// <summary>
342 /// Removes all attributes from the collection
343 /// </summary>
344 public void Remove()
346 foreach (HtmlAttribute item in items)
347 item.Remove();
350 #endregion
352 #region Internal Methods
354 /// <summary>
355 /// Clears the attribute collection
356 /// </summary>
357 internal void Clear()
359 Hashitems.Clear();
360 items.Clear();
363 internal int GetAttributeIndex(HtmlAttribute attribute)
365 if (attribute == null)
367 throw new ArgumentNullException("attribute");
369 for (int i = 0; i < items.Count; i++)
371 if ((items[i]) == attribute)
372 return i;
374 return -1;
377 internal int GetAttributeIndex(string name)
379 if (name == null)
381 throw new ArgumentNullException("name");
383 string lname = name.ToLower();
384 for (int i = 0; i < items.Count; i++)
386 if ((items[i]).Name == lname)
387 return i;
389 return -1;
392 #endregion