File: XhtmlTextWriter.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 (270 lines) | stat: -rw-r--r-- 11,260 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
//------------------------------------------------------------------------------
// <copyright file="XhtmlTextWriter.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//------------------------------------------------------------------------------

namespace System.Web.UI {
    using System;
    using System.Collections;
    using System.Collections.Specialized;
    using System.IO;
    using System.Text;
    using System.Globalization;

    public class XhtmlTextWriter : HtmlTextWriter {

        private Hashtable _commonAttributes = new Hashtable();
        // For _elementSpecificAttributes, each hashtable value is a hashtable.  If an attribute name appears as a key
        // in the hashtable corresponding to an element, the attribute is passed through.
        private Hashtable _elementSpecificAttributes = new Hashtable(StringComparer.CurrentCultureIgnoreCase);
        private Hashtable _suppressCommonAttributes = new Hashtable(StringComparer.CurrentCultureIgnoreCase);
        private XhtmlMobileDocType _docType;

        internal override bool RenderDivAroundHiddenInputs {
            get {
                return false;
            }
        }


        public XhtmlTextWriter(TextWriter writer) : this(writer, DefaultTabString) {
        }


        public XhtmlTextWriter(TextWriter writer, string tabString) : base(writer, tabString) {

            // Common attributes are defined in the xhtml modularization spec.  They are allowed by the writer unless
            // _suppressCommonAttribues["elementName"] is nonnull.
            _commonAttributes.Add("class", true);
            _commonAttributes.Add("id", true);
            _commonAttributes.Add("title", true);
            _commonAttributes.Add("xml:lang", true);

            // Note: "Dir" attribute is included in I18N if the bidirectional text module is included.  Since
            // this is not the case in XHTML-MP, omit the dir attribute.
            // See http://www.wapforum.org/tech/documents/WAP-277-XHTMLMP-20011029-a.pdf and
            // http://www.w3.org/TR/xhtml-modularization/abstract_modules.html.
            // See also VSWhidbey 228858.
            // _commonAttributes.Add("dir", true);

            // Note: style attribute is added in SetDocType in case doctype is XHTML-MP or WML20.  In XHTML Basic, it is disallowed.

            // Initialize dictionary lookup of attributes by element.

            // Structure Module
            // body elt has only common attributes.
            AddRecognizedAttributes("head", "xml:lang");
            _suppressCommonAttributes["head"] = true; // common attributes are disallowed for this elt.
            AddRecognizedAttributes("html", "xml:lang", "version", "xmlns");
            _suppressCommonAttributes["html"] = true; // common attributes are disallowed for this elt.
            AddRecognizedAttributes("title", "xml:lang");
            _suppressCommonAttributes["title"] = true; // common attributes are disallowed for this elt.

            // Text module
            // abbr, acronym, address have only common attributes.
            AddRecognizedAttributes("blockquote", "cite");
            AddRecognizedAttributes("br", "class", "id", "title"); // br allows only core attributes.
            _suppressCommonAttributes["br"] = true;
            // cite, code, dfn, div, em, h1-h6, kbd, p have only common attributes.
            AddRecognizedAttributes("pre", "xml:space");
            AddRecognizedAttributes("q", "cite");
            // samp, span, strong, var have only common attributes.

            // Hypertext module
            AddRecognizedAttributes("a", "accesskey", "charset", "href", "hreflang", "rel", "rev", "tabindex", "type", "title");
            
            // List module
            // dl, dt, dd, ol, ul, li have only common attributes.

            // Basic Forms module
            AddRecognizedAttributes("form", "action", "method", "enctype");
            AddRecognizedAttributes("input", "accesskey", "checked", "maxlength", "name", "size", "src", "tabindex", "type", "value", "title", "disabled");
            AddRecognizedAttributes("label", "accesskey");
            AddRecognizedAttributes("label", "for");
            AddRecognizedAttributes("select", "multiple", "name", "size", "tabindex", "disabled");
            AddRecognizedAttributes("option", "selected", "value");
            AddRecognizedAttributes("textarea", "accesskey", "cols", "name", "rows", "tabindex");

            // Basic Tables module
            // caption has only common attributes.
            AddRecognizedAttributes("table", "summary", "width");
            AddRecognizedAttributes("td", "abbr", "align", "axis", "colspan", "headers", "rowspan", "scope", "valign");
            AddRecognizedAttributes("th", "abbr", "align", "axis", "colspan", "headers", "rowspan", "scope", "valign");
            AddRecognizedAttributes("tr", "align", "valign");

            // Image module
            AddRecognizedAttributes("img", "alt", "height", "longdesc", "src", "width");

            // Object module
            AddRecognizedAttributes("object", "archive", "classid", "codebase", "codetype", "data", "declare", "height", "name",
                                    "standby", "tabindex", "type", "width");
            AddRecognizedAttributes("param", "id", "name", "type", "value", "valuetype");

            // Metainformation module
            AddRecognizedAttributes("meta", "xml:lang", "content", "http-equiv", "name", "scheme");
            _suppressCommonAttributes["meta"] = true; // common attributes are disallowed for this elt.

            // Link module
            AddRecognizedAttributes("link", "charset", "href", "hreflang", "media", "rel", "rev", "type");


            // Base module
            AddRecognizedAttributes("base", "href");
            _suppressCommonAttributes["base"] = true; // common attributes are disallowed for this elt.

            // Partial Forms module
            // fieldset has only common attributes.
            AddRecognizedAttributes("optgroup", "disabled", "label");

            // Partial Legacy module
            AddRecognizedAttributes("ol", "start");
            AddRecognizedAttributes("li", "value");

            // Partial Presentation module
            // b, big, hr, i, small have only common attributes

            // Style module
            AddRecognizedAttributes("style", "xml:lang", "media", "title", "type", "xml:space");
            _suppressCommonAttributes["style"] = true;  // common attributes are disallowed for this elt.

        }


        /// <devdoc>
        /// <para>[To be supplied.]</para>
        /// </devdoc>
        public virtual void AddRecognizedAttribute(string elementName, string attributeName) {
            AddRecognizedAttributes(elementName, attributeName);
        }

        private void AddRecognizedAttributes(string elementName, params string[] attributes) {
            Hashtable eltAttributes = (Hashtable) _elementSpecificAttributes[elementName];
            if (eltAttributes == null) {
                eltAttributes = new Hashtable(StringComparer.CurrentCultureIgnoreCase);
                _elementSpecificAttributes[elementName] = eltAttributes;
            }
            foreach(string attribute in attributes) {
                eltAttributes.Add(attribute, true);
            }
        }


        /// <devdoc>
        /// <para>[To be supplied.]</para>
        /// </devdoc>
        public override bool IsValidFormAttribute(string attributeName) {
            Hashtable formAttributes = (Hashtable)_elementSpecificAttributes["form"];
            return (formAttributes != null) && (formAttributes[attributeName] != null);
        }


        /// <devdoc>
        /// <para>[To be supplied.]</para>
        /// </devdoc>
        protected override bool OnAttributeRender(string name, string value, HtmlTextWriterAttribute key) {
            if (_commonAttributes[name] != null && _suppressCommonAttributes[TagName] == null) {
                return true;
            }

            // TagName is valid when OnAttributeRender is called.
            return _elementSpecificAttributes[TagName] != null && ((Hashtable)_elementSpecificAttributes[TagName])[name] != null;
        }


        /// <devdoc>
        /// <para>[To be supplied.]</para>
        /// </devdoc>
        protected override bool OnStyleAttributeRender(string name,string value, HtmlTextWriterStyle key) {
            if (_docType == XhtmlMobileDocType.XhtmlBasic) {
                return false;
            }

            if (TagName.ToLower(CultureInfo.InvariantCulture).Equals("div") && name.ToLower(CultureInfo.InvariantCulture).Equals("border-collapse")) {
                return false;
            }
            return true;
        }


        /// <devdoc>
        /// <para>[To be supplied.]</para>
        /// </devdoc>
        public virtual void RemoveRecognizedAttribute(string elementName, string attributeName) {
            Hashtable eltAttributes = (Hashtable) _elementSpecificAttributes[elementName];
            if (eltAttributes == null) {
                eltAttributes = new Hashtable(StringComparer.CurrentCultureIgnoreCase);
                _elementSpecificAttributes[elementName] = eltAttributes;
            }

            if (_commonAttributes[attributeName] == null || _suppressCommonAttributes[elementName] != null) {
                // Note: Hashtable::Remove silently continues if the key does not exist.
                eltAttributes.Remove(attributeName);
                return;
            }

            // (...else) This is an edge case.  The call removes a common attribute, so we need to add each common attribute and remove the
            // except the specified one.
            _suppressCommonAttributes[elementName] = true;
            foreach(string key in _commonAttributes.Keys) {
                if (key != attributeName) {
                    eltAttributes.Add(attributeName, true);
                }
            }
        }



        /// <devdoc>
        /// <para>[To be supplied.]</para>
        /// </devdoc>
        public virtual void SetDocType(XhtmlMobileDocType docType) {
            _docType = docType;

            if (docType != XhtmlMobileDocType.XhtmlBasic && _commonAttributes["style"] == null) {
                _commonAttributes.Add("style", true);
            }
        }


        /// <devdoc>
        /// <para>[To be supplied.]</para>
        /// </devdoc>
        public override void WriteBreak()  {
            WriteFullBeginTag("br/");
        }

        protected Hashtable CommonAttributes {
            get {
                return _commonAttributes;
            }
        }

        protected Hashtable ElementSpecificAttributes {
            get {
                return _elementSpecificAttributes;
            }
        }

        protected Hashtable SuppressCommonAttributes {
            get {
                return _suppressCommonAttributes;
            }
        }

    }


    /// <devdoc>
    /// <para>[To be supplied.]</para>
    /// </devdoc>
    public enum XhtmlMobileDocType {

        XhtmlBasic,

        XhtmlMobileProfile,

        Wml20
    }

}