CssSupportsRule
sealed class CssSupportsRule : CssConditionRule, ICssSupportsRule, ICssConditionRule, ICssGroupingRule, ICssRule
Represents an @supports rule.
using AngleSharp.Css;
using AngleSharp.Extensions;
using AngleSharp.Parser.Css;
using System.Collections.Generic;
using System.Linq;
namespace AngleSharp.Dom.Css
{
internal sealed class CssSupportsRule : CssConditionRule, ICssSupportsRule, ICssConditionRule, ICssGroupingRule, ICssRule
{
public interface ICondition
{
string Text { get; }
bool Check();
}
public sealed class AndCondition : ICondition
{
private readonly ICondition[] _conditions;
public string Text => string.Join(" and ", from m in _conditions
select m.Text);
public AndCondition(IEnumerable<ICondition> conditions)
{
_conditions = conditions.ToArray();
}
public bool Check()
{
ICondition[] conditions = _conditions;
for (int i = 0; i < conditions.Length; i++) {
if (!conditions[i].Check())
return false;
}
return true;
}
}
public sealed class OrCondition : ICondition
{
private readonly ICondition[] _conditions;
public string Text => string.Join(" or ", from m in _conditions
select m.Text);
public OrCondition(IEnumerable<ICondition> conditions)
{
_conditions = conditions.ToArray();
}
public bool Check()
{
ICondition[] conditions = _conditions;
for (int i = 0; i < conditions.Length; i++) {
if (conditions[i].Check())
return true;
}
return false;
}
}
public sealed class NotCondition : ICondition
{
private readonly ICondition _content;
public string Text => "not " + _content.Text;
public NotCondition(ICondition content)
{
_content = content;
}
public bool Check()
{
return !_content.Check();
}
}
private sealed class EmptyCondition : ICondition
{
public string Text => string.Empty;
public bool Check()
{
return true;
}
}
public sealed class DeclarationCondition : ICondition
{
private readonly CssProperty _property;
private readonly ICssValue _value;
public string Text {
get {
string str = _property.IsImportant ? " !important" : string.Empty;
string str2 = _value.CssText + str + ")";
return "(" + _property.Name + ": " + str2;
}
}
public DeclarationCondition(CssProperty property, ICssValue value)
{
_property = property;
_value = value;
}
public bool Check()
{
if (!(_property is CssUnknownProperty))
return _property.TrySetValue(_value);
return false;
}
}
public sealed class GroupCondition : ICondition
{
private readonly ICondition _content;
public string Text => "(" + _content.Text + ")";
public GroupCondition(ICondition content)
{
_content = content;
}
public bool Check()
{
return _content.Check();
}
}
private ICondition _condition;
private static readonly ICondition empty = new EmptyCondition();
public string ConditionText {
get {
return _condition.Text;
}
set {
ICondition condition = CssParser.ParseCondition(value, null);
if (condition == null)
throw new DomException(ErrorCode.Syntax);
_condition = condition;
}
}
public ICondition Condition {
get {
return _condition;
}
set {
_condition = (value ?? empty);
}
}
public bool IsSupported => _condition.Check();
internal CssSupportsRule()
: base(CssRuleType.Supports)
{
_condition = empty;
}
protected override void ReplaceWith(ICssRule rule)
{
base.ReplaceWith(rule);
CssSupportsRule cssSupportsRule = rule as CssSupportsRule;
ConditionText = cssSupportsRule.ConditionText;
}
internal override bool IsValid(RenderDevice device)
{
return true;
}
protected override string ToCss()
{
return "@supports " + ConditionText + " " + base.Rules.ToCssBlock();
}
}
}