1 | // <developer>niklas@protocol7.com</developer>
|
---|
2 | // <completed>100</completed>
|
---|
3 |
|
---|
4 | using System;
|
---|
5 | using System.Collections.Generic;
|
---|
6 | using System.Text.RegularExpressions;
|
---|
7 | using System.Xml;
|
---|
8 |
|
---|
9 | using SharpVectors.Dom.Css;
|
---|
10 | using SharpVectors.Dom.Stylesheets;
|
---|
11 |
|
---|
12 | namespace SharpVectors.Dom.Css
|
---|
13 | {
|
---|
14 | /// <summary>
|
---|
15 | /// The CSSImportRule interface represents a @import rule within a CSS style sheet. The @import rule is used to import style rules from other style sheets.
|
---|
16 | /// </summary>
|
---|
17 | public class CssImportRule : CssRule, ICssImportRule
|
---|
18 | {
|
---|
19 | #region Static members
|
---|
20 | private static Regex regex = new Regex(@"^@import\s(url\()?""(?<importhref>[^""]+)""\)?(\s(?<importmedia>([a-z]+)(\s*,\s*)?)+)?;");
|
---|
21 | internal static CssRule Parse(ref string css, object parent, bool readOnly, IList<string> replacedStrings, CssStyleSheetType origin)
|
---|
22 | {
|
---|
23 | Match match = regex.Match(css);
|
---|
24 | if(match.Success)
|
---|
25 | {
|
---|
26 | CssImportRule rule = new CssImportRule(match, parent, readOnly, replacedStrings, origin);
|
---|
27 | css = css.Substring(match.Length);
|
---|
28 | return rule;
|
---|
29 | }
|
---|
30 | else
|
---|
31 | {
|
---|
32 | // didn't match => do nothing
|
---|
33 | return null;
|
---|
34 | }
|
---|
35 | }
|
---|
36 | #endregion
|
---|
37 |
|
---|
38 | #region Constructors
|
---|
39 | /// <summary>
|
---|
40 | /// The constructor for CssImportRule
|
---|
41 | /// </summary>
|
---|
42 | /// <param name="match">The Regex match that found the charset rule</param>
|
---|
43 | /// <param name="parent">The parent rule or parent stylesheet</param>
|
---|
44 | /// <param name="readOnly">True if this instance is readonly</param>
|
---|
45 | /// <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>
|
---|
46 | /// <param name="origin">The type of CssStyleSheet</param>
|
---|
47 | internal CssImportRule(Match match, object parent, bool readOnly,
|
---|
48 | IList<string> replacedStrings, CssStyleSheetType origin)
|
---|
49 | : base(parent, readOnly, replacedStrings, origin)
|
---|
50 | {
|
---|
51 | media = new MediaList(match.Groups["importmedia"].Value);
|
---|
52 | href = DeReplaceStrings(match.Groups["importhref"].Value);
|
---|
53 |
|
---|
54 | styleSheet = new CssStyleSheet(ResolveOwnerNode(), Href, null,
|
---|
55 | match.Groups["importmedia"].Value, this, origin);
|
---|
56 | }
|
---|
57 | #endregion
|
---|
58 |
|
---|
59 | #region Internal methods
|
---|
60 | /// <summary>
|
---|
61 | /// Used to find matching style rules in the cascading order
|
---|
62 | /// </summary>
|
---|
63 | /// <param name="elt">The element to find styles for</param>
|
---|
64 | /// <param name="pseudoElt">The pseudo-element to find styles for</param>
|
---|
65 | /// <param name="ml">The medialist that the document is using</param>
|
---|
66 | /// <param name="csd">A CssStyleDeclaration that holds the collected styles</param>
|
---|
67 | protected internal override void GetStylesForElement(XmlElement elt, string pseudoElt, MediaList ml, CssCollectedStyleDeclaration csd)
|
---|
68 | {
|
---|
69 | if(media.Matches(ml))
|
---|
70 | {
|
---|
71 | ((CssStyleSheet)StyleSheet).GetStylesForElement(elt, pseudoElt, ml, csd);
|
---|
72 | }
|
---|
73 | }
|
---|
74 | #endregion
|
---|
75 |
|
---|
76 | #region Implementation of ICssImportRule
|
---|
77 | private CssStyleSheet styleSheet;
|
---|
78 | /// <summary>
|
---|
79 | /// The style sheet referred to by this rule, if it has been loaded. The value of this attribute is null if the style sheet has not yet been loaded or if it will not be loaded (e.g. if the style sheet is for a media type not supported by the user agent).
|
---|
80 | /// </summary>
|
---|
81 | public ICssStyleSheet StyleSheet
|
---|
82 | {
|
---|
83 | get
|
---|
84 | {
|
---|
85 | return styleSheet;
|
---|
86 | }
|
---|
87 | }
|
---|
88 |
|
---|
89 | private MediaList media;
|
---|
90 | /// <summary>
|
---|
91 | /// A list of media types for which this style sheet may be used.
|
---|
92 | /// </summary>
|
---|
93 | public IMediaList Media
|
---|
94 | {
|
---|
95 | get
|
---|
96 | {
|
---|
97 | return media;
|
---|
98 | }
|
---|
99 | }
|
---|
100 |
|
---|
101 | private string href;
|
---|
102 | /// <summary>
|
---|
103 | /// The location of the style sheet to be imported. The attribute will not contain the "url(...)" specifier around the URI
|
---|
104 | /// </summary>
|
---|
105 | public string Href
|
---|
106 | {
|
---|
107 | get
|
---|
108 | {
|
---|
109 | return href;
|
---|
110 | }
|
---|
111 | }
|
---|
112 | #endregion
|
---|
113 |
|
---|
114 | #region Implementation of ICssRule
|
---|
115 | /// <summary>
|
---|
116 | /// 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.
|
---|
117 | /// </summary>
|
---|
118 | public override CssRuleType Type
|
---|
119 | {
|
---|
120 | get
|
---|
121 | {
|
---|
122 | return CssRuleType.ImportRule;
|
---|
123 | }
|
---|
124 | }
|
---|
125 | #endregion
|
---|
126 | }
|
---|
127 | }
|
---|