AngleSharp by AngleSharp

<PackageReference Include="AngleSharp" Version="0.9.8" />

 CssParserExtensions

static class CssParserExtensions
Extensions to be used exclusively by the parser or the tokenizer.
using AngleSharp.Css; using AngleSharp.Dom.Css; using AngleSharp.Extensions; using System; using System.Collections.Generic; namespace AngleSharp.Parser.Css { internal static class CssParserExtensions { private static readonly Dictionary<string, Func<string, DocumentFunction>> functionTypes = new Dictionary<string, Func<string, DocumentFunction>>(StringComparer.OrdinalIgnoreCase) { { FunctionNames.Url, (string str) => new UrlFunction(str) }, { FunctionNames.Domain, (string str) => new DomainFunction(str) }, { FunctionNames.UrlPrefix, (string str) => new UrlPrefixFunction(str) } }; private static readonly Dictionary<string, Func<IEnumerable<IConditionFunction>, IConditionFunction>> groupCreators = new Dictionary<string, Func<IEnumerable<IConditionFunction>, IConditionFunction>>(StringComparer.OrdinalIgnoreCase) { { Keywords.And, CreateAndCondition }, { Keywords.Or, CreateOrCondition } }; private static IConditionFunction CreateAndCondition(IEnumerable<IConditionFunction> conditions) { AndCondition andCondition = new AndCondition(); foreach (IConditionFunction condition in conditions) { andCondition.AppendChild(condition); } return andCondition; } private static IConditionFunction CreateOrCondition(IEnumerable<IConditionFunction> conditions) { OrCondition orCondition = new OrCondition(); foreach (IConditionFunction condition in conditions) { orCondition.AppendChild(condition); } return orCondition; } public static CssTokenType GetTypeFromName(this string functionName) { Func<string, DocumentFunction> value = null; if (!functionTypes.TryGetValue(functionName, out value)) return CssTokenType.Function; return CssTokenType.Url; } public static Func<IEnumerable<IConditionFunction>, IConditionFunction> GetCreator(this string conjunction) { Func<IEnumerable<IConditionFunction>, IConditionFunction> value = null; groupCreators.TryGetValue(conjunction, out value); return value; } 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 IsDeclarationName(this CssToken token) { if (token.Type != CssTokenType.EndOfFile && token.Type != CssTokenType.Colon && token.Type != CssTokenType.Whitespace && token.Type != CssTokenType.Comment && token.Type != CssTokenType.CurlyBracketOpen) return token.Type != CssTokenType.Semicolon; return false; } public static DocumentFunction ToDocumentFunction(this CssToken token) { if (token.Type == CssTokenType.Url) { Func<string, DocumentFunction> value = null; string functionName = ((CssUrlToken)token).FunctionName; functionTypes.TryGetValue(functionName, out value); return value(token.Data); } if (token.Type == CssTokenType.Function && token.Data.Isi(FunctionNames.Regexp)) { string text = ((CssFunctionToken)token).ArgumentTokens.ToCssString(); if (text != null) return new RegexpFunction(text); } return null; } public static CssRule CreateRule(this CssParser parser, CssRuleType type) { switch (type) { case CssRuleType.Charset: return new CssCharsetRule(parser); case CssRuleType.Document: return new CssDocumentRule(parser); case CssRuleType.FontFace: return new CssFontFaceRule(parser); case CssRuleType.Import: return new CssImportRule(parser); case CssRuleType.Keyframe: return new CssKeyframeRule(parser); case CssRuleType.Keyframes: return new CssKeyframesRule(parser); case CssRuleType.Media: return new CssMediaRule(parser); case CssRuleType.Namespace: return new CssNamespaceRule(parser); case CssRuleType.Page: return new CssPageRule(parser); case CssRuleType.Style: return new CssStyleRule(parser); case CssRuleType.Supports: return new CssSupportsRule(parser); case CssRuleType.Viewport: return new CssViewportRule(parser); default: return null; } } } }