using System; using System.Xml; using System.Collections.Generic; using System.Text.RegularExpressions; using SharpVectors.Dom.Stylesheets; namespace SharpVectors.Dom.Css { public abstract class CssRule : ICssRule { #region Constructors protected CssRule(object parent, bool readOnly, IList replacedStrings, CssStyleSheetType origin) { if(parent is CssRule) { _ParentRule = (CssRule)parent; } else if(parent is CssStyleSheet) { _ParentStyleSheet = (CssStyleSheet)parent; } else { throw new Exception("The CssRule constructor can only take a CssRule or CssStyleSheet as it's second argument " + parent.GetType()); } this.origin = origin; this.replacedStrings = replacedStrings; this.readOnly = readOnly; } #endregion #region Private and protected fields /// /// The origin stylesheet type of this rule /// protected CssStyleSheetType origin; private IList replacedStrings; /// /// Specifies the read/write state of the instance /// protected bool readOnly; #endregion #region Private and internal methods private string StringReplaceEvaluator(Match match) { int i = Convert.ToInt32(match.Groups["number"].Value); string r = replacedStrings[i]; if(!match.Groups["quote"].Success) r = r.Trim(new char[2]{'\'', '"'}); return r; } internal string DeReplaceStrings(string s) { Regex re = new Regex(@"(?"")?<<<(?[0-9]+)>>>""?"); return re.Replace(s, new MatchEvaluator(StringReplaceEvaluator)); } /// /// Used to find matching style rules in the cascading order /// /// The element to find styles for /// The pseudo-element to find styles for /// The medialist that the document is using /// A CssStyleDeclaration that holds the collected styles protected internal virtual void GetStylesForElement(XmlElement elt, string pseudoElt, MediaList ml, CssCollectedStyleDeclaration csd) { } #endregion #region Public methods /// /// Finds the owner node of this rule /// /// The owner XmlNode public XmlNode ResolveOwnerNode() { if(ParentRule != null) { return ((CssRule)ParentRule).ResolveOwnerNode(); } else { return ((StyleSheet)ParentStyleSheet).ResolveOwnerNode(); } } #endregion #region Implementation of ICssRule private CssStyleSheet _ParentStyleSheet = null; /// /// The style sheet that contains this rule /// public ICssStyleSheet ParentStyleSheet { get { return _ParentStyleSheet; } } private CssRule _ParentRule = null; /// /// If this rule is contained inside another rule (e.g. a style rule inside an @media block), this is the containing rule. If this rule is not nested inside any other rules, this returns null /// public ICssRule ParentRule { get { return _ParentRule; } } /// /// The type of the rule, as defined above. 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. /// public abstract CssRuleType Type { get; } /// /// The parsable textual representation of the rule. This reflects the current state of the rule and not its initial value. /// public virtual string CssText { get { throw new NotImplementedException("CssText"); //return _CssText; } set { throw new NotImplementedException("CssText"); } } #endregion } }