Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GrammaticalOptimization/SharpVectorCss/Css/CssMediaRule.cs @ 13791

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

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

File size: 6.1 KB
Line 
1// <developer>niklas@protocol7.com</developer>
2// <completed>80</completed>
3
4using System;
5using System.Xml;
6using System.Text.RegularExpressions;
7using System.Collections.Generic;
8
9using SharpVectors.Dom.Stylesheets;
10
11namespace SharpVectors.Dom.Css
12{
13  /// <summary>
14  /// The CSSMediaRule interface represents a @media rule in a CSS style sheet. A @media rule can be used to delimit style rules for specific media types.
15  /// </summary>
16  public class CssMediaRule : CssRule, ICssMediaRule
17  {
18    #region Static members
19
20    private static Regex regex = new Regex(@"^@media\s(?<medianames>([a-z]+(\s*,\s*)?)+)");
21   
22    internal static CssRule Parse(ref string css, object parent, bool readOnly, string[] replacedStrings, CssStyleSheetType origin)
23    {
24      Match match = regex.Match(css);
25      if(match.Success)
26      {
27        CssMediaRule rule = new CssMediaRule(match, parent, readOnly, replacedStrings, origin);
28
29        css = css.Substring(match.Length);
30
31        rule.cssRules = new CssRuleList(ref css, rule, replacedStrings, origin);
32
33        return rule;
34      }
35      else
36      {
37        return null;
38      }
39    }
40   
41    internal static CssRule Parse(ref string css, object parent, bool readOnly, IList<string> replacedStrings, CssStyleSheetType origin)
42    {
43      Match match = regex.Match(css);
44      if(match.Success)
45      {
46        CssMediaRule rule = new CssMediaRule(match, parent, readOnly, replacedStrings, origin);
47
48        css = css.Substring(match.Length);
49
50        rule.cssRules = new CssRuleList(ref css, rule, replacedStrings, origin);
51
52        return rule;
53      }
54      else
55      {
56        return null;
57      }
58    }
59
60    #endregion
61
62    #region Constructors
63    /// <summary>
64    /// The constructor for CssMediaRule
65    /// </summary>
66    /// <param name="match">The Regex match that found the charset rule</param>
67    /// <param name="parent">The parent rule or parent stylesheet</param>
68    /// <param name="readOnly">True if this instance is readonly</param>
69    /// <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>
70    /// <param name="origin">The type of CssStyleSheet</param>
71    internal CssMediaRule(Match match, object parent, bool readOnly,
72            IList<string> replacedStrings, CssStyleSheetType origin)
73            : this(match.Groups["medianames"].Value, parent, readOnly, replacedStrings, origin)
74    {
75    }
76
77    public CssMediaRule(string cssText, object parent, bool readOnly,
78            IList<string> replacedStrings, CssStyleSheetType origin)
79            : base(parent, readOnly, replacedStrings, origin)
80    {
81      media = new MediaList(cssText);
82    }
83
84    #endregion
85
86    #region Public methods
87    /// <summary>
88    /// Used to find matching style rules in the cascading order
89    /// </summary>
90    /// <param name="elt">The element to find styles for</param>
91    /// <param name="pseudoElt">The pseudo-element to find styles for</param>
92    /// <param name="ml">The medialist that the document is using</param>
93    /// <param name="csd">A CssStyleDeclaration that holds the collected styles</param>
94    protected internal override void GetStylesForElement(XmlElement elt, string pseudoElt, MediaList ml, CssCollectedStyleDeclaration csd)
95    {
96      if(media.Matches(ml))
97      {
98        ((CssRuleList)CssRules).GetStylesForElement(elt, pseudoElt, ml, csd);
99      }
100    }
101    #endregion
102
103    #region Implementation of ICssMediaRule
104
105    /// <summary>
106    /// Used to delete a rule from the media block.
107    /// </summary>
108    /// <param name="index">The index within the media block's rule collection of the rule to remove.</param>
109    /// <exception cref="DomException">INDEX_SIZE_ERR: Raised if the specified index does not correspond to a rule in the media rule list.</exception>
110    /// <exception cref="DomException">NO_MODIFICATION_ALLOWED_ERR: Raised if this media rule is readonly.</exception>
111    public void DeleteRule(ulong index)
112    {
113      cssRules.DeleteRule(index);
114    }
115
116    /// <summary>
117    /// Used to insert a new rule into the media block
118    /// </summary>
119    /// <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>
120    /// <param name="index">The index within the media block's rule collection of the rule before which to insert the specified rule. If the specified index is equal to the length of the media blocks's rule collection, the rule will be added to the end of the media block.</param>
121    /// <returns>The index within the media block's rule collection of the newly inserted rule.</returns>
122    /// <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>
123    /// <exception cref="DomException">INDEX_SIZE_ERR: Raised if the specified index is not a valid insertion point.</exception>
124    /// <exception cref="DomException">NO_MODIFICATION_ALLOWED_ERR: Raised if this media rule is readonly</exception>
125    /// <exception cref="DomException">SYNTAX_ERR: Raised if the specified rule has a syntax error and is unparsable</exception>
126    public ulong InsertRule(string rule, ulong index)
127    {
128      throw new NotImplementedException("CssMediaRule.InsertRule()");
129      //return cssRules.InsertRule(rule, index);
130    }
131
132    private CssRuleList cssRules;
133    /// <summary>
134    /// A list of all CSS rules contained within the media block.
135    /// </summary>
136    public ICssRuleList CssRules
137    {
138      get
139      {
140        return cssRules;
141      }
142    }
143
144    private MediaList media;
145    /// <summary>
146    /// A list of media types for this rule
147    /// </summary>
148    public IMediaList Media
149    {
150      get
151      {
152        return media;
153      }
154    }
155    #endregion
156
157    #region Implementation of ICssRule
158    /// <summary>
159    /// The type of the rule. The expectation is that binding-specific casting methods can be used to cast down from an instance of the CSSRule interface to the specific derived interface implied by the type.
160    /// </summary>
161    public override CssRuleType Type
162    {
163      get
164      {
165        return CssRuleType.MediaRule;
166      }
167    }
168    #endregion
169  }
170}
Note: See TracBrowser for help on using the repository browser.