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 CssValue _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 CssValue Value => _value ?? CssValue.Initial;
public bool IsInherited {
get {
if (!_flags.HasFlag(PropertyFlags.Inherited) || !IsInitial)
return Value.Type == CssValueType.Inherit;
return true;
}
}
public bool IsAnimatable => _flags.HasFlag(PropertyFlags.Animatable);
public bool IsInitial {
get {
if (_value != null)
return _value.Type == CssValueType.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(CssValue value)
{
if (value == null || value.Type == CssValueType.Inherit || value.Type == CssValueType.Initial) {
Reset();
_value = value;
return true;
}
if (IsValid(value)) {
_value = value;
return true;
}
return false;
}
internal virtual void Reset()
{
_value = null;
}
protected abstract object GetDefault(IElement element);
protected abstract object Compute(IElement element);
protected abstract bool IsValid(CssValue 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) + ";");
}
}
}