Click here to Skip to main content
12,239,973 members (52,434 online)
Click here to Skip to main content

Stats

82.8K views
2.6K downloads
144 bookmarked
Posted

AngleSharp

, 3 Jul 2013 BSD
Bringing the DOM to C# with a HTML5/CSS3 parser written in C#.
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
ViewModels
Documents
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

Share

About the Author

Florian Rappl
Architect
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.

You may also be interested in...

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