#region Copyright 2010-2014 by Roger Knapp, Licensed under the Apache License, Version 2.0 /* Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #endregion using System; using System.Collections.Generic; using System.Web; namespace CSharpTest.Net.Html { /// /// A collection of attributes for an element /// public class XmlLightAttributes : IEnumerable> { private readonly Dictionary _attributes; internal XmlLightAttributes (IEnumerable list) { _attributes = new Dictionary(StringComparer.OrdinalIgnoreCase); int index = 0; foreach (XmlLightAttribute attribute in list) { XmlLightAttribute a = attribute; a.Ordinal = index ++; _attributes.Add(a.Name, a); } } /// /// Returns the number of items in the collection. /// public int Count { get { return _attributes.Count; } } /// /// Gets or Sets the attribute's unencoded text value /// public string this[string name] { get { return HttpUtility.HtmlDecode(_attributes[name].Value); } set { XmlLightAttribute a; if (!_attributes.TryGetValue(name, out a)) { a = new XmlLightAttribute(name); a.Ordinal = _attributes.Count; } else if (a.Quote == XmlQuoteStyle.None || a.Quote == XmlQuoteStyle.Missing) a.Quote = XmlQuoteStyle.Double; a.Value = HttpUtility.HtmlAttributeEncode(value); _attributes[name] = a; } } /// /// Gets the value associated with the specified key. /// /// The name of the attribute to find /// Set to the value of the attribute /// Returns true if the attribute was defined public bool TryGetValue(string name, out string value) { XmlLightAttribute attr; if (_attributes.TryGetValue(name, out attr)) { value = HttpUtility.HtmlDecode(attr.Value); return true; } value = null; return false; } /// Returns true if hte attribute is defined public bool ContainsKey(string name) { return _attributes.ContainsKey(name); } /// /// Returns the names of the attributes in appearance order /// public IEnumerable Keys { get { foreach (XmlLightAttribute a in ByOrdinal) yield return a.Name; } } /// /// Adds a new attribute to the collection /// public void Add(string name, string value) { Check.Assert(_attributes.ContainsKey(name) == false); this[name] = value; } /// /// Removes an item from the collection /// public bool Remove(string name) { if(_attributes.Remove(name)) { int index = 0; foreach (XmlLightAttribute attr in ByOrdinal) { XmlLightAttribute a = attr; a.Ordinal = index++; _attributes[a.Name] = a; } return true; } return false; } private List ByOrdinal { get { List all = new List(_attributes.Values); all.Sort(delegate(XmlLightAttribute a1, XmlLightAttribute a2) { return a1.Ordinal.CompareTo(a2.Ordinal); }); return all; } } /// /// Returns the attributes as a collection /// /// public XmlLightAttribute[] ToArray() { return ByOrdinal.ToArray(); } /// /// Returns an enumerator that iterates through the collection. /// public IEnumerator> GetEnumerator() { foreach (XmlLightAttribute a in ByOrdinal) yield return new KeyValuePair(a.Name, HttpUtility.HtmlDecode(a.Value)); } System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return GetEnumerator(); } } }