CssCollectedStyleDeclaration.cs :  » GUI » SharpVectorGraphics » SharpVectors » Dom » Css » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » GUI » SharpVectorGraphics 
SharpVectorGraphics » SharpVectors » Dom » Css » CssCollectedStyleDeclaration.cs
using System;
using System.Xml;
using System.Collections;

#if TEST 
using NUnit.Framework;
#endif

namespace SharpVectors.Dom.Css{
  /// <summary>
  /// Used internally for collection of styles for a specific element
  /// </summary>
  public class CssCollectedStyleDeclaration : CssStyleDeclaration
  {
    #region Contructors
    public CssCollectedStyleDeclaration(XmlElement elm)
    {
      _element = elm;
    }
    #endregion

    #region Private fields
    private XmlElement _element;
    private Hashtable collectedStyles = new Hashtable();
    #endregion

    #region Public methods
    public void CollectProperty(string name, int specificity, CssValue cssValue, CssStyleSheetType origin, string priority)
    {
      CssCollectedProperty newProp = new CssCollectedProperty(name, specificity, cssValue, origin, priority);
    
      if(!collectedStyles.ContainsKey(name))
      {
        collectedStyles[name] = newProp;
      }
      else
      {
        CssCollectedProperty existingProp = (CssCollectedProperty)collectedStyles[name];
        if(newProp.IsBetterThen(existingProp))
        {
          collectedStyles[name] = newProp;
        }
      }
    }

    /// <summary>
    /// Returns the origin type of the collected property
    /// </summary>
    /// <param name="propertyName">The name of the property</param>
    /// <returns>The origin type</returns>
    public CssStyleSheetType GetPropertyOrigin(string propertyName)
    {
      if(collectedStyles.ContainsKey(propertyName))
      {
        CssCollectedProperty scp = (CssCollectedProperty)collectedStyles[propertyName];
        return scp.Origin;
      }
      else
      {
        return CssStyleSheetType.Unknown;
      }
    }

    /// <summary>
    /// Used to retrieve the priority of a CSS property (e.g. the "important" qualifier) if the property has been explicitly set in this declaration block.
    /// </summary>
    /// <param name="propertyName">The name of the CSS property. See the CSS property index.</param>
    /// <returns>A string representing the priority (e.g. "important") if one exists. The empty string if none exists.</returns>
    public override string GetPropertyPriority(string propertyName)
    {
      return (collectedStyles.ContainsKey(propertyName)) ? ((CssCollectedProperty)collectedStyles[propertyName]).Priority : String.Empty;
    }

    private ICssValue getParentStyle(string propertyName)
    {
      CssXmlDocument doc = _element.OwnerDocument as CssXmlDocument;
      XmlElement parentNode = _element.ParentNode as XmlElement;
      if(doc != null && parentNode != null)
      {
        ICssStyleDeclaration parentCsd = doc.GetComputedStyle(parentNode, String.Empty);
        if(parentCsd == null)
        {
          return null;
        }
        else
        {
          return parentCsd.GetPropertyCssValue(propertyName);
        }
      }
      else
      {
        return null;
      }
    }

    public override ICssValue GetPropertyCssValue(string propertyName)
    {
      if(collectedStyles.ContainsKey(propertyName))
      {
        CssCollectedProperty scp = (CssCollectedProperty)collectedStyles[propertyName];
        if(scp.CssValue.CssValueType == CssValueType.Inherit)
        {
          // get style from parent chain
          return getParentStyle(propertyName);
        }
        else
        {
          return scp.CssValue.GetAbsoluteValue(propertyName, _element);
        }
      }
      else
      {
        // should this property inherit?
        CssXmlDocument doc = (CssXmlDocument)_element.OwnerDocument;
        if(doc.CssPropertyProfile.IsInherited(propertyName))
        {
          ICssValue parValue = getParentStyle(propertyName);
          if(parValue != null)
          {
            return parValue;
          }
        }

        string initValue = doc.CssPropertyProfile.GetInitialValue(propertyName);
        if(initValue == null)
        {
          return null;
        }
        else
        {
          return CssValue.GetCssValue(initValue, false).GetAbsoluteValue(propertyName, _element);
        }
      }
    }


    /// <summary>
    /// Used to retrieve the value of a CSS property if it has been explicitly set within this declaration block.
    /// </summary>
    /// <param name="propertyName">The name of the CSS property. See the CSS property index.</param>
    /// <returns>Returns the value of the property if it has been explicitly set for this declaration block. Returns the empty string if the property has not been set.</returns>
    public override string GetPropertyValue(string propertyName)
    {
      CssValue value = (CssValue)GetPropertyCssValue(propertyName);
      if(value != null)
      {
        return value.CssText;
      }
      else
      {
        return String.Empty;
      }
    }


    /// <summary>
    /// The number of properties that have been explicitly set in this declaration block. The range of valid indices is 0 to length-1 inclusive.
    /// </summary>
    public override ulong Length
    {
      get
      {
        return (ulong)collectedStyles.Count;
      }
    }

    /// <summary>
    /// The parsable textual representation of the declaration block (excluding the surrounding curly braces). Setting this attribute will result in the parsing of the new value and resetting of all the properties in the declaration block including the removal or addition of properties.
    /// </summary>
    /// <exception cref="DomException">SYNTAX_ERR: Raised if the specified CSS string value has a syntax error and is unparsable.</exception>
    /// <exception cref="DomException">NO_MODIFICATION_ALLOWED_ERR: Raised if this declaration is readonly or a property is readonly.</exception>
    public override string CssText
    {
      get
      {
        ulong len = Length;
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        for(ulong i = 0; i<len; i++)
        {
          string propName = this[i];
          sb.Append(propName);
          sb.Append(":");
          sb.Append(GetPropertyValue(propName));
          string prio = GetPropertyPriority(propName);
          if(prio.Length > 0)
          {
            sb.Append(" !" + prio);
          }
          sb.Append(";");
        }

        return sb.ToString();
      }
      set
      {
        throw new DomException(DomExceptionType.NoModificationAllowedErr);
      }
    }

    /// <summary>
    /// Used to retrieve the properties that have been explicitly set in this declaration block. The order of the properties retrieved using this method does not have to be the order in which they were set. This method can be used to iterate over all properties in this declaration block.
    /// The name of the property at this ordinal position. The empty string if no property exists at this position.
    /// </summary>
    public override string this[ulong index]
    {
      get
      {
        if(index>=Length) return String.Empty;
        else
        {
          int ind = (int)index;
          IDictionaryEnumerator enu = collectedStyles.GetEnumerator();
          enu.MoveNext();
          for(int i = 0; i<(int)ind; i++) enu.MoveNext();
          return (string)enu.Key;
        }
      }
    }
    #endregion

    #region Unit tests
    #if TEST
    [TestFixture]
    public class CssCollectedStyleDeclarationTests : CssStyleDeclarationTests
    {
      #region Helper methods
      private CssValue _getCssValue(string cssText)
      {
        return CssValue.GetCssValue(cssText, false);
      }

      protected override CssStyleDeclaration getCSD()
      {
        CssCollectedStyleDeclaration csd = new CssCollectedStyleDeclaration(getElm());
        csd.CollectProperty("foo", 1, _getCssValue("bar"), CssStyleSheetType.Author, String.Empty);
        csd.CollectProperty("length", 1, _getCssValue("23cm"), CssStyleSheetType.Author, "important");
        return csd;
      }
      #endregion

      #region Overriden CssStyleDeclaration tests
      [Test]
      public override void TestOrigin()
      {
        CssStyleDeclaration csd = getCSD();
        Assert.AreEqual(CssStyleSheetType.Collector, csd.Origin);
      }

      [Test]
      public override void TestReadWrite()
      {
      }
      #endregion

      #region Collection tests
      [Test]
      public void Collect()
      {
        XmlElement elm = getElm();
        CssCollectedStyleDeclaration csd = new CssCollectedStyleDeclaration(elm);
        Assert.AreEqual(0, csd.Length);
        csd.CollectProperty("foo", 1, _getCssValue("12px"), CssStyleSheetType.Author, "important");
        Assert.AreEqual(1, csd.Length);
        Assert.AreEqual("foo:12px !important;", csd.CssText);

        csd.CollectProperty("bar", 1, _getCssValue("test"), CssStyleSheetType.Author, "");
        Assert.AreEqual(2, csd.Length);
        Assert.AreEqual("foo:12px !important;bar:test;", csd.CssText);

      }

      [Test]
      public void CollectOverrideSameSpecificity()
      {
        XmlElement elm = getElm();
        CssCollectedStyleDeclaration csd = new CssCollectedStyleDeclaration(elm);
        csd.CollectProperty("foo", 1, _getCssValue("value1"), CssStyleSheetType.Author, "");
        csd.CollectProperty("foo", 1, _getCssValue("value2"), CssStyleSheetType.Author, "");
        Assert.AreEqual("foo:value2;", csd.CssText);
      }

      [Test]
      public void CollectOverrideHigherSpecificity()
      {
        XmlElement elm = getElm();
        CssCollectedStyleDeclaration csd = new CssCollectedStyleDeclaration(elm);
        csd.CollectProperty("foo", 1, _getCssValue("value1"), CssStyleSheetType.Author, "");
        csd.CollectProperty("foo", 100, _getCssValue("value2"), CssStyleSheetType.Author, "");
        Assert.AreEqual("foo:value2;", csd.CssText);
      }

      [Test]
      public void CollectOverrideLowerSpecificity()
      {
        XmlElement elm = getElm();
        CssCollectedStyleDeclaration csd = new CssCollectedStyleDeclaration(elm);
        csd.CollectProperty("foo", 100, _getCssValue("value1"), CssStyleSheetType.Author, "");
        csd.CollectProperty("foo", 1, _getCssValue("value2"), CssStyleSheetType.Author, "");
        Assert.AreEqual("foo:value1;", csd.CssText);
      }

      [Test]
      public void CollectOverrideImportant1()
      {
        XmlElement elm = getElm();
        CssCollectedStyleDeclaration csd = new CssCollectedStyleDeclaration(elm);
        csd.CollectProperty("foo", 100, _getCssValue("value1"), CssStyleSheetType.Author, "");
        csd.CollectProperty("foo", 1, _getCssValue("value2"), CssStyleSheetType.Author, "important");
        Assert.AreEqual("foo:value2 !important;", csd.CssText);
      }

      [Test]
      public void CollectOverrideImportant2()
      {
        XmlElement elm = getElm();
        CssCollectedStyleDeclaration csd = new CssCollectedStyleDeclaration(elm);
        csd.CollectProperty("foo", 1, _getCssValue("value1"), CssStyleSheetType.User, "important");
        csd.CollectProperty("foo", 100, _getCssValue("value2"), CssStyleSheetType.User, "");
        Assert.AreEqual("foo:value1 !important;", csd.CssText);
      }

      [Test]
      public void CollectOverrideImportant3()
      {
        XmlElement elm = getElm();
        CssCollectedStyleDeclaration csd = new CssCollectedStyleDeclaration(elm);
        csd.CollectProperty("foo", 1, _getCssValue("value1"), CssStyleSheetType.Author, "important");
        csd.CollectProperty("foo", 1, _getCssValue("value2"), CssStyleSheetType.Author, "important");
        Assert.AreEqual("foo:value2 !important;", csd.CssText);
      }

      [Test]
      public void CollectOverrideImportantUserAgent()
      {
        XmlElement elm = getElm();
        CssCollectedStyleDeclaration csd = new CssCollectedStyleDeclaration(elm);
        csd.CollectProperty("foo", 100, _getCssValue("value1"), CssStyleSheetType.UserAgent, "");
        csd.CollectProperty("foo", 1, _getCssValue("value2"), CssStyleSheetType.UserAgent, "important");
        Assert.AreEqual("foo:value1;", csd.CssText);
      }

      [Test]
      public void CollectOverrideUserAgentVsUser()
      {
        XmlElement elm = getElm();
        CssCollectedStyleDeclaration csd = new CssCollectedStyleDeclaration(elm);
        csd.CollectProperty("foo", 100, _getCssValue("value1"), CssStyleSheetType.UserAgent, "");
        csd.CollectProperty("foo", 1, _getCssValue("value2"), CssStyleSheetType.User, "");
        Assert.AreEqual("foo:value2;", csd.CssText);

        csd.CollectProperty("foo", 100, _getCssValue("value1"), CssStyleSheetType.UserAgent, "");
        Assert.AreEqual("foo:value2;", csd.CssText);

        csd.CollectProperty("foo", 100, _getCssValue("value1"), CssStyleSheetType.UserAgent, "important");
        Assert.AreEqual("foo:value2;", csd.CssText);
      }

      [Test]
      public void CollectOverrideUserAgentVsAuthor()
      {
        XmlElement elm = getElm();
        CssCollectedStyleDeclaration csd = new CssCollectedStyleDeclaration(elm);
        csd.CollectProperty("foo", 100, _getCssValue("value1"), CssStyleSheetType.UserAgent, "");
        csd.CollectProperty("foo", 1, _getCssValue("value2"), CssStyleSheetType.Author, "");
        Assert.AreEqual("foo:value2;", csd.CssText);

        csd.CollectProperty("foo", 100, _getCssValue("value1"), CssStyleSheetType.UserAgent, "");
        Assert.AreEqual("foo:value2;", csd.CssText);

        csd.CollectProperty("foo", 100, _getCssValue("value1"), CssStyleSheetType.UserAgent, "important");
        Assert.AreEqual("foo:value2;", csd.CssText);
      }

      [Test]
      public void CollectOverrideUserVsAuthor()
      {
        XmlElement elm = getElm();
        CssCollectedStyleDeclaration csd = new CssCollectedStyleDeclaration(elm);
        csd.CollectProperty("foo", 100, _getCssValue("value1"), CssStyleSheetType.User, "");
        csd.CollectProperty("foo", 1, _getCssValue("value2"), CssStyleSheetType.Author, "");
        Assert.AreEqual("foo:value2;", csd.CssText);

        csd.CollectProperty("foo", 100, _getCssValue("value1"), CssStyleSheetType.User, "");
        Assert.AreEqual("foo:value2;", csd.CssText);

        csd.CollectProperty("foo", 100, _getCssValue("value1"), CssStyleSheetType.User, "important");
        Assert.AreEqual("foo:value1 !important;", csd.CssText);

        csd.CollectProperty("foo", 200, _getCssValue("value2"), CssStyleSheetType.Author, "important");
        Assert.AreEqual("foo:value1 !important;", csd.CssText);
      }
      #endregion

      #region Absolute values tests
      [Test]
      public void TestEmsNotFontSize()
      {
        CssCollectedStyleDeclaration csd = new CssCollectedStyleDeclaration(getElm());
        csd.CollectProperty("notFontSize", 1, CssValue.GetCssValue("2em", false), CssStyleSheetType.Author, "");

        CssPrimitiveValue cssPrimValue = csd.GetPropertyCssValue("notFontSize") as CssPrimitiveValue;
        Assert.AreEqual(typeof(CssAbsPrimitiveLengthValue), cssPrimValue.GetType());
        Assert.AreEqual(20, cssPrimValue.GetFloatValue(CssPrimitiveType.Px));
      }

      [Test]
      public void TestEmsFontSize()
      {
        CssCollectedStyleDeclaration csd = new CssCollectedStyleDeclaration(getElm());
        csd.CollectProperty("font-size", 1, CssValue.GetCssValue("2em", false), CssStyleSheetType.Author, "");

        CssPrimitiveValue cssPrimValue = csd.GetPropertyCssValue("font-size") as CssPrimitiveValue;
        Assert.AreEqual(20, cssPrimValue.GetFloatValue(CssPrimitiveType.Px));
      }

      [Test]
      public void TestExsNotFontSize()
      {
        CssCollectedStyleDeclaration csd = new CssCollectedStyleDeclaration(getElm());
        csd.CollectProperty("notFontSize", 1, CssValue.GetCssValue("2ex", false), CssStyleSheetType.Author, "");

        CssPrimitiveValue cssPrimValue = csd.GetPropertyCssValue("notFontSize") as CssPrimitiveValue;
        Assert.AreEqual(10, cssPrimValue.GetFloatValue(CssPrimitiveType.Px));
      }

      [Test]
      public void TestExsFontSize()
      {
        CssCollectedStyleDeclaration csd = new CssCollectedStyleDeclaration(getElm());
        csd.CollectProperty("font-size", 1, CssValue.GetCssValue("2ex", false), CssStyleSheetType.Author, "");

        CssPrimitiveValue cssPrimValue = csd.GetPropertyCssValue("font-size") as CssPrimitiveValue;
        Assert.AreEqual(10, cssPrimValue.GetFloatValue(CssPrimitiveType.Px));
      }

      [Test]
      public void TestFuncAttr()
      {
        XmlElement elm = getElm();
        elm.SetAttribute("kalle", "", "roffe");
        CssCollectedStyleDeclaration csd = new CssCollectedStyleDeclaration(elm);
        csd.CollectProperty("foo", 1, CssValue.GetCssValue("attr(kalle)", false), CssStyleSheetType.Author, "");

        CssPrimitiveValue cssPrimValue = csd.GetPropertyCssValue("foo") as CssPrimitiveValue;
        Assert.IsNotNull(cssPrimValue);
        Assert.AreEqual("roffe", cssPrimValue.GetStringValue());
      }


      #endregion
    }
    #endif
    #endregion
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.