Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GrammaticalOptimization/SharpVectorCss/Css/CssCharsetRule.cs @ 13825

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

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

File size: 3.5 KB
Line 
1// <developer>niklas@protocol7.com</developer>
2// <completed>80</completed>
3
4using System;
5using System.Collections.Generic;
6using System.Text.RegularExpressions;
7
8namespace SharpVectors.Dom.Css
9{
10  /// <summary>
11  /// The CSSCharsetRule interface represents a @charset rule in a CSS style sheet. The value of the encoding attribute does not affect the encoding of text data in the DOM objects; this encoding is always UTF-16. After a stylesheet is loaded, the value of the encoding attribute is the value found in the @charset rule. If there was no @charset in the original document, then no CSSCharsetRule is created. The value of the encoding attribute may also be used as a hint for the encoding used on serialization of the style sheet.
12  /// The value of the @charset rule (and therefore of the CSSCharsetRule) may not correspond to the encoding the document actually came in; character encoding information e.g. in an HTTP header, has priority (see CSS document representation) but this is not reflected in the CSSCharsetRule.
13  /// </summary>
14  public class CssCharsetRule : CssRule, ICssCharsetRule
15  {
16    #region Static members
17    private static Regex regex = new Regex(@"^@charset\s""(?<charsetencoding>[^""]+)"";");
18
19        internal static CssRule Parse(ref string css, object parent, bool readOnly, IList<string> replacedStrings, CssStyleSheetType origin)
20    {
21      Match match = regex.Match(css);
22      if(match.Success)
23      {
24        CssCharsetRule rule = new CssCharsetRule(match, parent, readOnly, replacedStrings, origin);
25        css = css.Substring(match.Length);
26        return rule;
27      }
28      else
29      {
30        // didn't match => do nothing
31        return null;
32      }
33    }
34    #endregion
35
36    #region Constructors
37
38    /// <summary>
39    /// The constructor for CssCharSetRule
40    /// </summary>
41    /// <param name="match">The Regex match that found the charset rule</param>
42    /// <param name="parent">The parent rule or parent stylesheet</param>
43    /// <param name="readOnly">True if this instance is readonly</param>
44    /// <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>
45    /// <param name="origin">The type of CssStyleSheet</param>
46        internal CssCharsetRule(Match match, object parent, bool readOnly,
47            IList<string> replacedStrings, CssStyleSheetType origin)
48            : base(parent, readOnly, replacedStrings, origin)
49    {
50      _Encoding = DeReplaceStrings(match.Groups["charsetencoding"].Value);
51    }
52
53    #endregion
54   
55    #region Implementation of ICssCharsetRule
56    private string _Encoding;
57    /// <summary>
58    /// The encoding information used in this @charset rule
59    /// </summary>
60    public string Encoding
61    {
62      get
63      {
64        return _Encoding;
65      }
66      set
67      {
68        /*
69         * TODO: SYNTAX_ERR: Raised if the specified encoding value has a syntax error and is unparsable.
70         * */
71
72        if (readOnly)
73                    throw new DomException(DomExceptionType.NoModificationAllowedErr);
74
75        _Encoding = value;
76      }
77    }
78    #endregion
79
80    #region Implementation of ICssRule
81    /// <summary>
82    /// 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.
83    /// </summary>
84    public override CssRuleType Type
85    {
86      get
87      {
88        return CssRuleType.CharsetRule;
89      }
90    }
91    #endregion
92  }
93}
Note: See TracBrowser for help on using the repository browser.