StringMap
Represents a list of DOMTokens.
using AngleSharp.Extensions;
using AngleSharp.Html;
using System;
using System.Collections;
using System.Collections.Generic;
namespace AngleSharp.Dom.Collections
{
internal sealed class StringMap : IStringMap, IEnumerable<KeyValuePair<string, string>>, IEnumerable
{
private readonly string _prefix;
private readonly Element _parent;
public string this[string name] {
get {
return _parent.GetOwnAttribute(_prefix + Check(name));
}
set {
_parent.SetOwnAttribute(_prefix + Check(name), value, false);
}
}
internal StringMap(string prefix, Element parent)
{
_prefix = prefix;
_parent = parent;
}
public void Remove(string name)
{
if (Contains(name))
this[name] = null;
}
public bool Contains(string name)
{
return _parent.HasOwnAttribute(_prefix + Check(name));
}
private static string Check(string name)
{
if (name.StartsWith(TagNames.Xml, StringComparison.OrdinalIgnoreCase))
throw new DomException(DomError.Syntax);
if (name.IndexOf(';') >= 0)
throw new DomException(DomError.Syntax);
for (int i = 0; i < name.Length; i++) {
if (name[i].IsUppercaseAscii())
throw new DomException(DomError.Syntax);
}
return name;
}
public IEnumerator<KeyValuePair<string, string>> GetEnumerator()
{
foreach (IAttr attribute in _parent.Attributes) {
if (attribute.NamespaceUri == null && attribute.Name.StartsWith(_prefix, StringComparison.OrdinalIgnoreCase)) {
string key = attribute.Name.Remove(0, _prefix.Length);
string value = attribute.Value;
yield return new KeyValuePair<string, string>(key, value);
}
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}