4 * Copyright 2010, Moxiecode Systems AB
5 * Released under LGPL License.
7 * License: http://tinymce.moxiecode.com/license
8 * Contributing: http://tinymce.moxiecode.com/contributing
12 * This class is used to write HTML tags out it can be used with the Serializer or the SaxParser.
14 * @class tinymce.html.Writer
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());
20 * @class tinymce.html.Writer
25 * Constructs a new Writer instance.
29 * @param {Object} settings Name/value settings object.
31 tinymce.html.Writer = function(settings) {
32 var html = [], indent, indentBefore, indentAfter, encode, htmlOutput;
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";
43 * Writes the a start element such as <p id="a">.
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 />.
50 start: function(name, attrs, empty) {
51 var i, l, attr, value;
53 if (indent && indentBefore[name] && html.length > 0) {
54 value = html[html.length - 1];
56 if (value.length > 0 && value !== '\n')
63 for (i = 0, l = attrs.length; i < l; i++) {
65 html.push(' ', attr.name, '="', encode(attr.value, true), '"');
69 if (!empty || htmlOutput)
70 html[html.length] = '>';
72 html[html.length] = ' />';
74 if (empty && indent && indentAfter[name] && html.length > 0) {
75 value = html[html.length - 1];
77 if (value.length > 0 && value !== '\n')
83 * Writes the a end element such as </p>.
86 * @param {String} name Name of the element.
91 /*if (indent && indentBefore[name] && html.length > 0) {
92 value = html[html.length - 1];
94 if (value.length > 0 && value !== '\n')
98 html.push('</', name, '>');
100 if (indent && indentAfter[name] && html.length > 0) {
101 value = html[html.length - 1];
103 if (value.length > 0 && value !== '\n')
109 * Writes a text node.
112 * @param {String} text String to write out.
113 * @param {Boolean} raw Optional raw state if true the contents wont get encoded.
115 text: function(text, raw) {
117 html[html.length] = raw ? text : encode(text);
121 * Writes a cdata node such as <![CDATA[data]]>.
124 * @param {String} text String to write out inside the cdata.
126 cdata: function(text) {
127 html.push('<![CDATA[', text, ']]>');
131 * Writes a comment node such as <!-- Comment -->.
134 * @param {String} text String to write out inside the comment.
136 comment: function(text) {
137 html.push('<!--', text, '-->');
141 * Writes a PI node such as <?xml attr="value" ?>.
144 * @param {String} name Name of the pi.
145 * @param {String} text String to write out inside the pi.
147 pi: function(name, text) {
149 html.push('<?', name, ' ', text, '?>');
151 html.push('<?', name, '?>');
158 * Writes a doctype node such as <!DOCTYPE data>.
161 * @param {String} text String to write out inside the doctype.
163 doctype: function(text) {
164 html.push('<!DOCTYPE', text, '>', indent ? '\n' : '');
168 * Resets the internal buffer if one wants to reuse the writer.
177 * Returns the contents that got serialized.
180 * @return {String} HTML contents that got written down.
182 getContent: function() {
183 return html.join('').replace(/\n$/, '');