CssParserExtensions
Extensions to be used exclusively by the parser or the tokenizer.
using AngleSharp.Css;
using AngleSharp.Css.Conditions;
using AngleSharp.Css.DocumentFunctions;
using AngleSharp.Extensions;
using System;
using System.Collections.Generic;
using System.Diagnostics;
namespace AngleSharp.Parser.Css
{
[DebuggerStepThrough]
internal static class CssParserExtensions
{
private static readonly Dictionary<string, CssTokenType> functionTypes = new Dictionary<string, CssTokenType>(StringComparer.OrdinalIgnoreCase) {
{
FunctionNames.Url,
CssTokenType.Url
},
{
FunctionNames.Domain,
CssTokenType.Domain
},
{
FunctionNames.UrlPrefix,
CssTokenType.UrlPrefix
}
};
private static readonly Dictionary<string, Func<IEnumerable<CssCondition>, CssCondition>> groupCreators = new Dictionary<string, Func<IEnumerable<CssCondition>, CssCondition>>(StringComparer.OrdinalIgnoreCase) {
{
Keywords.And,
(IEnumerable<CssCondition> conditions) => new AndCondition(conditions)
},
{
Keywords.Or,
(IEnumerable<CssCondition> conditions) => new OrCondition(conditions)
}
};
public static CssTokenType GetTypeFromName(this string functionName)
{
CssTokenType value = CssTokenType.String;
if (functionTypes.TryGetValue(functionName, out value))
return value;
return CssTokenType.Function;
}
public static Func<IEnumerable<CssCondition>, CssCondition> GetCreator(this string conjunction)
{
Func<IEnumerable<CssCondition>, CssCondition> value = null;
if (groupCreators.TryGetValue(conjunction, out value))
return value;
return null;
}
public static int GetCode(this CssParseError code)
{
return (int)code;
}
public static bool Is(this CssToken token, CssTokenType a, CssTokenType b)
{
CssTokenType type = token.Type;
if (type != a)
return type == b;
return true;
}
public static bool IsNot(this CssToken token, CssTokenType a, CssTokenType b)
{
CssTokenType type = token.Type;
if (type != a)
return type != b;
return false;
}
public static bool IsNot(this CssToken token, CssTokenType a, CssTokenType b, CssTokenType c)
{
CssTokenType type = token.Type;
if (type != a && type != b)
return type != c;
return false;
}
public static bool IsMatchToken(this CssToken token)
{
CssTokenType type = token.Type;
if (type != CssTokenType.IncludeMatch && type != CssTokenType.DashMatch && type != CssTokenType.PrefixMatch && type != CssTokenType.SubstringMatch && type != CssTokenType.SuffixMatch)
return type == CssTokenType.NotMatch;
return true;
}
public static CssDocumentFunction ToDocumentFunction(this CssToken token)
{
switch (token.Type) {
case CssTokenType.Url:
return new UrlFunction(token.Data);
case CssTokenType.UrlPrefix:
return new UrlPrefixFunction(token.Data);
case CssTokenType.Domain:
return new DomainFunction(token.Data);
case CssTokenType.Function:
if (string.Compare(token.Data, FunctionNames.Regexp, StringComparison.OrdinalIgnoreCase) == 0) {
string text = ((CssFunctionToken)token).ToCssString();
if (text != null)
return new RegexpFunction(text);
}
break;
}
return null;
}
}
}