AngleSharp by AngleSharp

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

.NET API 1,230,848 bytes

 CssDeclarationRule

Represents the base class for all style-rule similar rules.
using AngleSharp.Extensions; using AngleSharp.Parser.Css; using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; namespace AngleSharp.Dom.Css { internal abstract class CssDeclarationRule : CssRule, ICssProperties, IEnumerable<ICssProperty>, IEnumerable { private struct FormatTransporter : IStyleFormattable { private readonly IEnumerable<CssProperty> _properties; public FormatTransporter(IEnumerable<CssProperty> properties) { _properties = from m in properties where m.HasValue select m; } public void ToCss(TextWriter writer, IStyleFormatter formatter) { IEnumerable<string> declarations = from m in _properties select m.ToCss(formatter); string value = formatter.Declarations(declarations); writer.Write(value); } } private readonly string _name; public string this[string propertyName] { get { return GetValue(propertyName); } } public IEnumerable<CssProperty> Declarations => base.Children.OfType<CssProperty>(); public int Length => Declarations.Count(); internal CssDeclarationRule(CssRuleType type, string name, CssParser parser) : base(type, parser) { _name = name; } public string GetPropertyValue(string propertyName) { return GetValue(propertyName); } public string GetPropertyPriority(string propertyName) { return null; } public void SetProperty(string propertyName, string propertyValue, string priority = null) { SetValue(propertyName, propertyValue); } public string RemoveProperty(string propertyName) { foreach (CssProperty declaration in Declarations) { if (declaration.HasValue && declaration.Name.Is(propertyName)) { string value = declaration.Value; RemoveChild(declaration); return value; } } return null; } public IEnumerator<ICssProperty> GetEnumerator() { return Declarations.GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } internal void SetProperty(CssProperty property) { foreach (CssProperty declaration in Declarations) { if (declaration.Name.Is(property.Name)) { ReplaceChild(declaration, property); return; } } AppendChild(property); } public override void ToCss(TextWriter writer, IStyleFormatter formatter) { FormatTransporter formatTransporter = new FormatTransporter(Declarations); string value = formatter.Style("@" + _name, formatTransporter); writer.Write(value); } protected abstract CssProperty CreateNewProperty(string name); protected string GetValue(string propertyName) { foreach (CssProperty declaration in Declarations) { if (declaration.HasValue && declaration.Name.Is(propertyName)) return declaration.Value; } return string.Empty; } protected void SetValue(string propertyName, string valueText) { foreach (CssProperty declaration in Declarations) { if (declaration.Name.Is(propertyName)) { CssValue newValue = base.Parser.ParseValue(valueText); declaration.TrySetValue(newValue); return; } } CssProperty cssProperty = CreateNewProperty(propertyName); if (cssProperty != null) { CssValue newValue2 = base.Parser.ParseValue(valueText); cssProperty.TrySetValue(newValue2); AppendChild(cssProperty); } } } }