Skip to content

Commit d20ea49

Browse files
committed
Implemented getPseudoElements
1 parent 5a5227c commit d20ea49

File tree

5 files changed

+80
-12
lines changed

5 files changed

+80
-12
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
namespace AngleSharp.Css.Dom
2+
{
3+
using AngleSharp.Dom;
4+
using AngleSharp.Dom.Events;
5+
using System;
6+
7+
sealed class CssPseudoElement : ICssPseudoElement
8+
{
9+
private readonly IPseudoElement _host;
10+
private readonly String _type;
11+
private readonly ICssStyleDeclaration _style;
12+
13+
public CssPseudoElement(IPseudoElement host, String type, ICssStyleDeclaration style)
14+
{
15+
_host = host;
16+
_type = type;
17+
_style = style;
18+
}
19+
20+
String ICssPseudoElement.Type => _type;
21+
22+
ICssStyleDeclaration ICssPseudoElement.Style => _style;
23+
24+
void IEventTarget.AddEventListener(string type, DomEventHandler callback, bool capture) => _host.AddEventListener(type, callback, capture);
25+
26+
bool IEventTarget.Dispatch(Event ev) => _host.Dispatch(ev);
27+
28+
void IEventTarget.InvokeEventListener(Event ev) => _host.InvokeEventListener(ev);
29+
30+
void IEventTarget.RemoveEventListener(string type, DomEventHandler callback, bool capture) => _host.RemoveEventListener(type, callback, capture);
31+
}
32+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
namespace AngleSharp.Css.Dom
2+
{
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
7+
sealed class CssPseudoElementList : ICssPseudoElementList
8+
{
9+
private readonly ICssPseudoElement[] _elements;
10+
11+
public CssPseudoElementList(IEnumerable<ICssPseudoElement> elements)
12+
{
13+
_elements = elements.ToArray();
14+
}
15+
16+
public ICssPseudoElement this[Int32 index] => index >= 0 && index < _elements.Length ? _elements[index] : null;
17+
18+
public ICssPseudoElement this[String type] => _elements.FirstOrDefault(m => m.Type == type);
19+
20+
public Int32 Length => _elements.Length;
21+
}
22+
}

src/AngleSharp.Css/Dom/Internal/PseudoElement.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ public String TextContent
133133

134134
public NodeFlags Flags => _host.Flags;
135135

136+
136137
#endregion
137138

138139
#region Methods

src/AngleSharp.Css/Extensions/WindowExtensions.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ namespace AngleSharp.Dom
44
using AngleSharp.Css;
55
using AngleSharp.Css.Dom;
66
using System;
7+
using System.Linq;
78

89
/// <summary>
910
/// A set of useful extension methods for the Window class.
@@ -36,7 +37,27 @@ public static IMediaQueryList MatchMedia(this IWindow window, String mediaText)
3637
[DomName("getPseudoElements")]
3738
public static ICssPseudoElementList GetPseudoElements(this IWindow window, IElement element, String type = null)
3839
{
39-
throw new NotImplementedException();
40+
var validTypes = new[] { "::before", "::after" };
41+
42+
if (type == null)
43+
{
44+
// Everything is fine - we take all valid types
45+
}
46+
else if (validTypes.Contains(type))
47+
{
48+
validTypes = new[] { type };
49+
}
50+
else
51+
{
52+
throw new DomException(DomError.NotSupported);
53+
}
54+
55+
return new CssPseudoElementList(validTypes.Select(pseudoSelector =>
56+
{
57+
var pseudoElement = element?.Pseudo(pseudoSelector.TrimStart(':'));
58+
var style = window.GetComputedStyle(pseudoElement);
59+
return new CssPseudoElement(pseudoElement, pseudoSelector, style);
60+
}));
4061
}
4162

4263
/// <summary>

src/AngleSharp.Css/Factories/DefaultPseudoElementFactory.cs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,7 @@ public class DefaultPseudoElementFactory : IPseudoElementFactory
3131
/// </summary>
3232
/// <param name="type">The pseudo element type.</param>
3333
/// <param name="creator">The creator to invoke.</param>
34-
public void Register(String type, Creator creator)
35-
{
36-
_creators.Add(type, creator);
37-
}
34+
public void Register(String type, Creator creator) => _creators.Add(type, creator);
3835

3936
/// <summary>
4037
/// Unregisters an existing creator for the given pseudo type.
@@ -60,10 +57,7 @@ public Creator Unregister(String type)
6057
/// <param name="host">The hosting element.</param>
6158
/// <param name="type">The type of pseudo element.</param>
6259
/// <returns>The default pseudo element instance.</returns>
63-
protected virtual IPseudoElement CreateDefault(IElement host, String type)
64-
{
65-
return default;
66-
}
60+
protected virtual IPseudoElement CreateDefault(IElement host, String type) => default;
6761

6862
/// <summary>
6963
/// Creates the pseudo element instance for the given pseudo type.
@@ -73,9 +67,7 @@ protected virtual IPseudoElement CreateDefault(IElement host, String type)
7367
/// <returns>The pseudo element instance.</returns>
7468
public IPseudoElement Create(IElement host, String type)
7569
{
76-
var creator = default(Creator);
77-
78-
if (!String.IsNullOrEmpty(type) && _creators.TryGetValue(type, out creator))
70+
if (!String.IsNullOrEmpty(type) && _creators.TryGetValue(type, out var creator))
7971
{
8072
return creator(host);
8173
}

0 commit comments

Comments
 (0)