Click here to Skip to main content
15,881,852 members
Articles / Web Development / CSS3

AngleSharp

Rate me:
Please Sign up or sign in to vote.
5.00/5 (87 votes)
3 Jul 2013BSD28 min read 260.5K   4.3K   166  
Bringing the DOM to C# with a HTML5/CSS3 parser written in C#.
using System;
using System.Globalization;

namespace AngleSharp.DOM.Css
{
    /// <summary>
    /// Represents a CSS primitive value.
    /// </summary>
    sealed class CSSPrimitiveValue : CSSValue
    {
        #region Members

        Object data;
        UnitType unit;

        #endregion

        #region ctor

        internal CSSPrimitiveValue(UnitType unitType, String value)
        {
            _type = CssValue.PrimitiveValue;
            SetStringValue(unitType, value);
        }

        internal CSSPrimitiveValue(UnitType unitType, Single value)
        {
            _type = CssValue.PrimitiveValue;
            SetFloatValue(unitType, value);
        }

        internal CSSPrimitiveValue(String unit, Single value)
        {
            _type = CssValue.PrimitiveValue;
            var unitType = ConvertStringToUnitType(unit);
            SetFloatValue(unitType, value);
        }

        internal CSSPrimitiveValue(HtmlColor value)
        {
            _text = value.ToCss();
            _type = CssValue.PrimitiveValue;
            unit = UnitType.Rgbcolor;
            data = value;
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets the unit type of the value.
        /// </summary>
        public UnitType PrimitiveType
        {
            get { return unit; }
        }

        #endregion

        #region Methods

        /// <summary>
        /// Sets the primitive value to the given number.
        /// </summary>
        /// <param name="unitType">The unit of the number.</param>
        /// <param name="value">The value of the number.</param>
        /// <returns>The CSS primitive value instance.</returns>
        public CSSPrimitiveValue SetFloatValue(UnitType unitType, Single value)
        {
            _text = value.ToString(CultureInfo.InvariantCulture) + ConvertUnitTypeToString(unitType);
            unit = unitType;
            data = value;
            return this;
        }

        /// <summary>
        /// Gets the primitive value's number if any.
        /// </summary>
        /// <param name="unitType">The unit of the number.</param>
        /// <returns>The value of the number if any.</returns>
        public Single? GetFloatValue(UnitType unitType)
        {
            if (data is Single)
            {
                var value = (Single)data;
                //TODO Convert
                return value;
            }

            return null;
        }

        /// <summary>
        /// Sets the primitive value to the given string.
        /// </summary>
        /// <param name="unitType">The unit of the string.</param>
        /// <param name="value">The value of the string.</param>
        /// <returns>The CSS primitive value instance.</returns>
        public CSSPrimitiveValue SetStringValue(UnitType unitType, String value)
        {
            switch (unitType)
            {
                case UnitType.String:
                    _text = "'" + value + "'";
                    break;
                case UnitType.Uri:
                    _text = "url('" + value + "')";
                    break;
                default:
                    _text = value;
                    break;
            }

            unit = unitType;
            data = value;
            return this;
        }

        /// <summary>
        /// Gets the primitive value's string if any.
        /// </summary>
        /// <returns>The value of the string if any.</returns>
        public String GetStringValue()
        {
            if (data is String)
            {
                var value = (String)data;
                //TODO Convert
                return value;
            }

            return null;
        }

        /// <summary>
        /// Gets the primitive value's counter if any.
        /// </summary>
        /// <returns>The value of the counter if any.</returns>
        public Counter GetCounterValue()
        {
            return data as Counter;
        }

        /// <summary>
        /// Gets the primitive value's rectangle if any.
        /// </summary>
        /// <returns>The value of the rectangle if any.</returns>
        public Rect GetRectValue()
        {
            return data as Rect;
        }

        /// <summary>
        /// Gets the primitive value's RGB color if any.
        /// </summary>
        /// <returns>The value of the RGB color if any.</returns>
        public HtmlColor? GetRGBColorValue()
        {
            if(unit == UnitType.Rgbcolor)
                return (HtmlColor)data;

            return null;
        }

        #endregion

        #region Helpers

        internal static UnitType ConvertStringToUnitType(String unit)
        {
            switch (unit)
            {
                case "%": return UnitType.Percentage;
                case "em": return UnitType.Ems;
                case "cm": return UnitType.Cm;
                case "deg": return UnitType.Deg;
                case "grad": return UnitType.Grad;
                case "rad": return UnitType.Rad;
                case "turn": return UnitType.Turn;
                case "ex": return UnitType.Exs;
                case "hz": return UnitType.Hz;
                case "in": return UnitType.In;
                case "khz": return UnitType.Khz;
                case "mm": return UnitType.Mm;
                case "ms": return UnitType.Ms;
                case "s": return UnitType.S;
                case "pc": return UnitType.Pc;
                case "pt": return UnitType.Pt;
                case "px": return UnitType.Px;
                case "vw": return UnitType.Vw;
                case "vh": return UnitType.Vh;
                case "vmin": return UnitType.Vmin;
                case "vmax": return UnitType.Vmax;
            }

            return UnitType.Unknown;
        }

        internal static String ConvertUnitTypeToString(UnitType unit)
        {
            switch (unit)
            {
                case UnitType.Percentage: return "%";
                case UnitType.Ems: return "em";
                case UnitType.Cm: return "cm";
                case UnitType.Deg: return "deg";
                case UnitType.Grad: return "grad";
                case UnitType.Rad: return "rad";
                case UnitType.Turn: return "turn";
                case UnitType.Exs: return "ex";
                case UnitType.Hz: return "hz";
                case UnitType.In: return "in";
                case UnitType.Khz: return "khz";
                case UnitType.Mm: return "mm";
                case UnitType.Ms: return "ms";
                case UnitType.S: return "s";
                case UnitType.Pc: return "pc";
                case UnitType.Pt: return "pt";
                case UnitType.Px: return "px";
                case UnitType.Vw: return "vw";
                case UnitType.Vh: return "vh";
                case UnitType.Vmin: return "vmin";
                case UnitType.Vmax: return "vmax";
            }

            return String.Empty;
        }

        #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


Written By
Chief Technology Officer
Germany Germany
Florian lives in Munich, 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. After graduating with a PhD in theoretical particle Physics he is working as a senior technical consultant in the field of home automation and IoT.

Florian has been giving lectures in C#, HTML5 with CSS3 and JavaScript, software design, and other topics. He is regularly giving talks at user groups, conferences, and companies. He is actively contributing to open-source projects. Florian is the maintainer of AngleSharp, a completely managed browser engine.

Comments and Discussions