2 * This is based loosely on tinymce
3 * @class Roo.htmleditor.TidyWriter
7 Roo.htmleditor.TidyWriter = function(settings)
10 // indent, indentBefore, indentAfter, encode, htmlOutput, html = [];
11 Roo.apply(this, settings);
14 this.indentBefore =this.makeMap(settings.indent_before || '');
15 this.indentAfter = this.makeMap(settings.indent_after || '');
16 this.encode = Roo.htmleditor.TidyEntities.getEncodeFunc(settings.entity_encoding || 'raw', settings.entities);
19 Roo.apply(Roo.htmleditor.TidyWriter,
23 makeMap : function (items, delim, map) {
29 if (typeof items == "string") {
30 items = items.split(delim);
49 encode = Entities.getEncodeFunc(settings.entity_encoding || 'raw', settings.entities);
50 htmlOutput = 'html' == settings.element_format;
54 * Writes the a start element such as <p id="a">.
57 * @param {String} name Name of the element.
58 * @param {Array} attrs Optional attribute array or undefined if it hasn't any.
59 * @param {Boolean} empty Optional empty state if the tag should end like <br />.
61 start: function(name, attrs, empty) {
62 var i, l, attr, value;
63 if (indent && indentBefore[name] && html.length > 0) {
64 value = html[html.length - 1];
65 value.length > 0 && '\n' !== value && html.push('\n');
69 for (i = 0, l = attrs.length; i < l; i++) {
71 html.push(' ', attr.name, '="', encode(attr.value, true), '"');
74 html[html.length] = !empty || htmlOutput ? '>' : ' />';
75 if (empty && indent && indentAfter[name] && html.length > 0) {
76 value = html[html.length - 1];
77 value.length > 0 && '\n' !== value && html.push('\n');
81 * Writes the a end element such as </p>.
84 * @param {String} name Name of the element.
88 /*if (indent && indentBefore[name] && html.length > 0) {
89 value = html[html.length - 1];
90 if (value.length > 0 && value !== '\n')
93 html.push('</', name, '>');
94 if (indent && indentAfter[name] && html.length > 0) {
95 value = html[html.length - 1];
96 value.length > 0 && '\n' !== value && html.push('\n');
100 * Writes a text node.
103 * @param {String} text String to write out.
104 * @param {Boolean} raw Optional raw state if true the contents wont get encoded.
106 text: function(text, raw) {
107 text.length > 0 && (html[html.length] = raw ? text : encode(text));
110 * Writes a cdata node such as <![CDATA[data]]>.
113 * @param {String} text String to write out inside the cdata.
115 cdata: function(text) {
116 html.push('<![CDATA[', text, ']]>');
119 * Writes a comment node such as <!-- Comment -->.
122 * @param {String} text String to write out inside the comment.
124 comment: function(text) {
125 html.push('<!--', text, '-->');
128 * Writes a PI node such as <?xml attr="value" ?>.
131 * @param {String} name Name of the pi.
132 * @param {String} text String to write out inside the pi.
134 pi: function(name, text) {
135 text ? html.push('<?', name, ' ', encode(text), '?>') : html.push('<?', name, '?>');
136 indent && html.push('\n');
139 * Writes a doctype node such as <!DOCTYPE data>.
142 * @param {String} text String to write out inside the doctype.
144 doctype: function(text) {
145 html.push('<!DOCTYPE', text, '>', indent ? '\n' : '');
148 * Resets the internal buffer if one wants to reuse the writer.
156 * Returns the contents that got serialized.
159 * @return {String} HTML contents that got written down.
161 getContent: function() {
162 return html.join('').replace(/\n$/, '');