1 // HtmlAgilityPack V1.0 - Simon Mourier <simon underscore mourier at hotmail dot com>
\r
3 using System.Collections;
\r
4 using System.Collections.Generic;
\r
6 namespace HtmlAgilityPack
\r
9 /// Represents a combined list and collection of HTML nodes.
\r
11 public class HtmlAttributeCollection : IList<HtmlAttribute>
\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
21 #region Constructors
\r
23 internal HtmlAttributeCollection(HtmlNode ownernode)
\r
25 _ownernode = ownernode;
\r
33 /// Gets a given attribute from the list using its name.
\r
35 public HtmlAttribute this[string name]
\r
41 throw new ArgumentNullException("name");
\r
43 return Hashitems.ContainsKey(name.ToLower()) ? Hashitems[name.ToLower()] : null;
\r
45 set { Append(value); }
\r
50 #region IList<HtmlAttribute> Members
\r
53 /// Gets the number of elements actually contained in the list.
\r
57 get { return items.Count; }
\r
61 /// Gets readonly status of colelction
\r
63 public bool IsReadOnly
\r
65 get { return false; }
\r
69 /// Gets the attribute at the specified index.
\r
71 public HtmlAttribute this[int index]
\r
73 get { return items[index]; }
\r
74 set { items[index] = value; }
\r
78 /// Adds supplied item to collection
\r
80 /// <param name="item"></param>
\r
81 public void Add(HtmlAttribute item)
\r
89 void ICollection<HtmlAttribute>.Clear()
\r
95 /// Retreives existence of supplied item
\r
97 /// <param name="item"></param>
\r
98 /// <returns></returns>
\r
99 public bool Contains(HtmlAttribute item)
\r
101 return items.Contains(item);
\r
105 /// Copies collection to array
\r
107 /// <param name="array"></param>
\r
108 /// <param name="arrayIndex"></param>
\r
109 public void CopyTo(HtmlAttribute[] array, int arrayIndex)
\r
111 items.CopyTo(array, arrayIndex);
\r
115 /// Get Explicit enumerator
\r
117 /// <returns></returns>
\r
118 IEnumerator<HtmlAttribute> IEnumerable<HtmlAttribute>.GetEnumerator()
\r
120 return items.GetEnumerator();
\r
124 /// Explicit non-generic enumerator
\r
126 /// <returns></returns>
\r
127 IEnumerator IEnumerable.GetEnumerator()
\r
129 return items.GetEnumerator();
\r
133 /// Retrieves the index for the supplied item, -1 if not found
\r
135 /// <param name="item"></param>
\r
136 /// <returns></returns>
\r
137 public int IndexOf(HtmlAttribute item)
\r
139 return items.IndexOf(item);
\r
143 /// Inserts given item into collection at supplied index
\r
145 /// <param name="index"></param>
\r
146 /// <param name="item"></param>
\r
147 public void Insert(int index, HtmlAttribute item)
\r
151 throw new ArgumentNullException("item");
\r
154 Hashitems[item.Name] = item;
\r
155 item._ownernode = _ownernode;
\r
156 items.Insert(index, item);
\r
158 _ownernode._innerchanged = true;
\r
159 _ownernode._outerchanged = true;
\r
163 /// Explicit collection remove
\r
165 /// <param name="item"></param>
\r
166 /// <returns></returns>
\r
167 bool ICollection<HtmlAttribute>.Remove(HtmlAttribute item)
\r
169 return items.Remove(item);
\r
173 /// Removes the attribute at the specified index.
\r
175 /// <param name="index">The index of the attribute to remove.</param>
\r
176 public void RemoveAt(int index)
\r
178 HtmlAttribute att = items[index];
\r
179 Hashitems.Remove(att.Name);
\r
180 items.RemoveAt(index);
\r
182 _ownernode._innerchanged = true;
\r
183 _ownernode._outerchanged = true;
\r
188 #region Public Methods
\r
191 /// Adds a new attribute to the collection with the given values
\r
193 /// <param name="name"></param>
\r
194 /// <param name="value"></param>
\r
195 public void Add(string name, string value)
\r
197 Append(name, value);
\r
201 /// Inserts the specified attribute as the last attribute in the collection.
\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
207 if (newAttribute == null)
\r
209 throw new ArgumentNullException("newAttribute");
\r
212 Hashitems[newAttribute.Name] = newAttribute;
\r
213 newAttribute._ownernode = _ownernode;
\r
214 items.Add(newAttribute);
\r
216 _ownernode._innerchanged = true;
\r
217 _ownernode._outerchanged = true;
\r
218 return newAttribute;
\r
222 /// Creates and inserts a new attribute as the last attribute in the collection.
\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
228 HtmlAttribute att = _ownernode._ownerdocument.CreateAttribute(name);
\r
229 return Append(att);
\r
233 /// Creates and inserts a new attribute as the last attribute in the collection.
\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
240 HtmlAttribute att = _ownernode._ownerdocument.CreateAttribute(name, value);
\r
241 return Append(att);
\r
245 /// Checks for existance of attribute with given name
\r
247 /// <param name="name"></param>
\r
248 /// <returns></returns>
\r
249 public bool Contains(string name)
\r
251 for (int i = 0; i < items.Count; i++)
\r
253 if (items[i].Name.Equals(name.ToLower()))
\r
260 /// Inserts the specified attribute as the first node in the collection.
\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
266 Insert(0, newAttribute);
\r
267 return newAttribute;
\r
271 /// Removes a given attribute from the list.
\r
273 /// <param name="attribute">The attribute to remove. May not be null.</param>
\r
274 public void Remove(HtmlAttribute attribute)
\r
276 if (attribute == null)
\r
278 throw new ArgumentNullException("attribute");
\r
280 int index = GetAttributeIndex(attribute);
\r
283 throw new IndexOutOfRangeException();
\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
291 /// <param name="name">The attribute's name. May not be null.</param>
\r
292 public void Remove(string name)
\r
296 throw new ArgumentNullException("name");
\r
299 string lname = name.ToLower();
\r
300 for (int i = 0; i < items.Count; i++)
\r
302 HtmlAttribute att = items[i];
\r
303 if (att.Name == lname)
\r
311 /// Remove all attributes in the list.
\r
313 public void RemoveAll()
\r
318 _ownernode._innerchanged = true;
\r
319 _ownernode._outerchanged = true;
\r
324 #region LINQ Methods
\r
327 /// Returns all attributes with specified name. Handles case insentivity
\r
329 /// <param name="attributeName">Name of the attribute</param>
\r
330 /// <returns></returns>
\r
331 public IEnumerable<HtmlAttribute> AttributesWithName(string attributeName)
\r
333 attributeName = attributeName.ToLower();
\r
334 for (int i = 0; i < items.Count; i++)
\r
336 if (items[i].Name.Equals(attributeName))
\r
337 yield return items[i];
\r
342 /// Removes all attributes from the collection
\r
344 public void Remove()
\r
346 foreach (HtmlAttribute item in items)
\r
352 #region Internal Methods
\r
355 /// Clears the attribute collection
\r
357 internal void Clear()
\r
363 internal int GetAttributeIndex(HtmlAttribute attribute)
\r
365 if (attribute == null)
\r
367 throw new ArgumentNullException("attribute");
\r
369 for (int i = 0; i < items.Count; i++)
\r
371 if ((items[i]) == attribute)
\r
377 internal int GetAttributeIndex(string name)
\r
381 throw new ArgumentNullException("name");
\r
383 string lname = name.ToLower();
\r
384 for (int i = 0; i < items.Count; i++)
\r
386 if ((items[i]).Name == lname)
\r