Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

AngleSharp

, 3 Jul 2013
Bringing the DOM to C# with a HTML5/CSS3 parser written in C#.
Demo.zip
AngleSharp
Css
Tokens
DOM
Collections
Css
Properties
Rules
Selector
Values
Enums
Exception
Html
Basic
Formatting
Forms
Frames
Head
Lists
Media
Objects
Semantic
Tables
Interfaces
Mathml
Objects
Svg
Various
Xml
Foundation
Attributes
Helpers
Mutations
Html
Tokens
Properties
Stream
Xml
Tokens
packages
ModernUI.WPF.1.0.4
lib
net40
FirstFloor.ModernUI.dll
Microsoft.Windows.Shell.dll
net45
FirstFloor.ModernUI.dll
ModernUI.WPF.1.0.4.nupkg
ModernUI.WPF.1.0.4.nuspec
OxyPlot.Core.2013.1.49.1
lib
portable-net4+sl4+wp71+win8
OxyPlot.dll
OxyPlot.Core.2013.1.49.1.nupkg
OxyPlot.Core.2013.1.49.1.nuspec
OxyPlot.Wpf.2013.1.49.1
lib
NET40
OxyPlot.Wpf.dll
OxyPlot.Xps.dll
NET45
OxyPlot.Wpf.dll
OxyPlot.Xps.dll
OxyPlot.Wpf.2013.1.49.1.nupkg
OxyPlot.Wpf.2013.1.49.1.nuspec
Samples
Logo.ico
Pages
Properties
Settings.settings
ViewModels
Source.zip
Documents
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace AngleSharp.DOM.Collections
{
    /// <summary>
    /// Represents a list of DOMTokens.
    /// </summary>
    [DOM("DOMStringMap")]
    public sealed class DOMStringMap : IEnumerable<KeyValuePair<String, String>>
    {
        #region Constant

        const String PREFIX = "data-";

        #endregion

        #region Members

        Element _parent;

        #endregion

        #region ctor

        /// <summary>
        /// Creates a new map of tokens.
        /// </summary>
        internal DOMStringMap(Element parent)
        {
            _parent = parent;
        }

        #endregion

        #region Index

        /// <summary>
        /// Gets or sets the value for a specified property.
        /// </summary>
        /// <param name="name">The name of the custom attribute property.</param>
        /// <returns>The value of the custom attribute property.</returns>
        public String this[String name]
        {
            get { return _parent.GetAttribute(PREFIX + Check(name)); }
            set { _parent.SetAttribute(PREFIX + Check(name), value); }
        }

        #endregion

        #region Methods

        /// <summary>
        /// Gets the value for a specified property.
        /// </summary>
        /// <param name="prop">The name of the property.</param>
        /// <returns>The value for the specified property name.</returns>
        [DOM("getDataAttr")]
        public String GetDataAttr(String prop)
        {
            return _parent.GetAttribute(PREFIX + Check(prop));
        }

        /// <summary>
        /// Checks if the specified property has been set.
        /// </summary>
        /// <param name="prop">The name of the property.</param>
        /// <returns>True if the property is set, otherwise false.</returns>
        [DOM("hetDataAttr")]
        public bool HasDataAttr(String prop)
        {
            return _parent.HasAttribute(PREFIX + Check(prop));
        }

        /// <summary>
        /// Removes the given property from the list of attributes.
        /// </summary>
        /// <param name="prop">The name of the property.</param>
        /// <returns>The current DOMStringMap.</returns>
        [DOM("removeDataAttr")]
        public DOMStringMap RemoveDataAttr(String prop)
        {
            if(HasDataAttr(prop))
                this[prop] = null;

            return this;
        }

        /// <summary>
        /// Sets the value of the specified property.
        /// </summary>
        /// <param name="prop">The name of the property.</param>
        /// <param name="value">The value of the property.</param>
        /// <returns>The current DOMStringMap.</returns>
        [DOM("setDataAttr")]
        public DOMStringMap SetDataAttr(String prop, String value)
        {
            _parent.SetAttribute(PREFIX + Check(prop), value);
            return this;
        }

        #endregion

        #region Helper

        /// <summary>
        /// Checks if the given name follows the rules (shall not start with xml, only lowercase, no semicolon).
        /// </summary>
        /// <param name="name">The name of the property.</param>
        /// <returns>The name again.</returns>
        String Check(String name)
        {
            if (name.StartsWith("xml", StringComparison.OrdinalIgnoreCase))
                throw new DOMException(ErrorCode.SyntaxError);

            if (name.Contains(Specification.SC))
                throw new DOMException(ErrorCode.SyntaxError);

            for (int i = 0; i < name.Length; i++)
            {
                if (Specification.IsUppercaseAscii(name[i]))
                    throw new DOMException(ErrorCode.SyntaxError);
            }

            return name;
        }

        #endregion

        #region IEnumerable implementation

        /// <summary>
        /// Gets an enumerator over all data- attributes.
        /// </summary>
        /// <returns>The generated enumerator.</returns>
        public IEnumerator<KeyValuePair<String, String>> GetEnumerator()
        {
            foreach (var attr in _parent.Attributes)
            {
                if (attr.Name.StartsWith(PREFIX, StringComparison.OrdinalIgnoreCase))
                    yield return new KeyValuePair<String, String>(attr.Name.Remove(0, PREFIX.Length), attr.Value);
            }
        }

        /// <summary>
        /// Gets the common enumerator.
        /// </summary>
        /// <returns>The generated enumerator.</returns>
        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The BSD License

Share

About the Author

Florian Rappl
Chief Technology Officer
Germany Germany
Florian is from Regensburg, Germany. He started his programming career with Perl. After programming C/C++ for some years he discovered his favorite programming language C#. He did work at Siemens as a programmer until he decided to study Physics. During his studies he worked as an IT consultant for various companies.
 
Florian is also giving lectures in C#, HTML5 with CSS3 and JavaScript, and other topics. Having graduated from University with a Master's degree in theoretical physics he is currently busy doing his PhD in the field of High Performance Computing.
Follow on   Google+

| Advertise | Privacy | Mobile
Web01 | 2.8.140827.1 | Last Updated 4 Jul 2013
Article Copyright 2013 by Florian Rappl
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid