PageRenderTime 27ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/XamlImageConverter/svg2xaml/SharpVectors/old/Dom/Css/CssStyleDeclaration.cs

http://xamlimageconverter.codeplex.com
C# | 313 lines | 208 code | 38 blank | 67 comment | 21 complexity | 4e839fa06094a3bd39a75a236b166207 MD5 | raw file
Possible License(s): GPL-3.0, AGPL-1.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, LGPL-3.0
  1. using System;
  2. using System.Xml;
  3. using System.Text.RegularExpressions;
  4. using System.Collections;
  5. using SharpVectors.Dom;
  6. namespace SharpVectors.Dom.Css
  7. {
  8. /// <summary>
  9. /// The CSSStyleDeclaration interface represents a single CSS declaration block. This interface may be used to determine the style properties currently set in a block or to set style properties explicitly within the block.
  10. /// While an implementation may not recognize all CSS properties within a CSS declaration block, it is expected to provide access to all specified properties in the style sheet through the CSSStyleDeclaration interface. Furthermore, implementations that support a specific level of CSS should correctly handle CSS shorthand properties for that level. For a further discussion of shorthand properties, see the CSS2Properties interface.
  11. /// This interface is also used to provide a read-only access to the computed values of an element. See also the ViewCSS interface.
  12. /// Note: The CSS Object Model doesn't provide an access to the specified or actual values of the CSS cascade
  13. /// </summary>
  14. /// <developer>niklas@protocol7.com</developer>
  15. /// <completed>80</completed>
  16. public class CssStyleDeclaration : ICssStyleDeclaration
  17. {
  18. #region Static members
  19. private static Regex styleRegex = new Regex(@"^(?<name>[A-Za-z\-0-9]+)\s*:(?<value>[^;\}!]+)(!\s?(?<priority>important))?;?");
  20. #endregion
  21. #region Constructors
  22. /// <summary>
  23. /// The constructor used internally when collecting styles for a specified element
  24. /// </summary>
  25. internal CssStyleDeclaration()
  26. {
  27. _origin = CssStyleSheetType.Collector;
  28. _readOnly = true;
  29. _parentRule = null;
  30. }
  31. /// <summary>
  32. /// The constructor for CssStyleDeclaration
  33. /// </summary>
  34. /// <param name="css">The string to parse for CSS</param>
  35. /// <param name="parentRule">The parent rule or parent stylesheet</param>
  36. /// <param name="readOnly">True if this instance is readonly</param>
  37. /// <param name="origin">The type of CssStyleSheet</param>
  38. public CssStyleDeclaration(ref string css, CssRule parentRule, bool readOnly, CssStyleSheetType origin)
  39. {
  40. _origin = origin;
  41. _readOnly = readOnly;
  42. _parentRule = parentRule;
  43. css = _parseString(css);
  44. }
  45. public CssStyleDeclaration(string css, CssRule parentRule, bool readOnly, CssStyleSheetType origin)
  46. {
  47. _origin = origin;
  48. _readOnly = readOnly;
  49. _parentRule = parentRule;
  50. _parseString(css);
  51. }
  52. #endregion
  53. private string _parseString(string cssText)
  54. {
  55. bool startedWithABracket = false;
  56. cssText = cssText.Trim();
  57. if(cssText.StartsWith("{"))
  58. {
  59. cssText = cssText.Substring(1).Trim();
  60. startedWithABracket = true;
  61. }
  62. Match match = styleRegex.Match(cssText);
  63. while(match.Success)
  64. {
  65. string name = match.Groups["name"].Value;
  66. string value = match.Groups["value"].Value;
  67. if(_parentRule != null)
  68. {
  69. value = ((CssRule)_parentRule).DeReplaceStrings(value);
  70. }
  71. string prio = match.Groups["priority"].Value;
  72. SharpCssStyle style = new SharpCssStyle(name, value, prio, _origin);
  73. bool addStyle = false;
  74. if(styles.ContainsKey(name))
  75. {
  76. string existingPrio = ((SharpCssStyle)styles[name]).Priority;
  77. if(existingPrio != "important" ||
  78. prio == "important")
  79. {
  80. styles.Remove(name);
  81. addStyle = true;
  82. }
  83. }
  84. else
  85. {
  86. addStyle = true;
  87. }
  88. if(addStyle)
  89. {
  90. styles.Add(name, style);
  91. }
  92. cssText = cssText.Substring(match.Length).Trim();
  93. match = styleRegex.Match(cssText);
  94. }
  95. cssText = cssText.Trim();
  96. if(cssText.StartsWith("}"))
  97. {
  98. cssText = cssText.Substring(1);
  99. }
  100. else if(startedWithABracket)
  101. {
  102. throw new DomException(DomExceptionType.SyntaxErr, "Style declaration ending bracket missing");
  103. }
  104. return cssText;
  105. }
  106. #region Private fields
  107. private CssStyleSheetType _origin;
  108. private bool _readOnly;
  109. private Hashtable styles = new Hashtable();
  110. #endregion
  111. public bool ReadOnly
  112. {
  113. get
  114. {
  115. return _readOnly;
  116. }
  117. }
  118. public CssStyleSheetType Origin
  119. {
  120. get
  121. {
  122. return _origin;
  123. }
  124. }
  125. #region Public methods
  126. /// <summary>
  127. /// Used to find matching style rules in the cascading order
  128. /// </summary>
  129. internal void GetStylesForElement(CssCollectedStyleDeclaration csd, int specificity)
  130. {
  131. foreach(DictionaryEntry de in styles)
  132. {
  133. SharpCssStyle scs = (SharpCssStyle)de.Value;
  134. csd.CollectProperty(scs.Name, specificity, (CssValue)GetPropertyCssValue(scs.Name), scs.Origin, scs.Priority);
  135. }
  136. }
  137. #endregion
  138. #region Implementation of ICssStyleDeclaration
  139. /// <summary>
  140. /// Used to set a property value and priority within this declaration block
  141. /// </summary>
  142. /// <param name="propertyName">The name of the CSS property. See the CSS property index.</param>
  143. /// <param name="value">The new value of the property.</param>
  144. /// <param name="priority">The new priority of the property (e.g. "important").</param>
  145. /// <exception cref="DomException">SYNTAX_ERR: Raised if the specified value has a syntax error and is unparsable.</exception>
  146. /// <exception cref="DomException">NO_MODIFICATION_ALLOWED_ERR: Raised if this declaration is readonly or the property is readonly.</exception>
  147. public void SetProperty(string propertyName, string value, string priority)
  148. {
  149. if(ReadOnly) throw new DomException(DomExceptionType.NoModificationAllowedErr);
  150. styles[propertyName] = new SharpCssStyle(propertyName, value, priority, _origin);
  151. }
  152. /// <summary>
  153. /// Used to retrieve the priority of a CSS property (e.g. the "important" qualifier) if the property has been explicitly set in this declaration block.
  154. /// </summary>
  155. /// <param name="propertyName">The name of the CSS property. See the CSS property index.</param>
  156. /// <returns>A string representing the priority (e.g. "important") if one exists. The empty string if none exists.</returns>
  157. public virtual string GetPropertyPriority(string propertyName)
  158. {
  159. return (styles.ContainsKey(propertyName)) ? ((SharpCssStyle)styles[propertyName]).Priority : String.Empty;
  160. }
  161. /// <summary>
  162. /// Used to remove a CSS property if it has been explicitly set within this declaration block.
  163. /// </summary>
  164. /// <param name="propertyName">The name of the CSS property. See the CSS property index.</param>
  165. /// <returns>Returns the value of the property if it has been explicitly set for this declaration block. Returns the empty string if the property has not been set or the property name does not correspond to a known CSS property.</returns>
  166. /// <exception cref="DomException">NO_MODIFICATION_ALLOWED_ERR: Raised if this declaration is readonly or the property is readonly.</exception>
  167. public string RemoveProperty(string propertyName)
  168. {
  169. if(ReadOnly) throw new DomException(DomExceptionType.NoModificationAllowedErr);
  170. if(styles.ContainsKey(propertyName))
  171. {
  172. SharpCssStyle s = (SharpCssStyle)styles[propertyName];
  173. styles.Remove(propertyName);
  174. return s.Value;
  175. }
  176. else return String.Empty;
  177. }
  178. /// <summary>
  179. /// Used to retrieve the object representation of the value of a CSS property if it has been explicitly set within this declaration block. This method returns null if the property is a shorthand property. Shorthand property values can only be accessed and modified as strings, using the getPropertyValue and setProperty methods.
  180. /// </summary>
  181. /// <param name="propertyName">The name of the CSS property. See the CSS property index.</param>
  182. /// <returns>Returns the value of the property if it has been explicitly set for this declaration block. Returns null if the property has not been set.</returns>
  183. public virtual ICssValue GetPropertyCssValue(string propertyName)
  184. {
  185. if(styles.ContainsKey(propertyName))
  186. {
  187. SharpCssStyle scs = (SharpCssStyle)styles[propertyName];
  188. if(scs.CssValue == null)
  189. {
  190. scs.CssValue = CssValue.GetCssValue(scs.Value, ReadOnly);
  191. }
  192. return scs.CssValue;
  193. }
  194. else
  195. {
  196. return null;
  197. }
  198. }
  199. /// <summary>
  200. /// Used to retrieve the value of a CSS property if it has been explicitly set within this declaration block.
  201. /// </summary>
  202. /// <param name="propertyName">The name of the CSS property. See the CSS property index.</param>
  203. /// <returns>Returns the value of the property if it has been explicitly set for this declaration block. Returns the empty string if the property has not been set.</returns>
  204. public virtual string GetPropertyValue(string propertyName)
  205. {
  206. return (styles.ContainsKey(propertyName)) ? ((SharpCssStyle)styles[propertyName]).Value : String.Empty;
  207. }
  208. private ICssRule _parentRule;
  209. /// <summary>
  210. /// The CSS rule that contains this declaration block or null if this CSSStyleDeclaration is not attached to a CSSRule.
  211. /// </summary>
  212. public ICssRule ParentRule
  213. {
  214. get
  215. {
  216. return _parentRule;
  217. }
  218. }
  219. /// <summary>
  220. /// The number of properties that have been explicitly set in this declaration block. The range of valid indices is 0 to length-1 inclusive.
  221. /// </summary>
  222. public virtual double Length
  223. {
  224. get
  225. {
  226. return (double)styles.Count;
  227. }
  228. }
  229. /// <summary>
  230. /// The parsable textual representation of the declaration block (excluding the surrounding curly braces). Setting this attribute will result in the parsing of the new value and resetting of all the properties in the declaration block including the removal or addition of properties.
  231. /// </summary>
  232. /// <exception cref="DomException">SYNTAX_ERR: Raised if the specified CSS string value has a syntax error and is unparsable.</exception>
  233. /// <exception cref="DomException">NO_MODIFICATION_ALLOWED_ERR: Raised if this declaration is readonly or a property is readonly.</exception>
  234. public virtual string CssText
  235. {
  236. get
  237. {
  238. string ret = String.Empty;
  239. IDictionaryEnumerator enu = styles.GetEnumerator();
  240. while(enu.MoveNext())
  241. {
  242. SharpCssStyle style = (SharpCssStyle)enu.Value;
  243. ret += style.CssText + ";";
  244. }
  245. return ret;
  246. }
  247. set
  248. {
  249. throw new NotImplementedException();
  250. /*if(ReadOnly) throw new DomException("NO_MODIFICATION_ALLOWED_ERR");
  251. else _Csstext = value;*/
  252. }
  253. }
  254. /// <summary>
  255. /// Used to retrieve the properties that have been explicitly set in this declaration block. The order of the properties retrieved using this method does not have to be the order in which they were set. This method can be used to iterate over all properties in this declaration block.
  256. /// The name of the property at this ordinal position. The empty string if no property exists at this position.
  257. /// </summary>
  258. public virtual string this[double index]
  259. {
  260. get
  261. {
  262. if(index>=Length) return String.Empty;
  263. else
  264. {
  265. int ind = (int)index;
  266. IDictionaryEnumerator enu = styles.GetEnumerator();
  267. enu.MoveNext();
  268. for(int i = 0; i<ind; i++) enu.MoveNext();
  269. return (string)enu.Key;
  270. }
  271. }
  272. }
  273. #endregion
  274. }
  275. }