1 // HtmlAgilityPack V1.0 - Simon Mourier <simon underscore mourier at hotmail dot com>
3 using System
.Collections
;
4 using System
.Collections
.Generic
;
6 namespace HtmlAgilityPack
9 /// Represents a combined list and collection of HTML nodes.
11 public class HtmlAttributeCollection
: IList
<HtmlAttribute
>
15 internal Dictionary
<string, HtmlAttribute
> Hashitems
= new Dictionary
<string, HtmlAttribute
>();
16 private HtmlNode _ownernode
;
17 private List
<HtmlAttribute
> items
= new List
<HtmlAttribute
>();
23 internal HtmlAttributeCollection(HtmlNode ownernode
)
25 _ownernode
= ownernode
;
33 /// Gets a given attribute from the list using its name.
35 public HtmlAttribute
this[string name
]
41 throw new ArgumentNullException("name");
43 return Hashitems
.ContainsKey(name
.ToLower()) ? Hashitems
[name
.ToLower()] : null;
45 set { Append(value); }
50 #region IList<HtmlAttribute> Members
53 /// Gets the number of elements actually contained in the list.
57 get { return items.Count; }
61 /// Gets readonly status of colelction
63 public bool IsReadOnly
69 /// Gets the attribute at the specified index.
71 public HtmlAttribute
this[int index
]
73 get { return items[index]; }
74 set { items[index] = value; }
78 /// Adds supplied item to collection
80 /// <param name="item"></param>
81 public void Add(HtmlAttribute item
)
89 void ICollection
<HtmlAttribute
>.Clear()
95 /// Retreives existence of supplied item
97 /// <param name="item"></param>
98 /// <returns></returns>
99 public bool Contains(HtmlAttribute item
)
101 return items
.Contains(item
);
105 /// Copies collection to array
107 /// <param name="array"></param>
108 /// <param name="arrayIndex"></param>
109 public void CopyTo(HtmlAttribute
[] array
, int arrayIndex
)
111 items
.CopyTo(array
, arrayIndex
);
115 /// Get Explicit enumerator
117 /// <returns></returns>
118 IEnumerator
<HtmlAttribute
> IEnumerable
<HtmlAttribute
>.GetEnumerator()
120 return items
.GetEnumerator();
124 /// Explicit non-generic enumerator
126 /// <returns></returns>
127 IEnumerator IEnumerable
.GetEnumerator()
129 return items
.GetEnumerator();
133 /// Retrieves the index for the supplied item, -1 if not found
135 /// <param name="item"></param>
136 /// <returns></returns>
137 public int IndexOf(HtmlAttribute item
)
139 return items
.IndexOf(item
);
143 /// Inserts given item into collection at supplied index
145 /// <param name="index"></param>
146 /// <param name="item"></param>
147 public void Insert(int index
, HtmlAttribute item
)
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;
163 /// Explicit collection remove
165 /// <param name="item"></param>
166 /// <returns></returns>
167 bool ICollection
<HtmlAttribute
>.Remove(HtmlAttribute item
)
169 return items
.Remove(item
);
173 /// Removes the attribute at the specified index.
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;
188 #region Public Methods
191 /// Adds a new attribute to the collection with the given values
193 /// <param name="name"></param>
194 /// <param name="value"></param>
195 public void Add(string name
, string value)
201 /// Inserts the specified attribute as the last attribute in the collection.
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;
222 /// Creates and inserts a new attribute as the last attribute in the collection.
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
);
233 /// Creates and inserts a new attribute as the last attribute in the collection.
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);
245 /// Checks for existance of attribute with given name
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()))
260 /// Inserts the specified attribute as the first node in the collection.
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
);
271 /// Removes a given attribute from the list.
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
);
283 throw new IndexOutOfRangeException();
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.
291 /// <param name="name">The attribute's name. May not be null.</param>
292 public void Remove(string name
)
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
)
311 /// Remove all attributes in the list.
313 public void RemoveAll()
318 _ownernode
._innerchanged
= true;
319 _ownernode
._outerchanged
= true;
327 /// Returns all attributes with specified name. Handles case insentivity
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
];
342 /// Removes all attributes from the collection
346 foreach (HtmlAttribute item
in items
)
352 #region Internal Methods
355 /// Clears the attribute collection
357 internal void 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
)
377 internal int GetAttributeIndex(string name
)
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
)