CssBorderProperty
More information available at:
https://developer.mozilla.org/en-US/docs/Web/CSS/border
using AngleSharp.Css;
using AngleSharp.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
namespace AngleSharp.Dom.Css
{
internal sealed class CssBorderProperty : CssShorthandProperty
{
internal static readonly IValueConverter<Tuple<CssValue, CssValue, CssValue>> Converter = Converters.WithAny(Converters.LineWidthConverter.Val().Option(), Converters.LineStyleConverter.Val().Option(), Converters.CurrentColorConverter.Val().Option());
internal CssBorderProperty(CssStyleDeclaration rule)
: base(PropertyNames.Border, rule, PropertyFlags.Animatable)
{
}
protected override bool IsValid(CssValue value)
{
return Converter.TryConvert(value, delegate(Tuple<CssValue, CssValue, CssValue> m) {
Get<CssBorderTopWidthProperty>().TrySetValue(m.Item1);
Get<CssBorderTopStyleProperty>().TrySetValue(m.Item2);
Get<CssBorderTopColorProperty>().TrySetValue(m.Item3);
Get<CssBorderLeftWidthProperty>().TrySetValue(m.Item1);
Get<CssBorderLeftStyleProperty>().TrySetValue(m.Item2);
Get<CssBorderLeftColorProperty>().TrySetValue(m.Item3);
Get<CssBorderRightWidthProperty>().TrySetValue(m.Item1);
Get<CssBorderRightStyleProperty>().TrySetValue(m.Item2);
Get<CssBorderRightColorProperty>().TrySetValue(m.Item3);
Get<CssBorderBottomWidthProperty>().TrySetValue(m.Item1);
Get<CssBorderBottomStyleProperty>().TrySetValue(m.Item2);
Get<CssBorderBottomColorProperty>().TrySetValue(m.Item3);
});
}
internal override string SerializeValue(IEnumerable<CssProperty> properties)
{
CssBorderLeftColorProperty cssBorderLeftColorProperty = properties.OfType<CssBorderLeftColorProperty>().FirstOrDefault();
CssBorderTopColorProperty cssBorderTopColorProperty = properties.OfType<CssBorderTopColorProperty>().FirstOrDefault();
CssBorderRightColorProperty cssBorderRightColorProperty = properties.OfType<CssBorderRightColorProperty>().FirstOrDefault();
CssBorderBottomColorProperty cssBorderBottomColorProperty = properties.OfType<CssBorderBottomColorProperty>().FirstOrDefault();
if (cssBorderLeftColorProperty == null || cssBorderRightColorProperty == null || cssBorderTopColorProperty == null || cssBorderBottomColorProperty == null)
return string.Empty;
if (cssBorderLeftColorProperty.Value != cssBorderRightColorProperty.Value || cssBorderLeftColorProperty.Value != cssBorderTopColorProperty.Value || cssBorderLeftColorProperty.Value != cssBorderBottomColorProperty.Value)
return string.Empty;
CssBorderLeftWidthProperty cssBorderLeftWidthProperty = properties.OfType<CssBorderLeftWidthProperty>().FirstOrDefault();
CssBorderTopWidthProperty cssBorderTopWidthProperty = properties.OfType<CssBorderTopWidthProperty>().FirstOrDefault();
CssBorderRightWidthProperty cssBorderRightWidthProperty = properties.OfType<CssBorderRightWidthProperty>().FirstOrDefault();
CssBorderBottomWidthProperty cssBorderBottomWidthProperty = properties.OfType<CssBorderBottomWidthProperty>().FirstOrDefault();
if (cssBorderLeftWidthProperty == null || cssBorderRightWidthProperty == null || cssBorderTopWidthProperty == null || cssBorderBottomWidthProperty == null)
return string.Empty;
if (cssBorderLeftWidthProperty.Value != cssBorderRightWidthProperty.Value || cssBorderLeftWidthProperty.Value != cssBorderTopWidthProperty.Value || cssBorderLeftWidthProperty.Value != cssBorderBottomWidthProperty.Value)
return string.Empty;
CssBorderLeftStyleProperty cssBorderLeftStyleProperty = properties.OfType<CssBorderLeftStyleProperty>().FirstOrDefault();
CssBorderTopStyleProperty cssBorderTopStyleProperty = properties.OfType<CssBorderTopStyleProperty>().FirstOrDefault();
CssBorderRightStyleProperty cssBorderRightStyleProperty = properties.OfType<CssBorderRightStyleProperty>().FirstOrDefault();
CssBorderBottomStyleProperty cssBorderBottomStyleProperty = properties.OfType<CssBorderBottomStyleProperty>().FirstOrDefault();
if (cssBorderLeftStyleProperty == null || cssBorderRightStyleProperty == null || cssBorderTopStyleProperty == null || cssBorderBottomStyleProperty == null)
return string.Empty;
if (cssBorderLeftStyleProperty.Value != cssBorderRightStyleProperty.Value || cssBorderLeftStyleProperty.Value != cssBorderTopStyleProperty.Value || cssBorderLeftStyleProperty.Value != cssBorderBottomStyleProperty.Value)
return string.Empty;
return SerializeValue(cssBorderLeftWidthProperty, cssBorderLeftStyleProperty, cssBorderLeftColorProperty);
}
internal static string SerializeValue(CssProperty width, CssProperty style, CssProperty color)
{
List<string> list = new List<string>();
if (width != null && width.HasValue)
list.Add(width.SerializeValue());
if (style != null && style.HasValue)
list.Add(style.SerializeValue());
if (color != null && color.HasValue)
list.Add(color.SerializeValue());
return string.Join(" ", list);
}
}
}