CssRuleList
Represents an array like structure containing CSS rules.
using AngleSharp.Extensions;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace AngleSharp.Dom.Css
{
internal sealed class CssRuleList : ICssRuleList, IEnumerable<ICssRule>, IEnumerable
{
private readonly CssNode _parent;
public CssRule this[int index] {
get {
return Nodes.GetItemByIndex(index);
}
}
ICssRule ICssRuleList.this[int index] {
get {
return this[index];
}
}
public bool HasDeclarativeRules => Nodes.Any((CssRule m) => IsDeclarativeRule(m));
public IEnumerable<CssRule> Nodes => _parent.Children.OfType<CssRule>();
public int Length => Nodes.Count();
internal CssRuleList(CssNode parent)
{
_parent = parent;
}
internal void RemoveAt(int index)
{
if (index < 0 || index >= Length)
throw new DomException(DomError.IndexSizeError);
CssRule cssRule = this[index];
if (cssRule.Type == CssRuleType.Namespace && HasDeclarativeRules)
throw new DomException(DomError.InvalidState);
Remove(cssRule);
}
internal void Remove(CssRule rule)
{
if (rule != null)
_parent.RemoveChild(rule);
}
internal void Insert(int index, CssRule rule)
{
if (rule == null)
throw new DomException(DomError.Syntax);
if (rule.Type == CssRuleType.Charset)
throw new DomException(DomError.Syntax);
if (index > Length || index < 0)
throw new DomException(DomError.IndexSizeError);
if (rule.Type == CssRuleType.Namespace && HasDeclarativeRules)
throw new DomException(DomError.InvalidState);
if (index == Length)
_parent.AppendChild(rule);
else
_parent.InsertBefore(this[index], rule);
}
internal void Add(CssRule rule)
{
if (rule != null)
_parent.AppendChild(rule);
}
public IEnumerator<ICssRule> GetEnumerator()
{
return Nodes.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
private static bool IsDeclarativeRule(CssRule rule)
{
CssRuleType type = rule.Type;
if (type != CssRuleType.Import && type != CssRuleType.Charset)
return type != CssRuleType.Namespace;
return false;
}
}
}