CssShorthandProperty
Base class for all shorthand properties
using AngleSharp.Css;
using System.Collections.Generic;
using System.Linq;
namespace AngleSharp.Dom.Css
{
internal abstract class CssShorthandProperty : CssProperty
{
private readonly CssProperty[] _properties;
public CssProperty[] Properties => _properties;
public CssShorthandProperty(string name, CssStyleDeclaration rule, PropertyFlags flags = PropertyFlags.None)
: base(name, rule, flags | PropertyFlags.Shorthand)
{
_properties = Factory.Properties.CreateLonghandsFor(name, rule).ToArray();
Reset();
}
protected TProperty Get<TProperty>()
{
return _properties.OfType<TProperty>().FirstOrDefault();
}
protected bool IsComplete(IEnumerable<CssProperty> properties)
{
CssProperty[] properties2 = _properties;
foreach (CssProperty value in properties2) {
if (!properties.Contains(value))
return false;
}
return true;
}
protected static bool ExpandPeriodic(CssValueList list)
{
if (list.Length == 0 || list.Length > 4)
return false;
if (list.Length == 1)
list.Add(list[0]);
if (list.Length == 2)
list.Add(list[0]);
if (list.Length == 3)
list.Add(list[1]);
return true;
}
protected static string SerializePeriodic(CssProperty t, CssProperty r, CssProperty b, CssProperty l)
{
string top = t.SerializeValue();
string right = r.SerializeValue();
string bottom = b.SerializeValue();
string left = l.SerializeValue();
return SerializePeriodic(top, right, bottom, left);
}
protected static string SerializePeriodic(ICssValue top, ICssValue right, ICssValue bottom, ICssValue left)
{
return SerializePeriodic(top.CssText, right.CssText, bottom.CssText, left.CssText);
}
protected static string SerializePeriodic(string top, string right, string bottom, string left)
{
if (left != right)
return $"{top}""{right}""{bottom}""{left}";
if (bottom != top)
return string.Format("{0} {1} {2}", new object[3] {
top,
right,
bottom
});
if (right != top)
return string.Format("{0} {1}", new object[2] {
top,
right
});
return top;
}
internal sealed override void Reset()
{
CssProperty[] properties = _properties;
for (int i = 0; i < properties.Length; i++) {
properties[i].Reset();
}
}
internal sealed override string SerializeValue()
{
return SerializeValue(_properties);
}
internal abstract string SerializeValue(IEnumerable<CssProperty> properties);
}
}