2 * This is based loosely on tinymce
3 * @class Roo.htmleditor.TidyWriter
4 * https://github.com/thorn0/tinymce.html/blob/master/tinymce.html.js
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);
17 this.htmlOutput = 'html' == settings.element_format;
19 Roo.htmleditor.TidyWriter.prototype = {
23 makeMap : function (items, delim, map) {
27 if (typeof items == "string") {
28 items = items.split(delim);
46 * Writes the a start element such as <p id="a">.
49 * @param {String} name Name of the element.
50 * @param {Array} attrs Optional attribute array or undefined if it hasn't any.
51 * @param {Boolean} empty Optional empty state if the tag should end like <br />.
53 start: function(name, attrs, empty) {
54 var i, l, attr, value;
55 if (this.indent && this.indentBefore[name] && this.html.length > 0) {
56 this.value = this.html[this.html.length - 1];
57 value.length > 0 && '\n' !== value && this.html.push('\n');
59 this.html.push('<', name);
61 for (i = 0, l = attrs.length; i < l; i++) {
63 this.html.push(' ', attr.name, '="', this.encode(attr.value, true), '"');
66 this.html[this.html.length] = !empty || this.htmlOutput ? '>' : ' />';
67 if (empty && this.indent && this.indentAfter[name] && this.html.length > 0) {
68 value = this.html[this.html.length - 1];
69 value.length > 0 && '\n' !== value && this.html.push('\n');
73 * Writes the a end element such as </p>.
76 * @param {String} name Name of the element.
81 this.html.push('</', name, '>');
82 if (this.indent && this.indentAfter[name] && this.html.length > 0) {
83 value = this.html[this.html.length - 1];
84 value.length > 0 && '\n' !== value && this.html.push('\n');
91 * @param {String} text String to write out.
92 * @param {Boolean} raw Optional raw state if true the contents wont get encoded.
94 text: function(text, raw) {
95 text.length > 0 && (this.html[this.html.length] = raw ? text : encode(text));
98 * Writes a cdata node such as <![CDATA[data]]>.
101 * @param {String} text String to write out inside the cdata.
103 cdata: function(text) {
104 this.html.push('<![CDATA[', text, ']]>');
107 * Writes a comment node such as <!-- Comment -->.
110 * @param {String} text String to write out inside the comment.
112 comment: function(text) {
113 this.html.push('<!--', text, '-->');
116 * Writes a PI node such as <?xml attr="value" ?>.
119 * @param {String} name Name of the pi.
120 * @param {String} text String to write out inside the pi.
122 pi: function(name, text) {
123 text ? this.html.push('<?', name, ' ', this.encode(text), '?>') : this.html.push('<?', name, '?>');
124 this.indent && this.html.push('\n');
127 * Writes a doctype node such as <!DOCTYPE data>.
130 * @param {String} text String to write out inside the doctype.
132 doctype: function(text) {
133 this.html.push('<!DOCTYPE', text, '>', this.indent ? '\n' : '');
136 * Resets the internal buffer if one wants to reuse the writer.
141 this.html.length = 0;
144 * Returns the contents that got serialized.
147 * @return {String} HTML contents that got written down.
149 getContent: function() {
150 return this.html.join('').replace(/\n$/, '');