Skip to content

Commit a873d68

Browse files
authored
Merge pull request #1227 from weitzhandler/devel
Have IHtmlCollection<T> implement IReadOnlyList<T>
2 parents 3a6e056 + 914c7e1 commit a873d68

File tree

5 files changed

+171
-128
lines changed

5 files changed

+171
-128
lines changed

src/AngleSharp/Dom/IHtmlCollection.cs

Lines changed: 49 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,56 @@
1-
namespace AngleSharp.Dom
1+
namespace AngleSharp.Dom;
2+
3+
using AngleSharp.Attributes;
4+
using System;
5+
using System.Collections.Generic;
6+
7+
/// <summary>
8+
/// HTMLCollection is an interface representing a generic collection
9+
/// (array) of elements (in document order) and offers methods and
10+
/// properties for selecting from the list.
11+
/// </summary>
12+
[DomName("HTMLCollection")]
13+
public interface IHtmlCollection<T> :
14+
#if NET8_0_OR_GREATER
15+
IReadOnlyList<T>
16+
#else
17+
IEnumerable<T>
18+
#endif
19+
20+
where T : IElement
221
{
3-
using AngleSharp.Attributes;
4-
using System;
5-
using System.Collections.Generic;
22+
/// <summary>
23+
/// Gets the number of items in the collection.
24+
/// </summary>
25+
[DomName("length")]
26+
Int32 Length { get; }
627

728
/// <summary>
8-
/// HTMLCollection is an interface representing a generic collection
9-
/// (array) of elements (in document order) and offers methods and
10-
/// properties for selecting from the list.
29+
/// Gets the specific node at the given zero-based index into the list.
1130
/// </summary>
12-
[DomName("HTMLCollection")]
13-
public interface IHtmlCollection<T> : IEnumerable<T>
14-
where T : IElement
15-
{
16-
/// <summary>
17-
/// Gets the number of items in the collection.
18-
/// </summary>
19-
[DomName("length")]
20-
Int32 Length { get; }
31+
/// <param name="index">The zero-based index.</param>
32+
/// <returns>Returns the element at the specified index.</returns>
33+
[DomName("item")]
34+
[DomAccessor(Accessors.Getter)]
35+
#if NET8_0_OR_GREATER
36+
abstract T IReadOnlyList<T>.this[Int32 index] { get; }
37+
#else
38+
T this[Int32 index] { get; }
39+
#endif
2140

22-
/// <summary>
23-
/// Gets the specific node at the given zero-based index into the list.
24-
/// </summary>
25-
/// <param name="index">The zero-based index.</param>
26-
/// <returns>Returns the element at the specified index.</returns>
27-
[DomName("item")]
28-
[DomAccessor(Accessors.Getter)]
29-
T this[Int32 index] { get; }
41+
/// <summary>
42+
/// Gets the specific node whose ID or, as a fallback, name matches the
43+
/// string specified by name. Matching by name is only done as a last
44+
/// resort, only in HTML, and only if the referenced element supports
45+
/// the name attribute.
46+
/// </summary>
47+
/// <param name="id">The id or name to match.</param>
48+
/// <returns>Returns the element with the specified name.</returns>
49+
[DomName("namedItem")]
50+
[DomAccessor(Accessors.Getter)]
51+
T? this[String id] { get; }
3052

31-
/// <summary>
32-
/// Gets the specific node whose ID or, as a fallback, name matches the
33-
/// string specified by name. Matching by name is only done as a last
34-
/// resort, only in HTML, and only if the referenced element supports
35-
/// the name attribute.
36-
/// </summary>
37-
/// <param name="id">The id or name to match.</param>
38-
/// <returns>Returns the element with the specified name.</returns>
39-
[DomName("namedItem")]
40-
[DomAccessor(Accessors.Getter)]
41-
T? this[String id] { get; }
42-
}
53+
#if NET8_0_OR_GREATER
54+
Int32 IReadOnlyCollection<T>.Count => Length;
55+
#endif
4356
}

src/AngleSharp/Dom/IStringList.cs

Lines changed: 45 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,50 @@
1-
namespace AngleSharp.Dom
1+
namespace AngleSharp.Dom;
2+
3+
using AngleSharp.Attributes;
4+
using System;
5+
using System.Collections.Generic;
6+
7+
/// <summary>
8+
/// Represents a string list.
9+
/// </summary>
10+
[DomName("DOMStringList")]
11+
public interface IStringList :
12+
#if NET8_0_OR_GREATER
13+
IReadOnlyList<String>
14+
#else
15+
IEnumerable<String>
16+
#endif
217
{
3-
using AngleSharp.Attributes;
4-
using System;
5-
using System.Collections.Generic;
18+
/// <summary>
19+
/// Gets the value at the specified index.
20+
/// </summary>
21+
/// <param name="index">The index of the value.</param>
22+
/// <returns>The string value at the given index.</returns>
23+
[DomName("item")]
24+
[DomAccessor(Accessors.Getter)]
25+
#if NET8_0_OR_GREATER
26+
abstract String IReadOnlyList<String>.this[Int32 index] { get; }
27+
#else
28+
String this[Int32 index] { get; }
29+
#endif
630

731
/// <summary>
8-
/// Represents a string list.
32+
/// Gets the number of entries.
933
/// </summary>
10-
[DomName("DOMStringList")]
11-
public interface IStringList : IEnumerable<String>
12-
{
13-
/// <summary>
14-
/// Gets the value at the specified index.
15-
/// </summary>
16-
/// <param name="index">The index of the value.</param>
17-
/// <returns>The string value at the given index.</returns>
18-
[DomName("item")]
19-
[DomAccessor(Accessors.Getter)]
20-
String this[Int32 index] { get; }
21-
22-
/// <summary>
23-
/// Gets the number of entries.
24-
/// </summary>
25-
[DomName("length")]
26-
Int32 Length { get; }
27-
28-
/// <summary>
29-
/// Returns a boolean indicating if the specified entry is available.
30-
/// </summary>
31-
/// <param name="entry">The entry that will be looked for.</param>
32-
/// <returns>
33-
/// True if the element is available, otherwise false.
34-
/// </returns>
35-
[DomName("contains")]
36-
Boolean Contains(String entry);
37-
}
34+
[DomName("length")]
35+
Int32 Length { get; }
36+
37+
/// <summary>
38+
/// Returns a boolean indicating if the specified entry is available.
39+
/// </summary>
40+
/// <param name="entry">The entry that will be looked for.</param>
41+
/// <returns>
42+
/// True if the element is available, otherwise false.
43+
/// </returns>
44+
[DomName("contains")]
45+
Boolean Contains(String entry);
46+
47+
#if NET8_0_OR_GREATER
48+
Int32 IReadOnlyCollection<String>.Count => Length;
49+
#endif
3850
}

src/AngleSharp/Dom/ITokenList.cs

Lines changed: 70 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,75 @@
1-
namespace AngleSharp.Dom
1+
namespace AngleSharp.Dom;
2+
3+
using AngleSharp.Attributes;
4+
using System;
5+
using System.Collections.Generic;
6+
7+
/// <summary>
8+
/// This type represents a set of space-separated tokens.
9+
/// </summary>
10+
[DomName("DOMTokenList")]
11+
public interface ITokenList :
12+
#if NET8_0_OR_GREATER
13+
IReadOnlyList<String>
14+
#else
15+
IEnumerable<String>
16+
#endif
217
{
3-
using AngleSharp.Attributes;
4-
using System;
5-
using System.Collections.Generic;
18+
/// <summary>
19+
/// Gets the number of contained tokens.
20+
/// </summary>
21+
[DomName("length")]
22+
Int32 Length { get; }
623

724
/// <summary>
8-
/// This type represents a set of space-separated tokens.
25+
/// Gets an item in the list by its index.
926
/// </summary>
10-
[DomName("DOMTokenList")]
11-
public interface ITokenList : IEnumerable<String>
12-
{
13-
/// <summary>
14-
/// Gets the number of contained tokens.
15-
/// </summary>
16-
[DomName("length")]
17-
Int32 Length { get; }
18-
19-
/// <summary>
20-
/// Gets an item in the list by its index.
21-
/// </summary>
22-
/// <param name="index">The index of the item.</param>
23-
/// <returns>The item at the specified index.</returns>
24-
[DomName("item")]
25-
[DomAccessor(Accessors.Getter)]
26-
String this[Int32 index] { get; }
27-
28-
/// <summary>
29-
/// Returns true if the underlying string contains a token, otherwise
30-
/// false.
31-
/// </summary>
32-
/// <param name="token">The token to search for.</param>
33-
/// <returns>The result of the search.</returns>
34-
[DomName("contains")]
35-
Boolean Contains(String token);
36-
37-
/// <summary>
38-
/// Adds some tokens to the underlying string.
39-
/// </summary>
40-
/// <param name="tokens">A list of tokens to add.</param>
41-
[DomName("add")]
42-
void Add(params String[] tokens);
43-
44-
/// <summary>
45-
/// Remove some tokens from the underlying string.
46-
/// </summary>
47-
/// <param name="tokens">A list of tokens to remove.</param>
48-
[DomName("remove")]
49-
void Remove(params String[] tokens);
50-
51-
/// <summary>
52-
/// Removes the specified token from string and returns false.
53-
/// If token doesn't exist it's added and the function returns true.
54-
/// </summary>
55-
/// <param name="token">The token to toggle.</param>
56-
/// <param name="force"></param>
57-
/// <returns>
58-
/// True if the token has been added, otherwise false.
59-
/// </returns>
60-
[DomName("toggle")]
61-
Boolean Toggle(String token, Boolean force = false);
62-
}
27+
/// <param name="index">The index of the item.</param>
28+
/// <returns>The item at the specified index.</returns>
29+
[DomName("item")]
30+
[DomAccessor(Accessors.Getter)]
31+
#if NET8_0_OR_GREATER
32+
abstract String IReadOnlyList<String>.this[Int32 index] { get; }
33+
#else
34+
String this[Int32 index] { get; }
35+
#endif
36+
37+
/// <summary>
38+
/// Returns true if the underlying string contains a token, otherwise
39+
/// false.
40+
/// </summary>
41+
/// <param name="token">The token to search for.</param>
42+
/// <returns>The result of the search.</returns>
43+
[DomName("contains")]
44+
Boolean Contains(String token);
45+
46+
/// <summary>
47+
/// Adds some tokens to the underlying string.
48+
/// </summary>
49+
/// <param name="tokens">A list of tokens to add.</param>
50+
[DomName("add")]
51+
void Add(params String[] tokens);
52+
53+
/// <summary>
54+
/// Remove some tokens from the underlying string.
55+
/// </summary>
56+
/// <param name="tokens">A list of tokens to remove.</param>
57+
[DomName("remove")]
58+
void Remove(params String[] tokens);
59+
60+
/// <summary>
61+
/// Removes the specified token from string and returns false.
62+
/// If token doesn't exist it's added and the function returns true.
63+
/// </summary>
64+
/// <param name="token">The token to toggle.</param>
65+
/// <param name="force"></param>
66+
/// <returns>
67+
/// True if the token has been added, otherwise false.
68+
/// </returns>
69+
[DomName("toggle")]
70+
Boolean Toggle(String token, Boolean force = false);
71+
72+
#if NET8_0_OR_GREATER
73+
Int32 IReadOnlyCollection<String>.Count => Length;
74+
#endif
6375
}

src/AngleSharp/Dom/Internal/HtmlFormControlsCollection.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,11 @@ public HtmlFormControlsCollection(IElement form, IElement? root = null)
6363

6464
IEnumerator IEnumerable.GetEnumerator() => _elements.GetEnumerator();
6565

66+
#if NET8_0_OR_GREATER
67+
IHtmlElement IReadOnlyList<IHtmlElement>.this[Int32 index] => this[index];
68+
#else
6669
IHtmlElement IHtmlCollection<IHtmlElement>.this[Int32 index] => this[index];
70+
#endif
6771

6872
IHtmlElement? IHtmlCollection<IHtmlElement>.this[String id] => this[id];
6973

src/AngleSharp/Html/Dom/IHtmlFormControlsCollection.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
namespace AngleSharp.Html.Dom
1+
namespace AngleSharp.Html.Dom
22
{
33
using AngleSharp.Attributes;
44
using AngleSharp.Dom;
5+
using System;
6+
using System.Collections.Generic;
57

68
/// <summary>
79
/// Represents a collection of HTML form controls.

0 commit comments

Comments
 (0)