Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GrammaticalOptimization/SharpVectorCss/Css/CssCollectedStyleDeclaration.cs @ 13825

Last change on this file since 13825 was 12762, checked in by aballeit, 9 years ago

#2283 GUI updates, Tree-chart, MCTS Version 2 (prune leaves)

File size: 7.1 KB
Line 
1using System;
2using System.Xml;
3using System.Collections.Generic;
4
5namespace SharpVectors.Dom.Css
6{
7  /// <summary>
8  /// Used internally for collection of styles for a specific element
9  /// </summary>
10  public class CssCollectedStyleDeclaration : CssStyleDeclaration
11  {
12    #region Contructors
13    public CssCollectedStyleDeclaration(XmlElement elm)
14    {
15      _element = elm;
16    }
17    #endregion
18
19    #region Private fields
20   
21        private XmlElement _element;
22    private Dictionary<string, CssCollectedProperty> collectedStyles =
23            new Dictionary<string, CssCollectedProperty>();
24
25    #endregion
26
27    #region Public methods
28
29    public void CollectProperty(string name, int specificity, CssValue cssValue, CssStyleSheetType origin, string priority)
30    {
31      CssCollectedProperty newProp = new CssCollectedProperty(name, specificity, cssValue, origin, priority);
32   
33      if (!collectedStyles.ContainsKey(name))
34      {
35        collectedStyles[name] = newProp;
36      }
37      else
38      {
39        CssCollectedProperty existingProp = collectedStyles[name];
40        if (newProp.IsBetterThen(existingProp))
41        {
42          collectedStyles[name] = newProp;
43        }
44      }
45    }
46
47    /// <summary>
48    /// Returns the origin type of the collected property
49    /// </summary>
50    /// <param name="propertyName">The name of the property</param>
51    /// <returns>The origin type</returns>
52    public CssStyleSheetType GetPropertyOrigin(string propertyName)
53    {
54      if (collectedStyles.ContainsKey(propertyName))
55      {
56        CssCollectedProperty scp = collectedStyles[propertyName];
57        return scp.Origin;
58      }
59      else
60      {
61        return CssStyleSheetType.Unknown;
62      }
63    }
64
65    /// <summary>
66    /// 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.
67    /// </summary>
68    /// <param name="propertyName">The name of the CSS property. See the CSS property index.</param>
69    /// <returns>A string representing the priority (e.g. "important") if one exists. The empty string if none exists.</returns>
70    public override string GetPropertyPriority(string propertyName)
71    {
72      return (collectedStyles.ContainsKey(propertyName)) ?
73                collectedStyles[propertyName].Priority : String.Empty;
74    }
75
76    private ICssValue getParentStyle(string propertyName)
77    {
78      CssXmlDocument doc = _element.OwnerDocument as CssXmlDocument;
79      XmlElement parentNode = _element.ParentNode as XmlElement;
80      if(doc != null && parentNode != null)
81      {
82        ICssStyleDeclaration parentCsd = doc.GetComputedStyle(parentNode, String.Empty);
83        if(parentCsd == null)
84        {
85          return null;
86        }
87        else
88        {
89          return parentCsd.GetPropertyCssValue(propertyName);
90        }
91      }
92      else
93      {
94        return null;
95      }
96    }
97
98    public override ICssValue GetPropertyCssValue(string propertyName)
99    {
100      if (collectedStyles.ContainsKey(propertyName))
101      {
102        CssCollectedProperty scp = collectedStyles[propertyName];
103        if(scp.CssValue.CssValueType == CssValueType.Inherit)
104        {
105          // get style from parent chain
106          return getParentStyle(propertyName);
107        }
108        else
109        {
110          return scp.CssValue.GetAbsoluteValue(propertyName, _element);
111        }
112      }
113      else
114      {
115        // should this property inherit?
116        CssXmlDocument doc = (CssXmlDocument)_element.OwnerDocument;
117        if(doc.CssPropertyProfile.IsInherited(propertyName))
118        {
119          ICssValue parValue = getParentStyle(propertyName);
120          if(parValue != null)
121          {
122            return parValue;
123          }
124        }
125
126        string initValue = doc.CssPropertyProfile.GetInitialValue(propertyName);
127        if(initValue == null)
128        {
129          return null;
130        }
131        else
132        {
133          return CssValue.GetCssValue(initValue, false).GetAbsoluteValue(propertyName, _element);
134        }
135      }
136    }
137
138
139    /// <summary>
140    /// Used to retrieve the value of a CSS property if it has been explicitly set within this declaration block.
141    /// </summary>
142    /// <param name="propertyName">The name of the CSS property. See the CSS property index.</param>
143    /// <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>
144    public override string GetPropertyValue(string propertyName)
145    {
146      CssValue value = (CssValue)GetPropertyCssValue(propertyName);
147      if(value != null)
148      {
149        return value.CssText;
150      }
151      else
152      {
153        return String.Empty;
154      }
155    }
156
157
158    /// <summary>
159    /// The number of properties that have been explicitly set in this declaration block. The range of valid indices is 0 to length-1 inclusive.
160    /// </summary>
161    public override ulong Length
162    {
163      get
164      {
165        return (ulong)collectedStyles.Count;
166      }
167    }
168
169    /// <summary>
170    /// 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.
171    /// </summary>
172    /// <exception cref="DomException">SYNTAX_ERR: Raised if the specified CSS string value has a syntax error and is unparsable.</exception>
173    /// <exception cref="DomException">NO_MODIFICATION_ALLOWED_ERR: Raised if this declaration is readonly or a property is readonly.</exception>
174    public override string CssText
175    {
176      get
177      {
178        ulong len = Length;
179        System.Text.StringBuilder sb = new System.Text.StringBuilder();
180        for(ulong i = 0; i<len; i++)
181        {
182          string propName = this[i];
183          sb.Append(propName);
184          sb.Append(":");
185          sb.Append(GetPropertyValue(propName));
186          string prio = GetPropertyPriority(propName);
187          if(prio.Length > 0)
188          {
189            sb.Append(" !" + prio);
190          }
191          sb.Append(";");
192        }
193
194        return sb.ToString();
195      }
196      set
197      {
198        throw new DomException(DomExceptionType.NoModificationAllowedErr);
199      }
200    }
201
202    /// <summary>
203    /// 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.
204    /// The name of the property at this ordinal position. The empty string if no property exists at this position.
205    /// </summary>
206    public override string this[ulong index]
207    {
208      get
209      {
210        if (index >= this.Length)
211                    return String.Empty;
212        else
213        {
214                    int ind = (int)index;//Dictionary<string, CssCollectedProperty>
215                    IEnumerator<KeyValuePair<string, CssCollectedProperty>> iterator =
216                        collectedStyles.GetEnumerator();
217                    iterator.MoveNext();
218
219                    KeyValuePair<string, CssCollectedProperty> enu = iterator.Current;
220
221                    for (int i = 0; i < (int)ind; i++)
222                    {
223                        iterator.MoveNext();
224                        enu = iterator.Current;
225                    }
226
227          return enu.Key;
228        }
229      }
230    }
231    #endregion
232  }
233}
Note: See TracBrowser for help on using the repository browser.