| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468 |
- // HtmlAgilityPack V1.0 - Simon Mourier <[email protected]>
- using System;
- using System.Collections;
- namespace HtmlAgilityPack
- {
- /// <summary>
- /// Represents an HTML attribute.
- /// </summary>
- public class HtmlAttribute: IComparable
- {
- internal int _line = 0;
- internal int _lineposition = 0;
- internal int _streamposition = 0;
- internal int _namestartindex = 0;
- internal int _namelength = 0;
- internal int _valuestartindex = 0;
- internal int _valuelength = 0;
- internal HtmlDocument _ownerdocument; // attribute can exists without a node
- internal HtmlNode _ownernode;
- internal string _name;
- internal string _value;
- internal HtmlAttribute(HtmlDocument ownerdocument)
- {
- _ownerdocument = ownerdocument;
- }
- /// <summary>
- /// Creates a duplicate of this attribute.
- /// </summary>
- /// <returns>The cloned attribute.</returns>
- public HtmlAttribute Clone()
- {
- HtmlAttribute att = new HtmlAttribute(_ownerdocument);
- att.Name = Name;
- att.Value = Value;
- return att;
- }
- /// <summary>
- /// Compares the current instance with another attribute. Comparison is based on attributes' name.
- /// </summary>
- /// <param name="obj">An attribute to compare with this instance.</param>
- /// <returns>A 32-bit signed integer that indicates the relative order of the names comparison.</returns>
- public int CompareTo(object obj)
- {
- HtmlAttribute att = obj as HtmlAttribute;
- if (att == null)
- {
- throw new ArgumentException("obj");
- }
- return Name.CompareTo(att.Name);
- }
- internal string XmlName
- {
- get
- {
- return HtmlDocument.GetXmlName(Name);
- }
- }
- internal string XmlValue
- {
- get
- {
- return Value;
- }
- }
- /// <summary>
- /// Gets the qualified name of the attribute.
- /// </summary>
- public string Name
- {
- get
- {
- if (_name == null)
- {
- _name = _ownerdocument._text.Substring(_namestartindex, _namelength).ToLower();
- }
- return _name;
- }
- set
- {
- if (value == null)
- {
- throw new ArgumentNullException("value");
- }
- _name = value.ToLower();
- if (_ownernode != null)
- {
- _ownernode._innerchanged = true;
- _ownernode._outerchanged = true;
- }
- }
- }
- /// <summary>
- /// Gets or sets the value of the attribute.
- /// </summary>
- public string Value
- {
- get
- {
- if (_value == null)
- {
- _value = _ownerdocument._text.Substring(_valuestartindex, _valuelength);
- }
- return _value;
- }
- set
- {
- _value = value;
- if (_ownernode != null)
- {
- _ownernode._innerchanged = true;
- _ownernode._outerchanged = true;
- }
- }
- }
- /// <summary>
- /// Gets the line number of this attribute in the document.
- /// </summary>
- public int Line
- {
- get
- {
- return _line;
- }
- }
- /// <summary>
- /// Gets the column number of this attribute in the document.
- /// </summary>
- public int LinePosition
- {
- get
- {
- return _lineposition;
- }
- }
- /// <summary>
- /// Gets the stream position of this attribute in the document, relative to the start of the document.
- /// </summary>
- public int StreamPosition
- {
- get
- {
- return _streamposition;
- }
- }
- /// <summary>
- /// Gets the HTML node to which this attribute belongs.
- /// </summary>
- public HtmlNode OwnerNode
- {
- get
- {
- return _ownernode;
- }
- }
- /// <summary>
- /// Gets the HTML document to which this attribute belongs.
- /// </summary>
- public HtmlDocument OwnerDocument
- {
- get
- {
- return _ownerdocument;
- }
- }
- }
- /// <summary>
- /// Represents a combined list and collection of HTML nodes.
- /// </summary>
- public class HtmlAttributeCollection: IEnumerable
- {
- internal Hashtable _hashitems = new Hashtable();
- private ArrayList _items = new ArrayList();
- private HtmlNode _ownernode;
- internal HtmlAttributeCollection(HtmlNode ownernode)
- {
- _ownernode = ownernode;
- }
- /// <summary>
- /// Inserts the specified attribute as the last attribute in the collection.
- /// </summary>
- /// <param name="newAttribute">The attribute to insert. May not be null.</param>
- /// <returns>The appended attribute.</returns>
- public HtmlAttribute Append(HtmlAttribute newAttribute)
- {
- if (newAttribute == null)
- {
- throw new ArgumentNullException("newAttribute");
- }
- _hashitems[newAttribute.Name] = newAttribute;
- newAttribute._ownernode = _ownernode;
- _items.Add(newAttribute);
- _ownernode._innerchanged = true;
- _ownernode._outerchanged = true;
- return newAttribute;
- }
- /// <summary>
- /// Creates and inserts a new attribute as the last attribute in the collection.
- /// </summary>
- /// <param name="name">The name of the attribute to insert.</param>
- /// <returns>The appended attribute.</returns>
- public HtmlAttribute Append(string name)
- {
- HtmlAttribute att = _ownernode._ownerdocument.CreateAttribute(name);
- return Append(att);
- }
- /// <summary>
- /// Creates and inserts a new attribute as the last attribute in the collection.
- /// </summary>
- /// <param name="name">The name of the attribute to insert.</param>
- /// <param name="value">The value of the attribute to insert.</param>
- /// <returns>The appended attribute.</returns>
- public HtmlAttribute Append(string name, string value)
- {
- HtmlAttribute att = _ownernode._ownerdocument.CreateAttribute(name, value);
- return Append(att);
- }
- /// <summary>
- /// Inserts the specified attribute as the first node in the collection.
- /// </summary>
- /// <param name="newAttribute">The attribute to insert. May not be null.</param>
- /// <returns>The prepended attribute.</returns>
- public HtmlAttribute Prepend(HtmlAttribute newAttribute)
- {
- if (newAttribute == null)
- {
- throw new ArgumentNullException("newAttribute");
- }
- _hashitems[newAttribute.Name] = newAttribute;
- newAttribute._ownernode = _ownernode;
- _items.Insert(0, newAttribute);
- _ownernode._innerchanged = true;
- _ownernode._outerchanged = true;
- return newAttribute;
- }
- /// <summary>
- /// Removes the attribute at the specified index.
- /// </summary>
- /// <param name="index">The index of the attribute to remove.</param>
- public void RemoveAt(int index)
- {
- HtmlAttribute att = (HtmlAttribute)_items[index];
- _hashitems.Remove(att.Name);
- _items.RemoveAt(index);
- _ownernode._innerchanged = true;
- _ownernode._outerchanged = true;
- }
- /// <summary>
- /// Removes a given attribute from the list.
- /// </summary>
- /// <param name="attribute">The attribute to remove. May not be null.</param>
- public void Remove(HtmlAttribute attribute)
- {
- if (attribute == null)
- {
- throw new ArgumentNullException("attribute");
- }
- int index = GetAttributeIndex(attribute);
- if (index == -1)
- {
- throw new IndexOutOfRangeException();
- }
- RemoveAt(index);
- }
- /// <summary>
- /// Removes an attribute from the list, using its name. If there are more than one attributes with this name, they will all be removed.
- /// </summary>
- /// <param name="name">The attribute's name. May not be null.</param>
- public void Remove(string name)
- {
- if (name == null)
- {
- throw new ArgumentNullException("name");
- }
- string lname = name.ToLower();
- for(int i=0;i<_items.Count;i++)
- {
- HtmlAttribute att = (HtmlAttribute)_items[i];
- if (att.Name == lname)
- {
- RemoveAt(i);
- }
- }
- }
- /// <summary>
- /// Remove all attributes in the list.
- /// </summary>
- public void RemoveAll()
- {
- _hashitems.Clear();
- _items.Clear();
- _ownernode._innerchanged = true;
- _ownernode._outerchanged = true;
- }
- /// <summary>
- /// Gets the number of elements actually contained in the list.
- /// </summary>
- public int Count
- {
- get
- {
- return _items.Count;
- }
- }
- internal int GetAttributeIndex(HtmlAttribute attribute)
- {
- if (attribute == null)
- {
- throw new ArgumentNullException("attribute");
- }
- for(int i=0;i<_items.Count;i++)
- {
- if (((HtmlAttribute)_items[i])==attribute)
- return i;
- }
- return -1;
- }
- internal int GetAttributeIndex(string name)
- {
- if (name == null)
- {
- throw new ArgumentNullException("name");
- }
- string lname = name.ToLower();
- for(int i=0;i<_items.Count;i++)
- {
- if (((HtmlAttribute)_items[i]).Name==lname)
- return i;
- }
- return -1;
- }
- /// <summary>
- /// Gets a given attribute from the list using its name.
- /// </summary>
- public HtmlAttribute this[string name]
- {
- get
- {
- if (name == null)
- {
- throw new ArgumentNullException("name");
- }
- return _hashitems[name.ToLower()] as HtmlAttribute;
- }
- }
- /// <summary>
- /// Gets the attribute at the specified index.
- /// </summary>
- public HtmlAttribute this[int index]
- {
- get
- {
- return _items[index] as HtmlAttribute;
- }
- }
- internal void Clear()
- {
- _hashitems.Clear();
- _items.Clear();
- }
- /// <summary>
- /// Returns an enumerator that can iterate through the list.
- /// </summary>
- /// <returns>An IEnumerator for the entire list.</returns>
- public HtmlAttributeEnumerator GetEnumerator()
- {
- return new HtmlAttributeEnumerator(_items);
- }
- IEnumerator IEnumerable.GetEnumerator()
- {
- return GetEnumerator();
- }
- /// <summary>
- /// Represents an enumerator that can iterate through the list.
- /// </summary>
- public class HtmlAttributeEnumerator: IEnumerator
- {
- int _index;
- ArrayList _items;
- internal HtmlAttributeEnumerator(ArrayList items)
- {
- _items = items;
- _index = -1;
- }
- /// <summary>
- /// Sets the enumerator to its initial position, which is before the first element in the collection.
- /// </summary>
- public void Reset()
- {
- _index = -1;
- }
- /// <summary>
- /// Advances the enumerator to the next element of the collection.
- /// </summary>
- /// <returns>true if the enumerator was successfully advanced to the next element, false if the enumerator has passed the end of the collection.</returns>
- public bool MoveNext()
- {
- _index++;
- return (_index<_items.Count);
- }
- /// <summary>
- /// Gets the current element in the collection.
- /// </summary>
- public HtmlAttribute Current
- {
- get
- {
- return (HtmlAttribute)(_items[_index]);
- }
- }
- /// <summary>
- /// Gets the current element in the collection.
- /// </summary>
- object IEnumerator.Current
- {
- get
- {
- return (Current);
- }
- }
- }
- }
- }
|