]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/javascript/tiny_mce/classes/html/Writer.js
Release 6.5.0
[Github/sugarcrm.git] / include / javascript / tiny_mce / classes / html / Writer.js
1 /**
2  * Writer.js
3  *
4  * Copyright 2010, Moxiecode Systems AB
5  * Released under LGPL License.
6  *
7  * License: http://tinymce.moxiecode.com/license
8  * Contributing: http://tinymce.moxiecode.com/contributing
9  */
10
11 /**
12  * This class is used to write HTML tags out it can be used with the Serializer or the SaxParser.
13  *
14  * @class tinymce.html.Writer
15  * @example
16  * var writer = new tinymce.html.Writer({indent : true});
17  * var parser = new tinymce.html.SaxParser(writer).parse('<p><br></p>');
18  * console.log(writer.getContent());
19  *
20  * @class tinymce.html.Writer
21  * @version 3.4
22  */
23
24 /**
25  * Constructs a new Writer instance.
26  *
27  * @constructor
28  * @method Writer
29  * @param {Object} settings Name/value settings object.
30  */
31 tinymce.html.Writer = function(settings) {
32         var html = [], indent, indentBefore, indentAfter, encode, htmlOutput;
33
34         settings = settings || {};
35         indent = settings.indent;
36         indentBefore = tinymce.makeMap(settings.indent_before || '');
37         indentAfter = tinymce.makeMap(settings.indent_after || '');
38         encode = tinymce.html.Entities.getEncodeFunc(settings.entity_encoding || 'raw', settings.entities);
39         htmlOutput = settings.element_format == "html";
40
41         return {
42                 /**
43                  * Writes the a start element such as <p id="a">.
44                  *
45                  * @method start
46                  * @param {String} name Name of the element.
47                  * @param {Array} attrs Optional attribute array or undefined if it hasn't any.
48                  * @param {Boolean} empty Optional empty state if the tag should end like <br />.
49                  */
50                 start: function(name, attrs, empty) {
51                         var i, l, attr, value;
52
53                         if (indent && indentBefore[name] && html.length > 0) {
54                                 value = html[html.length - 1];
55
56                                 if (value.length > 0 && value !== '\n')
57                                         html.push('\n');
58                         }
59
60                         html.push('<', name);
61
62                         if (attrs) {
63                                 for (i = 0, l = attrs.length; i < l; i++) {
64                                         attr = attrs[i];
65                                         html.push(' ', attr.name, '="', encode(attr.value, true), '"');
66                                 }
67                         }
68
69                         if (!empty || htmlOutput)
70                                 html[html.length] = '>';
71                         else
72                                 html[html.length] = ' />';
73
74                         if (empty && indent && indentAfter[name] && html.length > 0) {
75                                 value = html[html.length - 1];
76
77                                 if (value.length > 0 && value !== '\n')
78                                         html.push('\n');
79                         }
80                 },
81
82                 /**
83                  * Writes the a end element such as </p>.
84                  *
85                  * @method end
86                  * @param {String} name Name of the element.
87                  */
88                 end: function(name) {
89                         var value;
90
91                         /*if (indent && indentBefore[name] && html.length > 0) {
92                                 value = html[html.length - 1];
93
94                                 if (value.length > 0 && value !== '\n')
95                                         html.push('\n');
96                         }*/
97
98                         html.push('</', name, '>');
99
100                         if (indent && indentAfter[name] && html.length > 0) {
101                                 value = html[html.length - 1];
102
103                                 if (value.length > 0 && value !== '\n')
104                                         html.push('\n');
105                         }
106                 },
107
108                 /**
109                  * Writes a text node.
110                  *
111                  * @method text
112                  * @param {String} text String to write out.
113                  * @param {Boolean} raw Optional raw state if true the contents wont get encoded.
114                  */
115                 text: function(text, raw) {
116                         if (text.length > 0)
117                                 html[html.length] = raw ? text : encode(text);
118                 },
119
120                 /**
121                  * Writes a cdata node such as <![CDATA[data]]>.
122                  *
123                  * @method cdata
124                  * @param {String} text String to write out inside the cdata.
125                  */
126                 cdata: function(text) {
127                         html.push('<![CDATA[', text, ']]>');
128                 },
129
130                 /**
131                  * Writes a comment node such as <!-- Comment -->.
132                  *
133                  * @method cdata
134                  * @param {String} text String to write out inside the comment.
135                  */
136                 comment: function(text) {
137                         html.push('<!--', text, '-->');
138                 },
139
140                 /**
141                  * Writes a PI node such as <?xml attr="value" ?>.
142                  *
143                  * @method pi
144                  * @param {String} name Name of the pi.
145                  * @param {String} text String to write out inside the pi.
146                  */
147                 pi: function(name, text) {
148                         if (text)
149                                 html.push('<?', name, ' ', text, '?>');
150                         else
151                                 html.push('<?', name, '?>');
152
153                         if (indent)
154                                 html.push('\n');
155                 },
156
157                 /**
158                  * Writes a doctype node such as <!DOCTYPE data>.
159                  *
160                  * @method doctype
161                  * @param {String} text String to write out inside the doctype.
162                  */
163                 doctype: function(text) {
164                         html.push('<!DOCTYPE', text, '>', indent ? '\n' : '');
165                 },
166
167                 /**
168                  * Resets the internal buffer if one wants to reuse the writer.
169                  *
170                  * @method reset
171                  */
172                 reset: function() {
173                         html.length = 0;
174                 },
175
176                 /**
177                  * Returns the contents that got serialized.
178                  *
179                  * @method getContent
180                  * @return {String} HTML contents that got written down.
181                  */
182                 getContent: function() {
183                         return html.join('').replace(/\n$/, '');
184                 }
185         };
186 };