[12762] | 1 | using System;
|
---|
| 2 | using System.Xml;
|
---|
| 3 | using System.Collections.Generic;
|
---|
| 4 | using System.Text.RegularExpressions;
|
---|
| 5 | using System.Diagnostics;
|
---|
| 6 |
|
---|
| 7 | using SharpVectors.Dom.Css;
|
---|
| 8 |
|
---|
| 9 | namespace SharpVectors.Dom.Svg
|
---|
| 10 | {
|
---|
| 11 | /// <summary>
|
---|
| 12 | /// SvgStyleableElement is an extension to the Svg DOM to create a class for all elements that are styleable.
|
---|
| 13 | /// </summary>
|
---|
| 14 | public abstract class SvgStyleableElement : SvgElement, ISvgStylable
|
---|
| 15 | {
|
---|
| 16 | #region Private static fields
|
---|
| 17 | private static Regex isImportant = new Regex(@"!\s*important$");
|
---|
| 18 | #endregion
|
---|
| 19 |
|
---|
| 20 | #region Private Fields
|
---|
| 21 |
|
---|
| 22 | #endregion
|
---|
| 23 |
|
---|
| 24 | #region Constructors
|
---|
| 25 | internal SvgStyleableElement(string prefix, string localname, string ns, SvgDocument doc)
|
---|
| 26 | : base(prefix, localname, ns, doc)
|
---|
| 27 | {
|
---|
| 28 | }
|
---|
| 29 | #endregion
|
---|
| 30 |
|
---|
| 31 | #region ISvgStylable Members
|
---|
| 32 |
|
---|
| 33 | #region ClassName
|
---|
| 34 | private ISvgAnimatedString className;
|
---|
| 35 | public ISvgAnimatedString ClassName
|
---|
| 36 | {
|
---|
| 37 | get
|
---|
| 38 | {
|
---|
| 39 | if (className == null)
|
---|
| 40 | {
|
---|
| 41 | className = new SvgAnimatedString(GetAttribute("class", String.Empty));
|
---|
| 42 | }
|
---|
| 43 | return className;
|
---|
| 44 | }
|
---|
| 45 | }
|
---|
| 46 | #endregion
|
---|
| 47 |
|
---|
| 48 | #region PresentationAttributes
|
---|
| 49 | private Dictionary<string, ICssValue> presentationAttributes = new Dictionary<string, ICssValue>();
|
---|
| 50 | public ICssValue GetPresentationAttribute(string name)
|
---|
| 51 | {
|
---|
| 52 | if (!presentationAttributes.ContainsKey(name))
|
---|
| 53 | {
|
---|
| 54 | ICssValue result;
|
---|
| 55 | string attValue = GetAttribute(name, String.Empty).Trim();
|
---|
| 56 | if (attValue != null && attValue.Length > 0)
|
---|
| 57 | {
|
---|
| 58 | if (isImportant.IsMatch(attValue))
|
---|
| 59 | {
|
---|
| 60 | result = null;
|
---|
| 61 | }
|
---|
| 62 | else
|
---|
| 63 | {
|
---|
| 64 | result = CssValue.GetCssValue(attValue, false);
|
---|
| 65 | }
|
---|
| 66 | }
|
---|
| 67 | else
|
---|
| 68 | {
|
---|
| 69 | result = null;
|
---|
| 70 | }
|
---|
| 71 | presentationAttributes[name] = result;
|
---|
| 72 |
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | return presentationAttributes[name];
|
---|
| 76 | }
|
---|
| 77 | #endregion
|
---|
| 78 | #endregion
|
---|
| 79 |
|
---|
| 80 | #region GetValues
|
---|
| 81 | public string GetPropertyValue(string name)
|
---|
| 82 | {
|
---|
| 83 | return GetComputedStyle(String.Empty).GetPropertyValue(name);
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | public string GetPropertyValue(string name1, string name2)
|
---|
| 87 | {
|
---|
| 88 | string cssString = GetComputedStyle(String.Empty).GetPropertyValue(name1);
|
---|
| 89 | if (cssString == null)
|
---|
| 90 | {
|
---|
| 91 | cssString = GetComputedStyle(String.Empty).GetPropertyValue(name2);
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | return cssString;
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | public override ICssStyleDeclaration GetComputedStyle(string pseudoElt)
|
---|
| 98 | {
|
---|
| 99 | if (cachedCSD == null)
|
---|
| 100 | {
|
---|
| 101 | CssCollectedStyleDeclaration csd = (CssCollectedStyleDeclaration)base.GetComputedStyle(pseudoElt);
|
---|
| 102 |
|
---|
| 103 | IEnumerator<string> cssPropNames = OwnerDocument.CssPropertyProfile.GetAllPropertyNames().GetEnumerator();
|
---|
| 104 | while (cssPropNames.MoveNext())
|
---|
| 105 | {
|
---|
| 106 | string cssPropName = cssPropNames.Current;
|
---|
| 107 | CssValue cssValue = (CssValue)GetPresentationAttribute(cssPropName);
|
---|
| 108 | if (cssValue != null)
|
---|
| 109 | {
|
---|
| 110 | csd.CollectProperty(cssPropName, 0, cssValue,
|
---|
| 111 | CssStyleSheetType.NonCssPresentationalHints, String.Empty);
|
---|
| 112 | }
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | cachedCSD = csd;
|
---|
| 116 | }
|
---|
| 117 | return cachedCSD;
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | #endregion
|
---|
| 121 |
|
---|
| 122 | #region Update handling
|
---|
| 123 | public override void HandleAttributeChange(XmlAttribute attribute)
|
---|
| 124 | {
|
---|
| 125 | if (attribute.NamespaceURI.Length == 0)
|
---|
| 126 | {
|
---|
| 127 | string localName = attribute.LocalName;
|
---|
| 128 | if (presentationAttributes.ContainsKey(localName))
|
---|
| 129 | {
|
---|
| 130 | presentationAttributes.Remove(localName);
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | switch (attribute.LocalName)
|
---|
| 134 | {
|
---|
| 135 | case "class":
|
---|
| 136 | className = null;
|
---|
| 137 | // class changes need to propagate to children and invalidate CSS
|
---|
| 138 | break;
|
---|
| 139 | }
|
---|
| 140 | }
|
---|
| 141 | base.HandleAttributeChange(attribute);
|
---|
| 142 | }
|
---|
| 143 | #endregion
|
---|
| 144 | }
|
---|
| 145 | }
|
---|