PseudoElementSelectorFactory
Provides string to CSS pseudo element selector instance mappings.
using AngleSharp.Css;
using AngleSharp.Dom;
using AngleSharp.Dom.Css;
using AngleSharp.Extensions;
using System;
using System.Collections.Generic;
namespace AngleSharp.Services.Default
{
public class PseudoElementSelectorFactory : IPseudoElementSelectorFactory
{
private readonly Dictionary<string, ISelector> _selectors = new Dictionary<string, ISelector>(StringComparer.OrdinalIgnoreCase) {
{
PseudoElementNames.Before,
SimpleSelector.PseudoElement((IElement el) => el.IsPseudo("::" + PseudoElementNames.Before), PseudoElementNames.Before)
},
{
PseudoElementNames.After,
SimpleSelector.PseudoElement((IElement el) => el.IsPseudo("::" + PseudoElementNames.After), PseudoElementNames.After)
},
{
PseudoElementNames.Selection,
SimpleSelector.PseudoElement((IElement el) => false, PseudoElementNames.Selection)
},
{
PseudoElementNames.FirstLine,
SimpleSelector.PseudoElement(delegate(IElement el) {
if (el.HasChildNodes)
return el.ChildNodes[0].NodeType == NodeType.Text;
return false;
}, PseudoElementNames.FirstLine)
},
{
PseudoElementNames.FirstLetter,
SimpleSelector.PseudoElement(delegate(IElement el) {
if (el.HasChildNodes && el.ChildNodes[0].NodeType == NodeType.Text)
return el.ChildNodes[0].TextContent.Length > 0;
return false;
}, PseudoElementNames.FirstLetter)
},
{
PseudoElementNames.Content,
SimpleSelector.PseudoElement((IElement el) => false, PseudoElementNames.Content)
}
};
public void Register(string name, ISelector selector)
{
_selectors.Add(name, selector);
}
public ISelector Unregister(string name)
{
ISelector value = null;
if (_selectors.TryGetValue(name, out value))
_selectors.Remove(name);
return value;
}
protected virtual ISelector CreateDefault(string name)
{
return null;
}
public ISelector Create(string name)
{
ISelector value = null;
if (_selectors.TryGetValue(name, out value))
return value;
return CreateDefault(name);
}
}
}