CssDeclarationRule
Represents the base class for all style-rule similar rules.
using AngleSharp.Extensions;
using AngleSharp.Parser.Css;
using System.Collections.Generic;
using System.Linq;
namespace AngleSharp.Dom.Css
{
internal abstract class CssDeclarationRule : CssRule
{
private readonly string _name;
public IEnumerable<CssProperty> Declarations => base.Children.OfType<CssProperty>();
internal CssDeclarationRule(CssRuleType type, string name, CssParser parser)
: base(type, parser)
{
_name = name;
}
internal void SetProperty(CssProperty property)
{
foreach (CssProperty declaration in Declarations) {
if (declaration.Name.Is(property.Name)) {
ReplaceChild(declaration, property);
return;
}
}
AppendChild(property);
}
public override string ToCss(IStyleFormatter formatter)
{
string rules = formatter.Block(from m in Declarations
where m.HasValue
select m);
return formatter.Rule("@" + _name, null, rules);
}
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);
break;
}
}
}
}
}