CssDeclarationRule
using AngleSharp.Parser.Css;
using System.Collections.Generic;
using System.Linq;
namespace AngleSharp.Dom.Css
{
internal abstract class CssDeclarationRule : CssRule
{
private readonly List<CssProperty> _declarations;
private readonly string _name;
internal CssDeclarationRule(CssRuleType type, string name, CssParser parser)
: base(type, parser)
{
_declarations = new List<CssProperty>();
_name = name;
}
internal void SetProperty(CssProperty property)
{
for (int i = 0; i < _declarations.Count; i++) {
if (_declarations[i].Name == property.Name) {
_declarations[i] = property;
return;
}
}
_declarations.Add(property);
}
protected override void ReplaceWith(ICssRule rule)
{
CssDeclarationRule cssDeclarationRule = (CssDeclarationRule)rule;
_declarations.Clear();
_declarations.AddRange(cssDeclarationRule._declarations);
cssDeclarationRule._declarations.Clear();
}
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 == propertyName)
return declaration.Value;
}
return string.Empty;
}
protected void SetValue(string propertyName, string valueText)
{
foreach (CssProperty declaration in _declarations) {
if (declaration.Name == propertyName) {
CssValue newValue = base.Parser.ParseValue(valueText);
declaration.TrySetValue(newValue);
break;
}
}
}
}
}