CssRuleList.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 » CssRuleList.cs
using System;
using System.Collections;
using System.Text.RegularExpressions;
using System.Xml;
using SharpVectors.Dom.Stylesheets;

namespace SharpVectors.Dom.Css{

  /// <summary>
  /// The CSSRuleList interface provides the abstraction of an ordered collection of CSS rules.
  /// The items in the CSSRuleList are accessible via an integral index, starting from 0.
  /// </summary>
  /// <developer>niklas@protocol7.com</developer>
  /// <completed>80</completed>
  public class CssRuleList : ICssRuleList
  {
    #region Static members
    /* can take two kind of structures:
     * rule{}
     * rule{}
     * or:
     * {
     *  rule{}
     *  rule{}
     * }
     * */
    private void Parse(ref string css, object parent, bool readOnly, string[] replacedStrings, CssStyleSheetType origin)
    {
      bool withBrackets = false;
      css = css.Trim();
      if(css.StartsWith("{"))
      {
        withBrackets = true;
        css = css.Substring(1);
      }

      while(true)
      {
        css = css.Trim();
        if(css.Length == 0)
        {
          if(withBrackets)
          {
            throw new DomException(DomExceptionType.SyntaxErr, "Style block missing ending bracket");
          }
          break;
        }
        else if(css.StartsWith("}"))
        {
          // end of block;
          css = css.Substring(1);
          break;
        }
        else if(css.StartsWith("@"))
        {
          #region Parse at-rules
          // @-rule
          CssRule rule;

          // creates and parses a CssMediaRule or return null
          rule = CssMediaRule.Parse(ref css, parent, readOnly, replacedStrings, origin);
          if(rule == null)
          {
            // create ImportRule
                        rule = CssImportRule.Parse(ref css, parent, readOnly, replacedStrings, origin);

            if(rule == null)
            {
              // create CharSetRule
              rule = CssCharsetRule.Parse(ref css, parent, readOnly, replacedStrings, origin);

              if(rule == null)
              {
                                rule = CssFontFaceRule.Parse(ref css, parent, readOnly, replacedStrings, origin);

                if(rule == null)
                {
                                    rule = CssPageRule.Parse(ref css, parent, readOnly, replacedStrings, origin);

                  if(rule == null)
                  {
                                        rule = CssUnknownRule.Parse(ref css, parent, readOnly, replacedStrings, origin);
                  }
                }
              }
            }
          }
          InsertRule(rule);
          #endregion
        }
        else
        {
          // must be a selector or error
          CssRule rule = CssStyleRule.Parse(ref css, parent, readOnly, replacedStrings, origin);
          if(rule != null)
          {
            InsertRule(rule);
          }
          else
          {
                        // this is an unknown rule format, possibly a new kind of selector. Try to find the end of it to skip it

            int startBracket = css.IndexOf("{");
            int endBracket = css.IndexOf("}");
            int endSemiColon = css.IndexOf(";");
            int endRule;

            if(endSemiColon > 0 && endSemiColon < startBracket)
            {
              endRule = endSemiColon;
            }
            else
            {
              endRule = endBracket;
            }


            if(endRule > -1)
            {
              css = css.Substring(endRule+1);
            }
            else
            {
              throw new DomException(DomExceptionType.SyntaxErr, "Can not parse the CSS file");
            }
          }

        //}
        }  
      }
    }
    #endregion
    
    #region Constructor
    /// <summary>
    /// Constructor for CssRuleList
    /// </summary>
    /// <param name="parent">The parent rule or parent stylesheet</param>
    /// <param name="cssText">The CSS text containing the rules that will be in this list</param>
    /// <param name="replacedStrings">An array of strings that have been replaced in the string used for matching. These needs to be put back use the DereplaceStrings method</param>
    /// <param name="origin">The type of CssStyleSheet</param>
    internal CssRuleList(ref string cssText, object parent, string[] replacedStrings, CssStyleSheetType origin) : this(ref cssText, parent, replacedStrings, false, origin)
    {
    }

    /// <summary>
    /// Constructor for CssRuleList
    /// </summary>
    /// <param name="parent">The parent rule or parent stylesheet</param>
    /// <param name="cssText">The CSS text containing the rules that will be in this list</param>
    /// <param name="readOnly">True if this instance is readonly</param>
    /// <param name="replacedStrings">An array of strings that have been replaced in the string used for matching. These needs to be put back use the DereplaceStrings method</param>
    /// <param name="origin">The type of CssStyleSheet</param>
    public CssRuleList(ref string cssText, object parent, string[] replacedStrings, bool readOnly, CssStyleSheetType origin)
    {
      Origin = origin;
      ReadOnly = readOnly;
      if(parent is CssRule || parent is CssStyleSheet)
      {
        Parent = parent;
      }
      else
      {
        throw new Exception("The CssRuleList constructor can only take a CssRule or CssStyleSheet as it's first argument " + parent.GetType());
      }
      Parse(ref cssText, parent, readOnly, replacedStrings, origin);
      //AppendRules(cssText, replacedStrings);
    }
    #endregion

    #region Private fields
    private CssStyleSheetType Origin;
    private object Parent;
    private ArrayList cssRules = new ArrayList();
    private bool ReadOnly = false;
    #endregion

    #region Public methods
    /// <summary>
    /// Used to find matching style rules in the cascading order
    /// </summary>
    /// <param name="elt">The element to find styles for</param>
    /// <param name="pseudoElt">The pseudo-element to find styles for</param>
    /// <param name="ml">The medialist that the document is using</param>
    /// <param name="csd">A CssStyleDeclaration that holds the collected styles</param>
    internal void GetStylesForElement(XmlElement elt, string pseudoElt, MediaList ml, CssCollectedStyleDeclaration csd)
    {
      ulong len = Length;
      for(ulong i = 0; i<len; i++)
      {
        CssRule csr = (CssRule)this[i];
        csr.GetStylesForElement(elt, pseudoElt, ml, csd);
      }
    }

    internal ulong InsertRule(CssRule rule, ulong index)
    {
      /* TODO:
       * HIERARCHY_REQUEST_ERR: Raised if the rule cannot be inserted at the specified index e.g. if an @import rule is inserted after a standard rule set or other at-rule.
       * SYNTAX_ERR: Raised if the specified rule has a syntax error and is unparsable
      */
      
      if(ReadOnly) throw new DomException(DomExceptionType.NoModificationAllowedErr);
      
      if(index > Length || index<0) throw new DomException(DomExceptionType.IndexSizeErr);
  
      if(index == Length)
      {
        cssRules.Add(rule);
      }
      else
      {
        cssRules.Insert((int) index, rule);
      }

      return index;
    }

    internal ulong InsertRule(CssRule rule)
    {
      return InsertRule(rule, Length);
    }

    
    /// <summary>
    /// Deletes a rule from the list
    /// </summary>
    /// <param name="index">The index of the rule to delete</param>
    /// <exception cref="DomException">NO_MODIFICATION_ALLOWED_ERR</exception>
    /// <exception cref="DomException">INDEX_SIZE_ERR</exception>
    internal void DeleteRule(ulong index)
    {
      if(ReadOnly) throw new DomException(DomExceptionType.InvalidModificationErr);
      
      if(index >= Length || index<0) throw new DomException(DomExceptionType.IndexSizeErr);

      cssRules.RemoveAt((int)index);
    }

    #endregion

    #region Implementation of ICssRuleList
    /// <summary>
    /// The number of CSSRules in the list. The range of valid child rule indices is 0 to length-1 inclusive
    /// </summary>
    public ulong Length
    {
      get
      {
        return (ulong)cssRules.Count;
      }
    }

    /// <summary>
    ///     Used to retrieve a CSS rule by ordinal index. The order in this collection represents the order of the rules in the CSS style sheet. If index is greater than or equal to the number of rules in the list, this returns null
    /// </summary>
    public SharpVectors.Dom.Css.ICssRule this[ulong index]
    {
      get
      {
        return (index<Length) ? (CssRule)cssRules[(int)index] : null;
      }
      set
      {
      }
    }
    #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.