Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GrammaticalOptimization/SharpVectorCss/Css/CssStyleSheet.cs @ 13949

Last change on this file since 13949 was 12762, checked in by aballeit, 9 years ago

#2283 GUI updates, Tree-chart, MCTS Version 2 (prune leaves)

File size: 6.9 KB
Line 
1// <developer>niklas@protocol7.com</developer>
2// <completed>80</completed>
3
4using System;
5using System.Xml;
6using System.Net;
7using System.Text.RegularExpressions;
8using System.Collections.Generic;
9using SharpVectors.Dom.Stylesheets;
10
11namespace SharpVectors.Dom.Css
12{
13  /// <summary>
14  /// The CSSStyleSheet interface is a concrete interface used to represent a CSS style sheet i.e., a style sheet whose content type is "text/css".
15  /// </summary>
16  public class CssStyleSheet : StyleSheet, ICssStyleSheet
17  {
18    #region Constructors
19    /// <summary>
20    /// Constructor for CssStyleSheet
21    /// </summary>
22    /// <param name="pi">The XML processing instruction that references the stylesheet</param>
23    /// <param name="origin">The type of stylesheet</param>
24    internal CssStyleSheet(XmlProcessingInstruction pi, CssStyleSheetType origin) : base(pi)
25    {
26      Origin = origin;
27    }
28
29    /// <summary>
30    /// Constructor for CssStyleSheet
31    /// </summary>
32    /// <param name="styleElement">The XML style element that references the stylesheet</param>
33    /// <param name="origin">The type of stylesheet</param>
34    internal CssStyleSheet(XmlElement styleElement, CssStyleSheetType origin) : base(styleElement)
35    {
36      Origin = origin;
37    }
38
39    /// <summary>
40    /// Constructor for CssStyleSheet
41    /// </summary>
42    /// <param name="ownerNode">The node that owns this stylesheet. E.g. used for getting the BaseUri</param>
43    /// <param name="href">The URL of the stylesheet</param>
44    /// <param name="title">The title of the stylesheet</param>
45    /// <param name="media">List of medias for the stylesheet</param>
46    /// <param name="ownerRule">The rule (e.g. ImportRule) that referenced this stylesheet</param>
47    /// <param name="origin">The type of stylesheet</param>
48    public CssStyleSheet(XmlNode ownerNode, string href, string title, string media,
49            CssRule ownerRule, CssStyleSheetType origin)
50            : base(ownerNode, href, "text/css", title, media)
51    {
52      Origin = origin;
53      this.ownerRule = ownerRule;
54    }
55    #endregion
56
57    #region Public methods
58    /// <summary>
59    /// Used to find matching style rules in the cascading order
60    /// </summary>
61    /// <param name="elt">The element to find styles for</param>
62    /// <param name="pseudoElt">The pseudo-element to find styles for</param>
63    /// <param name="ml">The medialist that the document is using</param>
64    /// <param name="csd">A CssStyleDeclaration that holds the collected styles</param>
65    protected internal override void GetStylesForElement(XmlElement elt, string pseudoElt, MediaList ml, CssCollectedStyleDeclaration csd)
66    {
67      if(((MediaList)Media).Matches(ml))
68      {
69        ((CssRuleList)CssRules).GetStylesForElement(elt, pseudoElt, ml, csd);
70      }
71    }
72    #endregion
73
74    #region Private methods
75
76    private string StringReplaceEvaluator(Match match)
77    {
78      alReplacedStrings.Add(match.Value);
79
80      return "\"<<<" + (alReplacedStrings.Count - 1) + ">>>\"";
81    }
82
83    private string PreProcessContent()
84    {
85      if (SheetContent != null && SheetContent.Length > 0)
86      {
87        // "escape" strings, eg: "foo" => "<<<number>>>"     
88        Regex re = new Regex(@"(""(.|\n)*?[^\\]"")|('(.|\n)*?[^\\]')");
89                alReplacedStrings.Clear();
90        string s = re.Replace(SheetContent, new MatchEvaluator(StringReplaceEvaluator));
91     
92        //ReplacedStrings = alReplacedStrings.ToArray();
93        //alReplacedStrings.Clear();
94
95        // remove comments
96        Regex reComment = new Regex(@"(//.*)|(/\*(.|\n)*?\*/)");
97        s = reComment.Replace(s, String.Empty);
98        return s;
99      }
100      else
101      {
102        return "";
103      }
104    }
105
106    #endregion
107
108    #region Private fields
109    private readonly CssStyleSheetType Origin;
110        private List<string> alReplacedStrings = new List<string>();
111    //private string[] ReplacedStrings;
112    #endregion
113
114    #region Implementation of ICssStyleSheet
115    /// <summary>
116    /// Used to delete a rule from the style sheet.
117    /// </summary>
118    /// <param name="index">The index within the style sheet's rule list of the rule to remove.</param>
119    /// <exception cref="DomException">INDEX_SIZE_ERR: Raised if the specified index does not correspond to a rule in the style sheet's rule list.</exception>
120    /// <exception cref="DomException">NO_MODIFICATION_ALLOWED_ERR: Raised if this style sheet is readonly.</exception>
121    public void DeleteRule(ulong index)
122    {
123      ((CssRuleList)CssRules).DeleteRule(index);
124    }
125
126    /// <summary>
127    /// Used to insert a new rule into the style sheet. The new rule now becomes part of the cascade.
128    /// </summary>
129    /// <param name="rule">The parsable text representing the rule. For rule sets this contains both the selector and the style declaration. For at-rules, this specifies both the at-identifier and the rule content.</param>
130    /// <param name="index">The index within the style sheet's rule list of the rule before which to insert the specified rule. If the specified index is equal to the length of the style sheet's rule collection, the rule will be added to the end of the style sheet.</param>
131    /// <returns>The index within the style sheet's rule collection of the newly inserted rule.</returns>
132    /// <exception cref="DomException">INDEX_SIZE_ERR: Raised if the specified index does not correspond to a rule in the style sheet's rule list.</exception>
133    /// <exception cref="DomException">NO_MODIFICATION_ALLOWED_ERR: Raised if this style sheet is readonly.</exception>
134    /// <exception cref="DomException">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.</exception>
135    /// <exception cref="DomException">SYNTAX_ERR: Raised if the specified rule has a syntax error and is unparsable.</exception>
136    public ulong InsertRule(string rule, ulong index)
137    {
138      throw new NotImplementedException("CssStyleSheet.InsertRule()");
139      //return ((CssRuleList)CssRules).InsertRule(rule, index);
140    }
141
142    private CssRuleList cssRules = null;
143    /// <summary>
144    /// The list of all CSS rules contained within the style sheet. This includes both rule sets and at-rules.
145    /// </summary>
146    public ICssRuleList CssRules
147    {
148      get
149      {
150        if (cssRules == null)
151        {
152          string css = PreProcessContent();
153                    cssRules = new CssRuleList(ref css, this, alReplacedStrings, Origin);
154        }
155
156        return cssRules;
157      }
158      set
159      {
160        throw new NotImplementedException();
161      }
162    }
163
164    private CssRule ownerRule;
165    /// <summary>
166    /// If this style sheet comes from an @import rule, the ownerRule attribute will contain the CSSImportRule. In that case, the ownerNode attribute in the StyleSheet interface will be null. If the style sheet comes from an element or a processing instruction, the ownerRule attribute will be null and the ownerNode attribute will contain the Node.
167    /// </summary>
168    public ICssRule OwnerRule
169    {
170      get
171      {
172        return ownerRule;
173      }
174    }
175    #endregion
176  }
177}
Note: See TracBrowser for help on using the repository browser.