File: CssStyleCollection.cs

package info (click to toggle)
mono 6.8.0.105%2Bdfsg-3.3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,284,512 kB
  • sloc: cs: 11,172,132; xml: 2,850,069; ansic: 671,653; cpp: 122,091; perl: 59,366; javascript: 30,841; asm: 22,168; makefile: 20,093; sh: 15,020; python: 4,827; pascal: 925; sql: 859; sed: 16; php: 1
file content (357 lines) | stat: -rw-r--r-- 11,998 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
//------------------------------------------------------------------------------
// <copyright file="CssStyleCollection.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>                                                                
//------------------------------------------------------------------------------

namespace System.Web.UI {
    using System.IO;
    using System.Collections;
    using System.Collections.Specialized;
    using System.Reflection;
    using System.Web.UI;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Security.Permissions;


    /// <devdoc>
    ///    <para>
    ///       The <see langword='CssStyleCollection'/>
    ///       class contains HTML
    ///       cascading-style sheets (CSS) inline style attributes. It automatically parses
    ///       and exposes CSS properties through a dictionary pattern API. Each CSS key can be
    ///       manipulated using a key/value indexed collection.
    ///    </para>
    /// </devdoc>
    public sealed class CssStyleCollection {

        private static readonly Regex _styleAttribRegex = new Regex(
                                                                   "\\G(\\s*(;\\s*)*" +        // match leading semicolons and spaces
                                                                   "(?<stylename>[^:]+?)" +    // match stylename - chars up to the semicolon
                                                                   "\\s*:\\s*" +               // spaces, then the colon, then more spaces
                                                                   "(?<styleval>[^;]*)" +      // now match styleval
                                                                   ")*\\s*(;\\s*)*$",          // match a trailing semicolon and trailing spaces
                                                                   RegexOptions.Singleline | 
                                                                   RegexOptions.Multiline |
                                                                   RegexOptions.ExplicitCapture);    
        private StateBag _state;
        private string _style;

        private IDictionary _table;
        private IDictionary _intTable;


        internal CssStyleCollection() : this(null) {
        }

        /*
         * Constructs an CssStyleCollection given a StateBag.
         */
        internal CssStyleCollection(StateBag state) {
            _state = state;
        }

        /*
         * Automatically adds new keys.
         */

        /// <devdoc>
        ///    <para>
        ///       Gets or sets a specified CSS value.
        ///    </para>
        /// </devdoc>
        public string this[string key] {
            get {
                if (_table == null)
                    ParseString();
                string value = (string)_table[key];

                if (value == null) {
                    HtmlTextWriterStyle style = CssTextWriter.GetStyleKey(key);
                    if (style != (HtmlTextWriterStyle)(-1)) {
                        value = this[style];
                    }
                }

                return value;
            }
            set { 
                Add(key, value); 
            }
        }


        /// <devdoc>
        /// Gets or sets the specified known CSS value.
        /// </devdoc>
        public string this[HtmlTextWriterStyle key] {
            get {
                if (_intTable == null) {
                    return null;
                }
                return (string)_intTable[(int)key];
            }
            set {
                Add(key, value);
            }
        }

        /*
         * Returns a collection of keys.
         */

        /// <devdoc>
        ///    <para>
        ///       Gets a collection of keys to all the styles in the
        ///    <see langword='CssStyleCollection'/>. 
        ///    </para>
        /// </devdoc>
        public ICollection Keys {
            get { 
                if (_table == null)
                    ParseString();

                if (_intTable != null) {
                    // combine the keys into a single table. Note that to preserve existing
                    // behavior, we convert enum values into strings to maintain a homogeneous collection.

                    string[] keys = new string[_table.Count + _intTable.Count];
                    int i = 0;
                    
                    foreach (string s in _table.Keys) {
                        keys[i] = s;
                        i++;
                    }
                    foreach (HtmlTextWriterStyle style in _intTable.Keys) {
                        keys[i] = CssTextWriter.GetStyleName(style);
                        i++;
                    }

                    return keys;
                }
                
                return _table.Keys; 
            }
        }


        /// <devdoc>
        ///    <para>
        ///       Gets the number of items in the <see langword='CssStyleCollection'/>.
        ///    </para>
        /// </devdoc>
        public int Count {
            get { 
                if (_table == null)
                    ParseString();
                return _table.Count + ((_intTable != null) ? _intTable.Count : 0); 
            }
        }


        public string Value {
            get { 
                if (_state == null) {
                    if (_style == null) {
                        _style = BuildString();
                    }
                    return _style;
                }
                return(string)_state["style"];
            }
            set { 
                if (_state == null) {
                    _style = value;
                }
                else {
                    _state["style"] = value;
                }
                _table = null;
            }
        }


        /// <devdoc>
        ///    <para>
        ///       Adds a style to the CssStyleCollection.
        ///    </para>
        /// </devdoc>
        public void Add(string key, string value) {
            if (String.IsNullOrEmpty(key)) {
                throw new ArgumentNullException("key");
            }
            
            if (_table == null)
                ParseString();
            _table[key] = value;

            if (_intTable != null) {
                // Remove from the other table to avoid duplicates.
                HtmlTextWriterStyle style = CssTextWriter.GetStyleKey(key);
                if (style != (HtmlTextWriterStyle)(-1)) {
                    _intTable.Remove(style);
                }
            }

            if (_state != null) {
                // keep style attribute synchronized
                _state["style"] = BuildString();
            }
            _style = null;
        }


        public void Add(HtmlTextWriterStyle key, string value) {
            if (_intTable == null) {
                _intTable = new HybridDictionary();
            }
            _intTable[(int)key] = value;

            string name = CssTextWriter.GetStyleName(key);
            if (name.Length != 0) {
                // Remove from the other table to avoid duplicates.
                if (_table == null)
                    ParseString();

                _table.Remove(name);
            }

            if (_state != null) {
                // keep style attribute synchronized
                _state["style"] = BuildString();
            }
            _style = null;
        }


        /// <devdoc>
        ///    <para>
        ///       Removes a style from the <see langword='CssStyleCollection'/>.
        ///    </para>
        /// </devdoc>
        public void Remove(string key) {
            if (_table == null)
                ParseString();
            if (_table[key] != null) {
                _table.Remove(key);

                if (_state != null) {
                    // keep style attribute synchronized
                    _state["style"] = BuildString();
                }
                _style = null;
            }
        }


        public void Remove(HtmlTextWriterStyle key) {
            if (_intTable == null) {
                return;
            }
            _intTable.Remove((int)key);

            if (_state != null) {
                // keep style attribute synchronized
                _state["style"] = BuildString();
            }
            _style = null;
        }


        /// <devdoc>
        ///    <para>
        ///       Removes all styles from the <see langword='CssStyleCollection'/>.
        ///    </para>
        /// </devdoc>
        public void Clear() {
            _table = null;
            _intTable = null;

            if (_state != null) {
                _state.Remove("style");
            }
            _style = null;
        }

        /*  BuildString
         *  Form the style string from data contained in the 
         *  hash table
         */
        private string BuildString() {
            // if the tables are null, there is nothing to build
            if (((_table == null) || (_table.Count == 0)) &&
                ((_intTable == null) || (_intTable.Count == 0))) {
                return null;
            }

            StringWriter sw = new StringWriter();
            CssTextWriter writer = new CssTextWriter(sw);

            Render(writer);
            return sw.ToString();
        }

        /*  ParseString
         *  Parse the style string and fill the hash table with
         *  corresponding values.  
         */
        private void ParseString() {
            // create a case-insensitive dictionary
            _table = new HybridDictionary(true);

            string s = (_state == null) ? _style : (string)_state["style"];
            if (s != null) {
                Match match;

                if ((match = _styleAttribRegex.Match( s, 0)).Success) {
                    CaptureCollection stylenames = match.Groups["stylename"].Captures;
                    CaptureCollection stylevalues = match.Groups["styleval"].Captures;

                    for (int i = 0; i < stylenames.Count; i++) {
                        String styleName = stylenames[i].ToString();
                        String styleValue = stylevalues[i].ToString();

                        _table[styleName] = styleValue;
                    }
                }
            }
        }


        /// <devdoc>
        /// Render out the attribute collection into a CSS TextWriter. This
        /// effectively renders the value of an inline style attribute.
        /// </devdoc>
        internal void Render(CssTextWriter writer) {
            if (_table != null && _table.Count > 0) {
                foreach (DictionaryEntry entry in _table) {
                    writer.WriteAttribute((string)entry.Key, (string)entry.Value);
                }
            }
            if (_intTable != null && _intTable.Count > 0) {
                foreach (DictionaryEntry entry in _intTable) {
                    writer.WriteAttribute((HtmlTextWriterStyle)entry.Key, (string)entry.Value);
                }
            }
        }

        /// <devdoc>
        /// Render out the attribute collection into a CSS TextWriter. This
        /// effectively renders the value of an inline style attribute.
        /// Used by a Style object to render out its CSS attributes into an HtmlTextWriter.
        /// </devdoc>
        internal void Render(HtmlTextWriter writer) {
            if (_table != null && _table.Count > 0) {
                foreach (DictionaryEntry entry in _table) {
                    writer.AddStyleAttribute((string)entry.Key, (string)entry.Value);
                }
            }
            if (_intTable != null && _intTable.Count > 0) {
                foreach (DictionaryEntry entry in _intTable) {
                    writer.AddStyleAttribute((HtmlTextWriterStyle)entry.Key, (string)entry.Value);
                }
            }
        }
    }
}