File: HtmlAttribute.cs

package info (click to toggle)
mono 6.8.0.105%2Bdfsg-3.3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,284,512 kB
  • sloc: cs: 11,172,132; xml: 2,850,069; ansic: 671,653; cpp: 122,091; perl: 59,366; javascript: 30,841; asm: 22,168; makefile: 20,093; sh: 15,020; python: 4,827; pascal: 925; sql: 859; sed: 16; php: 1
file content (468 lines) | stat: -rw-r--r-- 10,435 bytes parent folder | download | duplicates (15)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
// HtmlAgilityPack V1.0 - Simon Mourier <simonm@microsoft.com>
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);
				}
			}
		}
	}

}