HtmlAttribute.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. // HtmlAgilityPack V1.0 - Simon Mourier <[email protected]>
  2. using System;
  3. using System.Collections;
  4. namespace HtmlAgilityPack
  5. {
  6. /// <summary>
  7. /// Represents an HTML attribute.
  8. /// </summary>
  9. public class HtmlAttribute: IComparable
  10. {
  11. internal int _line = 0;
  12. internal int _lineposition = 0;
  13. internal int _streamposition = 0;
  14. internal int _namestartindex = 0;
  15. internal int _namelength = 0;
  16. internal int _valuestartindex = 0;
  17. internal int _valuelength = 0;
  18. internal HtmlDocument _ownerdocument; // attribute can exists without a node
  19. internal HtmlNode _ownernode;
  20. internal string _name;
  21. internal string _value;
  22. internal HtmlAttribute(HtmlDocument ownerdocument)
  23. {
  24. _ownerdocument = ownerdocument;
  25. }
  26. /// <summary>
  27. /// Creates a duplicate of this attribute.
  28. /// </summary>
  29. /// <returns>The cloned attribute.</returns>
  30. public HtmlAttribute Clone()
  31. {
  32. HtmlAttribute att = new HtmlAttribute(_ownerdocument);
  33. att.Name = Name;
  34. att.Value = Value;
  35. return att;
  36. }
  37. /// <summary>
  38. /// Compares the current instance with another attribute. Comparison is based on attributes' name.
  39. /// </summary>
  40. /// <param name="obj">An attribute to compare with this instance.</param>
  41. /// <returns>A 32-bit signed integer that indicates the relative order of the names comparison.</returns>
  42. public int CompareTo(object obj)
  43. {
  44. HtmlAttribute att = obj as HtmlAttribute;
  45. if (att == null)
  46. {
  47. throw new ArgumentException("obj");
  48. }
  49. return Name.CompareTo(att.Name);
  50. }
  51. internal string XmlName
  52. {
  53. get
  54. {
  55. return HtmlDocument.GetXmlName(Name);
  56. }
  57. }
  58. internal string XmlValue
  59. {
  60. get
  61. {
  62. return Value;
  63. }
  64. }
  65. /// <summary>
  66. /// Gets the qualified name of the attribute.
  67. /// </summary>
  68. public string Name
  69. {
  70. get
  71. {
  72. if (_name == null)
  73. {
  74. _name = _ownerdocument._text.Substring(_namestartindex, _namelength).ToLower();
  75. }
  76. return _name;
  77. }
  78. set
  79. {
  80. if (value == null)
  81. {
  82. throw new ArgumentNullException("value");
  83. }
  84. _name = value.ToLower();
  85. if (_ownernode != null)
  86. {
  87. _ownernode._innerchanged = true;
  88. _ownernode._outerchanged = true;
  89. }
  90. }
  91. }
  92. /// <summary>
  93. /// Gets or sets the value of the attribute.
  94. /// </summary>
  95. public string Value
  96. {
  97. get
  98. {
  99. if (_value == null)
  100. {
  101. _value = _ownerdocument._text.Substring(_valuestartindex, _valuelength);
  102. }
  103. return _value;
  104. }
  105. set
  106. {
  107. _value = value;
  108. if (_ownernode != null)
  109. {
  110. _ownernode._innerchanged = true;
  111. _ownernode._outerchanged = true;
  112. }
  113. }
  114. }
  115. /// <summary>
  116. /// Gets the line number of this attribute in the document.
  117. /// </summary>
  118. public int Line
  119. {
  120. get
  121. {
  122. return _line;
  123. }
  124. }
  125. /// <summary>
  126. /// Gets the column number of this attribute in the document.
  127. /// </summary>
  128. public int LinePosition
  129. {
  130. get
  131. {
  132. return _lineposition;
  133. }
  134. }
  135. /// <summary>
  136. /// Gets the stream position of this attribute in the document, relative to the start of the document.
  137. /// </summary>
  138. public int StreamPosition
  139. {
  140. get
  141. {
  142. return _streamposition;
  143. }
  144. }
  145. /// <summary>
  146. /// Gets the HTML node to which this attribute belongs.
  147. /// </summary>
  148. public HtmlNode OwnerNode
  149. {
  150. get
  151. {
  152. return _ownernode;
  153. }
  154. }
  155. /// <summary>
  156. /// Gets the HTML document to which this attribute belongs.
  157. /// </summary>
  158. public HtmlDocument OwnerDocument
  159. {
  160. get
  161. {
  162. return _ownerdocument;
  163. }
  164. }
  165. }
  166. /// <summary>
  167. /// Represents a combined list and collection of HTML nodes.
  168. /// </summary>
  169. public class HtmlAttributeCollection: IEnumerable
  170. {
  171. internal Hashtable _hashitems = new Hashtable();
  172. private ArrayList _items = new ArrayList();
  173. private HtmlNode _ownernode;
  174. internal HtmlAttributeCollection(HtmlNode ownernode)
  175. {
  176. _ownernode = ownernode;
  177. }
  178. /// <summary>
  179. /// Inserts the specified attribute as the last attribute in the collection.
  180. /// </summary>
  181. /// <param name="newAttribute">The attribute to insert. May not be null.</param>
  182. /// <returns>The appended attribute.</returns>
  183. public HtmlAttribute Append(HtmlAttribute newAttribute)
  184. {
  185. if (newAttribute == null)
  186. {
  187. throw new ArgumentNullException("newAttribute");
  188. }
  189. _hashitems[newAttribute.Name] = newAttribute;
  190. newAttribute._ownernode = _ownernode;
  191. _items.Add(newAttribute);
  192. _ownernode._innerchanged = true;
  193. _ownernode._outerchanged = true;
  194. return newAttribute;
  195. }
  196. /// <summary>
  197. /// Creates and inserts a new attribute as the last attribute in the collection.
  198. /// </summary>
  199. /// <param name="name">The name of the attribute to insert.</param>
  200. /// <returns>The appended attribute.</returns>
  201. public HtmlAttribute Append(string name)
  202. {
  203. HtmlAttribute att = _ownernode._ownerdocument.CreateAttribute(name);
  204. return Append(att);
  205. }
  206. /// <summary>
  207. /// Creates and inserts a new attribute as the last attribute in the collection.
  208. /// </summary>
  209. /// <param name="name">The name of the attribute to insert.</param>
  210. /// <param name="value">The value of the attribute to insert.</param>
  211. /// <returns>The appended attribute.</returns>
  212. public HtmlAttribute Append(string name, string value)
  213. {
  214. HtmlAttribute att = _ownernode._ownerdocument.CreateAttribute(name, value);
  215. return Append(att);
  216. }
  217. /// <summary>
  218. /// Inserts the specified attribute as the first node in the collection.
  219. /// </summary>
  220. /// <param name="newAttribute">The attribute to insert. May not be null.</param>
  221. /// <returns>The prepended attribute.</returns>
  222. public HtmlAttribute Prepend(HtmlAttribute newAttribute)
  223. {
  224. if (newAttribute == null)
  225. {
  226. throw new ArgumentNullException("newAttribute");
  227. }
  228. _hashitems[newAttribute.Name] = newAttribute;
  229. newAttribute._ownernode = _ownernode;
  230. _items.Insert(0, newAttribute);
  231. _ownernode._innerchanged = true;
  232. _ownernode._outerchanged = true;
  233. return newAttribute;
  234. }
  235. /// <summary>
  236. /// Removes the attribute at the specified index.
  237. /// </summary>
  238. /// <param name="index">The index of the attribute to remove.</param>
  239. public void RemoveAt(int index)
  240. {
  241. HtmlAttribute att = (HtmlAttribute)_items[index];
  242. _hashitems.Remove(att.Name);
  243. _items.RemoveAt(index);
  244. _ownernode._innerchanged = true;
  245. _ownernode._outerchanged = true;
  246. }
  247. /// <summary>
  248. /// Removes a given attribute from the list.
  249. /// </summary>
  250. /// <param name="attribute">The attribute to remove. May not be null.</param>
  251. public void Remove(HtmlAttribute attribute)
  252. {
  253. if (attribute == null)
  254. {
  255. throw new ArgumentNullException("attribute");
  256. }
  257. int index = GetAttributeIndex(attribute);
  258. if (index == -1)
  259. {
  260. throw new IndexOutOfRangeException();
  261. }
  262. RemoveAt(index);
  263. }
  264. /// <summary>
  265. /// Removes an attribute from the list, using its name. If there are more than one attributes with this name, they will all be removed.
  266. /// </summary>
  267. /// <param name="name">The attribute's name. May not be null.</param>
  268. public void Remove(string name)
  269. {
  270. if (name == null)
  271. {
  272. throw new ArgumentNullException("name");
  273. }
  274. string lname = name.ToLower();
  275. for(int i=0;i<_items.Count;i++)
  276. {
  277. HtmlAttribute att = (HtmlAttribute)_items[i];
  278. if (att.Name == lname)
  279. {
  280. RemoveAt(i);
  281. }
  282. }
  283. }
  284. /// <summary>
  285. /// Remove all attributes in the list.
  286. /// </summary>
  287. public void RemoveAll()
  288. {
  289. _hashitems.Clear();
  290. _items.Clear();
  291. _ownernode._innerchanged = true;
  292. _ownernode._outerchanged = true;
  293. }
  294. /// <summary>
  295. /// Gets the number of elements actually contained in the list.
  296. /// </summary>
  297. public int Count
  298. {
  299. get
  300. {
  301. return _items.Count;
  302. }
  303. }
  304. internal int GetAttributeIndex(HtmlAttribute attribute)
  305. {
  306. if (attribute == null)
  307. {
  308. throw new ArgumentNullException("attribute");
  309. }
  310. for(int i=0;i<_items.Count;i++)
  311. {
  312. if (((HtmlAttribute)_items[i])==attribute)
  313. return i;
  314. }
  315. return -1;
  316. }
  317. internal int GetAttributeIndex(string name)
  318. {
  319. if (name == null)
  320. {
  321. throw new ArgumentNullException("name");
  322. }
  323. string lname = name.ToLower();
  324. for(int i=0;i<_items.Count;i++)
  325. {
  326. if (((HtmlAttribute)_items[i]).Name==lname)
  327. return i;
  328. }
  329. return -1;
  330. }
  331. /// <summary>
  332. /// Gets a given attribute from the list using its name.
  333. /// </summary>
  334. public HtmlAttribute this[string name]
  335. {
  336. get
  337. {
  338. if (name == null)
  339. {
  340. throw new ArgumentNullException("name");
  341. }
  342. return _hashitems[name.ToLower()] as HtmlAttribute;
  343. }
  344. }
  345. /// <summary>
  346. /// Gets the attribute at the specified index.
  347. /// </summary>
  348. public HtmlAttribute this[int index]
  349. {
  350. get
  351. {
  352. return _items[index] as HtmlAttribute;
  353. }
  354. }
  355. internal void Clear()
  356. {
  357. _hashitems.Clear();
  358. _items.Clear();
  359. }
  360. /// <summary>
  361. /// Returns an enumerator that can iterate through the list.
  362. /// </summary>
  363. /// <returns>An IEnumerator for the entire list.</returns>
  364. public HtmlAttributeEnumerator GetEnumerator()
  365. {
  366. return new HtmlAttributeEnumerator(_items);
  367. }
  368. IEnumerator IEnumerable.GetEnumerator()
  369. {
  370. return GetEnumerator();
  371. }
  372. /// <summary>
  373. /// Represents an enumerator that can iterate through the list.
  374. /// </summary>
  375. public class HtmlAttributeEnumerator: IEnumerator
  376. {
  377. int _index;
  378. ArrayList _items;
  379. internal HtmlAttributeEnumerator(ArrayList items)
  380. {
  381. _items = items;
  382. _index = -1;
  383. }
  384. /// <summary>
  385. /// Sets the enumerator to its initial position, which is before the first element in the collection.
  386. /// </summary>
  387. public void Reset()
  388. {
  389. _index = -1;
  390. }
  391. /// <summary>
  392. /// Advances the enumerator to the next element of the collection.
  393. /// </summary>
  394. /// <returns>true if the enumerator was successfully advanced to the next element, false if the enumerator has passed the end of the collection.</returns>
  395. public bool MoveNext()
  396. {
  397. _index++;
  398. return (_index<_items.Count);
  399. }
  400. /// <summary>
  401. /// Gets the current element in the collection.
  402. /// </summary>
  403. public HtmlAttribute Current
  404. {
  405. get
  406. {
  407. return (HtmlAttribute)(_items[_index]);
  408. }
  409. }
  410. /// <summary>
  411. /// Gets the current element in the collection.
  412. /// </summary>
  413. object IEnumerator.Current
  414. {
  415. get
  416. {
  417. return (Current);
  418. }
  419. }
  420. }
  421. }
  422. }