CssProperty
Fore more information about CSS properties see:
http://www.w3.org/TR/CSS21/propidx.html.
using AngleSharp.Css;
namespace AngleSharp.Dom.Css
{
internal abstract class CssProperty : ICssProperty
{
private readonly PropertyFlags _flags;
private readonly string _name;
private readonly CssStyleDeclaration _rule;
private bool _important;
private ICssValue _value;
internal CssStyleDeclaration Rule => _rule;
internal bool HasValue => _value != null;
internal bool CanBeHashless => _flags.HasFlag(PropertyFlags.Hashless);
internal bool CanBeUnitless => _flags.HasFlag(PropertyFlags.Unitless);
internal bool CanBeInherited => _flags.HasFlag(PropertyFlags.Inherited);
internal bool IsShorthand => _flags.HasFlag(PropertyFlags.Shorthand);
internal ICssValue Value => _value ?? CssValue.Initial;
public bool IsInherited {
get {
if (!_flags.HasFlag(PropertyFlags.Inherited) || !IsInitial)
return _value == CssValue.Inherit;
return true;
}
}
public bool IsAnimatable => _flags.HasFlag(PropertyFlags.Animatable);
public bool IsInitial {
get {
if (_value != null)
return _value == CssValue.Initial;
return true;
}
}
public string Name => _name;
ICssValue ICssProperty.Value {
get {
return Value;
}
}
public bool IsImportant {
get {
return _important;
}
set {
_important = value;
}
}
public string CssText => Serialize(_name, SerializeValue(), _important);
internal CssProperty(string name, CssStyleDeclaration rule, PropertyFlags flags = PropertyFlags.None)
{
_rule = rule;
_name = name;
_flags = flags;
}
internal bool TrySetValue(ICssValue value)
{
if (value == CssValue.Inherit || value == CssValue.Initial || value == null) {
Reset();
_value = value;
return true;
}
if (IsValid(value)) {
_value = value;
return true;
}
return false;
}
internal abstract void Reset();
protected abstract bool IsValid(ICssValue value);
internal virtual string SerializeValue()
{
return Value.CssText;
}
internal static string Serialize(string name, string value, bool important)
{
return name + ": " + (value + (important ? " !important" : string.Empty) + ";");
}
}
}