[12762] | 1 | using System;
|
---|
| 2 | using System.Xml;
|
---|
| 3 | using System.Xml.XPath;
|
---|
| 4 | using System.Text.RegularExpressions;
|
---|
| 5 | using SharpVectors.Dom.Stylesheets;
|
---|
| 6 | using System.Collections;
|
---|
| 7 | using System.Collections.Generic;
|
---|
| 8 |
|
---|
| 9 | namespace SharpVectors.Dom.Css
|
---|
| 10 | {
|
---|
| 11 | /// <summary>
|
---|
| 12 | /// The CSSStyleRule interface represents a single rule set in a CSS style sheet.
|
---|
| 13 | /// </summary>
|
---|
| 14 | /// <developer>niklas@protocol7.com</developer>
|
---|
| 15 | /// <completed>80</completed>
|
---|
| 16 | public class CssStyleRule : CssRule,ICssStyleRule
|
---|
| 17 | {
|
---|
| 18 | #region Static members
|
---|
| 19 |
|
---|
| 20 | internal static string nsPattern = @"([A-Za-z\*][A-Za-z0-9]*)?\|";
|
---|
| 21 | internal static string attributeValueCheck = "(?<attname>(" + nsPattern + ")?[a-zA-Z0-9]+)\\s*(?<eqtype>[\\~\\^\\$\\*\\|]?)=\\s*(\"|\')?(?<attvalue>.*?)(\"|\')?";
|
---|
| 22 |
|
---|
| 23 |
|
---|
| 24 | internal static string sSelector = "(?<ns>" + nsPattern + ")?" +
|
---|
| 25 | @"(?<type>([A-Za-z\*][A-Za-z0-9]*))?" +
|
---|
| 26 | @"((?<class>\.[A-Za-z][A-Za-z0-9]*)+)?" +
|
---|
| 27 | @"(?<id>\#[A-Za-z][A-Za-z0-9]*)?" +
|
---|
| 28 | @"((?<predicate>\[\s*(" +
|
---|
| 29 | @"(?<attributecheck>(" + nsPattern + ")?[a-zA-Z0-9]+)" +
|
---|
| 30 | @"|" +
|
---|
| 31 | "(?<attributevaluecheck>" + attributeValueCheck + ")" +
|
---|
| 32 | @")\s*\])+)?" +
|
---|
| 33 | @"((?<pseudoclass>\:[a-z\-]+(\([^\)]+\))?)+)?" +
|
---|
| 34 | @"(?<pseudoelements>(\:\:[a-z\-]+)+)?" +
|
---|
| 35 | @"(?<seperator>(\s*(\+|\>|\~)\s*)|(\s+))?";
|
---|
| 36 | private static string sStyleRule = "^((?<selector>(" + sSelector + @")+)(\s*,\s*)?)+";
|
---|
| 37 | private static Regex regex = new Regex(sStyleRule);
|
---|
| 38 |
|
---|
| 39 | internal static CssRule Parse(ref string css, object parent, bool readOnly,
|
---|
| 40 | IList<string> replacedStrings, CssStyleSheetType origin)
|
---|
| 41 | {
|
---|
| 42 | Match match = regex.Match(css);
|
---|
| 43 | if(match.Success && match.Length > 0)
|
---|
| 44 | {
|
---|
| 45 | CssStyleRule rule = new CssStyleRule(match, parent, readOnly, replacedStrings, origin);
|
---|
| 46 |
|
---|
| 47 | css = css.Substring(match.Length);
|
---|
| 48 |
|
---|
| 49 | rule._Style = new CssStyleDeclaration(ref css, rule, readOnly, origin);
|
---|
| 50 |
|
---|
| 51 | return rule;
|
---|
| 52 | }
|
---|
| 53 | else
|
---|
| 54 | {
|
---|
| 55 | return null;
|
---|
| 56 | }
|
---|
| 57 | }
|
---|
| 58 | #endregion
|
---|
| 59 |
|
---|
| 60 | #region Constructors
|
---|
| 61 |
|
---|
| 62 | /// <summary>
|
---|
| 63 | /// The constructor for CssStyleRule
|
---|
| 64 | /// </summary>
|
---|
| 65 | /// <param name="match">The Regex match that found the charset rule</param>
|
---|
| 66 | /// <param name="parent">The parent rule or parent stylesheet</param>
|
---|
| 67 | /// <param name="readOnly">True if this instance is readonly</param>
|
---|
| 68 | /// <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>
|
---|
| 69 | /// <param name="origin">The type of CssStyleSheet</param>
|
---|
| 70 | internal CssStyleRule(Match match, object parent, bool readOnly,
|
---|
| 71 | IList<string> replacedStrings, CssStyleSheetType origin)
|
---|
| 72 | : base(parent, readOnly, replacedStrings, origin)
|
---|
| 73 | {
|
---|
| 74 | //SelectorText = DeReplaceStrings(match.Groups["selectors"].Value.Trim());
|
---|
| 75 | //_Style = new CssStyleDeclaration(match, this, readOnly, Origin);
|
---|
| 76 |
|
---|
| 77 | Group selectorMatches = match.Groups["selector"];
|
---|
| 78 |
|
---|
| 79 | int len = selectorMatches.Captures.Count;
|
---|
| 80 | ArrayList sels = new ArrayList();
|
---|
| 81 | for(int i = 0; i<len; i++)
|
---|
| 82 | {
|
---|
| 83 | string str = DeReplaceStrings(selectorMatches.Captures[i].Value.Trim());
|
---|
| 84 | if(str.Length > 0)
|
---|
| 85 | {
|
---|
| 86 | sels.Add(new CssXPathSelector(str));
|
---|
| 87 | }
|
---|
| 88 | }
|
---|
| 89 | XPathSelectors = (CssXPathSelector[])sels.ToArray(typeof(CssXPathSelector));
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | #endregion
|
---|
| 93 |
|
---|
| 94 | #region Public methods
|
---|
| 95 | /// <summary>
|
---|
| 96 | /// Used to find matching style rules in the cascading order
|
---|
| 97 | /// </summary>
|
---|
| 98 | /// <param name="elt">The element to find styles for</param>
|
---|
| 99 | /// <param name="pseudoElt">The pseudo-element to find styles for</param>
|
---|
| 100 | /// <param name="ml">The medialist that the document is using</param>
|
---|
| 101 | /// <param name="csd">A CssStyleDeclaration that holds the collected styles</param>
|
---|
| 102 | protected internal override void GetStylesForElement(XmlElement elt, string pseudoElt, MediaList ml, CssCollectedStyleDeclaration csd)
|
---|
| 103 | {
|
---|
| 104 | XPathNavigator nav = elt.CreateNavigator();
|
---|
| 105 | foreach(CssXPathSelector sel in XPathSelectors)
|
---|
| 106 | {
|
---|
| 107 | // TODO: deal with pseudoElt
|
---|
| 108 | if(sel != null && sel.Matches(nav))
|
---|
| 109 | {
|
---|
| 110 | ((CssStyleDeclaration)Style).GetStylesForElement(csd, sel.Specificity);
|
---|
| 111 | break;
|
---|
| 112 | }
|
---|
| 113 | }
|
---|
| 114 | }
|
---|
| 115 | #endregion
|
---|
| 116 |
|
---|
| 117 | #region Private fields
|
---|
| 118 | private CssXPathSelector[] XPathSelectors;
|
---|
| 119 | #endregion
|
---|
| 120 |
|
---|
| 121 | #region Implementation of ICssStyleRule
|
---|
| 122 | /// <summary>
|
---|
| 123 | /// The textual representation of the selector for the rule set. The implementation may have stripped out insignificant whitespace while parsing the selector.
|
---|
| 124 | /// </summary>
|
---|
| 125 | /// <exception cref="DomException">SYNTAX_ERR: Raised if the specified CSS string value has a syntax error and is unparsable.</exception>
|
---|
| 126 | /// <exception cref="DomException">NO_MODIFICATION_ALLOWED_ERR: Raised if this rule is readonly</exception>
|
---|
| 127 | public string SelectorText
|
---|
| 128 | {
|
---|
| 129 | get
|
---|
| 130 | {
|
---|
| 131 | string ret = String.Empty;
|
---|
| 132 | foreach(CssXPathSelector sel in XPathSelectors)
|
---|
| 133 | {
|
---|
| 134 | ret += sel.CssSelector + ",";
|
---|
| 135 | }
|
---|
| 136 | return ret.Substring(0, ret.Length-1);
|
---|
| 137 | }
|
---|
| 138 | set
|
---|
| 139 | {
|
---|
| 140 | // TODO: invalidate
|
---|
| 141 | throw new NotImplementedException("setting SelectorText");
|
---|
| 142 |
|
---|
| 143 | }
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | /// <summary>
|
---|
| 147 | /// The entire text of the CssStyleRule
|
---|
| 148 | /// </summary>
|
---|
| 149 | public override string CssText
|
---|
| 150 | {
|
---|
| 151 | get
|
---|
| 152 | {
|
---|
| 153 | return SelectorText + "{" + ((CssStyleDeclaration)Style).CssText + "}";
|
---|
| 154 | }
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 | private CssStyleDeclaration _Style;
|
---|
| 158 | /// <summary>
|
---|
| 159 | /// The declaration-block of this rule set.
|
---|
| 160 | /// </summary>
|
---|
| 161 | public ICssStyleDeclaration Style
|
---|
| 162 | {
|
---|
| 163 | get
|
---|
| 164 | {
|
---|
| 165 | return _Style;
|
---|
| 166 | }
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 |
|
---|
| 170 |
|
---|
| 171 | #endregion
|
---|
| 172 |
|
---|
| 173 | #region Implementation of ICssRule
|
---|
| 174 | /// <summary>
|
---|
| 175 | /// 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.
|
---|
| 176 | /// </summary>
|
---|
| 177 | public override CssRuleType Type
|
---|
| 178 | {
|
---|
| 179 | get
|
---|
| 180 | {
|
---|
| 181 | return CssRuleType.StyleRule;
|
---|
| 182 | }
|
---|
| 183 | }
|
---|
| 184 | #endregion
|
---|
| 185 |
|
---|
| 186 | }
|
---|
| 187 | }
|
---|