CssCombinator
An enumeration with possible CSS combinator values.
using AngleSharp.Css.Dom;
using AngleSharp.Dom;
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
namespace AngleSharp.Css.Parser
{
[System.Runtime.CompilerServices.NullableContext(1)]
[System.Runtime.CompilerServices.Nullable(0)]
internal abstract class CssCombinator
{
[System.Runtime.CompilerServices.NullableContext(0)]
private sealed class ChildCombinator : CssCombinator
{
public ChildCombinator()
{
base.Delimiter = CombinatorSymbols.Child;
base.Transform = ((IElement el) => Single(el.ParentElement));
}
}
[System.Runtime.CompilerServices.NullableContext(0)]
private sealed class DeepCombinator : CssCombinator
{
public DeepCombinator()
{
base.Delimiter = CombinatorSymbols.Deep;
base.Transform = ((IElement el) => Single((el.Parent as IShadowRoot)?.Host));
}
}
[System.Runtime.CompilerServices.NullableContext(0)]
private sealed class DescendantCombinator : CssCombinator
{
public DescendantCombinator()
{
base.Delimiter = CombinatorSymbols.Descendant;
base.Transform = delegate(IElement el) {
List<IElement> list = new List<IElement>();
for (IElement parentElement = el.ParentElement; parentElement != null; parentElement = parentElement.ParentElement) {
list.Add(parentElement);
}
return list;
};
}
}
[System.Runtime.CompilerServices.NullableContext(0)]
private sealed class AdjacentSiblingCombinator : CssCombinator
{
public AdjacentSiblingCombinator()
{
base.Delimiter = CombinatorSymbols.Adjacent;
base.Transform = ((IElement el) => Single(el.PreviousElementSibling));
}
}
[System.Runtime.CompilerServices.NullableContext(0)]
private sealed class SiblingCombinator : CssCombinator
{
public SiblingCombinator()
{
base.Delimiter = CombinatorSymbols.Sibling;
base.Transform = delegate(IElement el) {
IElement parentElement = el.ParentElement;
if (parentElement != null) {
List<IElement> list = new List<IElement>();
{
foreach (INode childNode in parentElement.ChildNodes) {
IElement element = childNode as IElement;
if (element != null) {
if (element == el)
return list;
list.Add(element);
}
}
return list;
}
}
return Array.Empty<IElement>();
};
}
}
[System.Runtime.CompilerServices.NullableContext(0)]
private sealed class NamespaceCombinator : CssCombinator
{
public NamespaceCombinator()
{
base.Delimiter = CombinatorSymbols.Pipe;
base.Transform = ((IElement el) => Single(el));
}
[System.Runtime.CompilerServices.NullableContext(1)]
public override ISelector Change(ISelector selector)
{
TypeSelector typeSelector = selector as TypeSelector;
string prefix = (typeSelector == null) ? selector.Text : typeSelector.TypeName;
return new NamespaceSelector(prefix);
}
}
[System.Runtime.CompilerServices.NullableContext(0)]
private sealed class ColumnCombinator : CssCombinator
{
public ColumnCombinator()
{
base.Delimiter = CombinatorSymbols.Column;
}
}
public static readonly CssCombinator Child = new ChildCombinator();
public static readonly CssCombinator Deep = new DeepCombinator();
public static readonly CssCombinator Descendant = new DescendantCombinator();
public static readonly CssCombinator AdjacentSibling = new AdjacentSiblingCombinator();
public static readonly CssCombinator Sibling = new SiblingCombinator();
public static readonly CssCombinator Namespace = new NamespaceCombinator();
public static readonly CssCombinator Column = new ColumnCombinator();
[System.Runtime.CompilerServices.Nullable(new byte[] {
2,
1,
1,
1
})]
[field: System.Runtime.CompilerServices.Nullable(new byte[] {
2,
1,
1,
1
})]
public Func<IElement, IEnumerable<IElement>> Transform {
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
2,
1,
1,
1
})]
get;
[param: System.Runtime.CompilerServices.Nullable(new byte[] {
2,
1,
1,
1
})]
protected set;
}
[System.Runtime.CompilerServices.Nullable(2)]
[field: System.Runtime.CompilerServices.Nullable(2)]
public string Delimiter {
[System.Runtime.CompilerServices.NullableContext(2)]
get;
[System.Runtime.CompilerServices.NullableContext(2)]
protected set;
}
public virtual ISelector Change(ISelector selector)
{
return selector;
}
protected static IEnumerable<IElement> Single([System.Runtime.CompilerServices.Nullable(2)] IElement element)
{
if (element != null)
yield return element;
}
}
}