4 * Copyright, Moxiecode Systems AB
5 * Released under LGPL License.
7 * License: http://www.tinymce.com/license
8 * Contributing: http://www.tinymce.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
23 define("tinymce/html/Writer", [
24 "tinymce/html/Entities",
26 ], function(Entities, Tools) {
27 var makeMap = Tools.makeMap;
30 * Constructs a new Writer instance.
34 * @param {Object} settings Name/value settings object.
36 return function(settings) {
37 var html = [], indent, indentBefore, indentAfter, encode, htmlOutput;
39 settings = settings || {};
40 indent = settings.indent;
41 indentBefore = makeMap(settings.indent_before || '');
42 indentAfter = makeMap(settings.indent_after || '');
43 encode = Entities.getEncodeFunc(settings.entity_encoding || 'raw', settings.entities);
44 htmlOutput = settings.element_format == "html";
48 * Writes the a start element such as <p id="a">.
51 * @param {String} name Name of the element.
52 * @param {Array} attrs Optional attribute array or undefined if it hasn't any.
53 * @param {Boolean} empty Optional empty state if the tag should end like <br />.
55 start: function(name, attrs, empty) {
56 var i, l, attr, value;
58 if (indent && indentBefore[name] && html.length > 0) {
59 value = html[html.length - 1];
61 if (value.length > 0 && value !== '\n') {
69 for (i = 0, l = attrs.length; i < l; i++) {
71 html.push(' ', attr.name, '="', encode(attr.value, true), '"');
75 if (!empty || htmlOutput) {
76 html[html.length] = '>';
78 html[html.length] = ' />';
81 if (empty && indent && indentAfter[name] && html.length > 0) {
82 value = html[html.length - 1];
84 if (value.length > 0 && value !== '\n') {
91 * Writes the a end element such as </p>.
94 * @param {String} name Name of the element.
99 /*if (indent && indentBefore[name] && html.length > 0) {
100 value = html[html.length - 1];
102 if (value.length > 0 && value !== '\n')
106 html.push('</', name, '>');
108 if (indent && indentAfter[name] && html.length > 0) {
109 value = html[html.length - 1];
111 if (value.length > 0 && value !== '\n') {
118 * Writes a text node.
121 * @param {String} text String to write out.
122 * @param {Boolean} raw Optional raw state if true the contents wont get encoded.
124 text: function(text, raw) {
125 if (text.length > 0) {
126 html[html.length] = raw ? text : encode(text);
131 * Writes a cdata node such as <![CDATA[data]]>.
134 * @param {String} text String to write out inside the cdata.
136 cdata: function(text) {
137 html.push('<![CDATA[', text, ']]>');
141 * Writes a comment node such as <!-- Comment -->.
144 * @param {String} text String to write out inside the comment.
146 comment: function(text) {
147 html.push('<!--', text, '-->');
151 * Writes a PI node such as <?xml attr="value" ?>.
154 * @param {String} name Name of the pi.
155 * @param {String} text String to write out inside the pi.
157 pi: function(name, text) {
159 html.push('<?', name, ' ', text, '?>');
161 html.push('<?', name, '?>');
170 * Writes a doctype node such as <!DOCTYPE data>.
173 * @param {String} text String to write out inside the doctype.
175 doctype: function(text) {
176 html.push('<!DOCTYPE', text, '>', indent ? '\n' : '');
180 * Resets the internal buffer if one wants to reuse the writer.
189 * Returns the contents that got serialized.
192 * @return {String} HTML contents that got written down.
194 getContent: function() {
195 return html.join('').replace(/\n$/, '');