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);
17 this.htmlOutput = 'html' == settings.element_format;
19 Roo.apply(Roo.htmleditor.TidyWriter,
23 makeMap : function (items, delim, map) {
29 if (typeof items == "string") {
30 items = items.split(delim);
51 * Writes the a start element such as <p id="a">.
54 * @param {String} name Name of the element.
55 * @param {Array} attrs Optional attribute array or undefined if it hasn't any.
56 * @param {Boolean} empty Optional empty state if the tag should end like <br />.
58 start: function(name, attrs, empty) {
59 var i, l, attr, value;
60 if (this.indent && this.indentBefore[name] && this.html.length > 0) {
61 this.value = this.html[this.html.length - 1];
62 value.length > 0 && '\n' !== value && this.html.push('\n');
64 this.html.push('<', name);
66 for (i = 0, l = attrs.length; i < l; i++) {
68 this.html.push(' ', attr.name, '="', this.encode(attr.value, true), '"');
71 this.html[this.html.length] = !empty || this.htmlOutput ? '>' : ' />';
72 if (empty && this.indent && this.indentAfter[name] && this.html.length > 0) {
73 value = this.html[this.html.length - 1];
74 value.length > 0 && '\n' !== value && this.html.push('\n');
78 * Writes the a end element such as </p>.
81 * @param {String} name Name of the element.
86 this.html.push('</', name, '>');
87 if (this.indent && this.indentAfter[name] && this.html.length > 0) {
88 value = this.html[this.html.length - 1];
89 value.length > 0 && '\n' !== value && this.html.push('\n');
96 * @param {String} text String to write out.
97 * @param {Boolean} raw Optional raw state if true the contents wont get encoded.
99 text: function(text, raw) {
100 text.length > 0 && (this.html[this.html.length] = raw ? text : encode(text));
103 * Writes a cdata node such as <![CDATA[data]]>.
106 * @param {String} text String to write out inside the cdata.
108 cdata: function(text) {
109 this.html.push('<![CDATA[', text, ']]>');
112 * Writes a comment node such as <!-- Comment -->.
115 * @param {String} text String to write out inside the comment.
117 comment: function(text) {
118 this.html.push('<!--', text, '-->');
121 * Writes a PI node such as <?xml attr="value" ?>.
124 * @param {String} name Name of the pi.
125 * @param {String} text String to write out inside the pi.
127 pi: function(name, text) {
128 text ? this.html.push('<?', name, ' ', this.encode(text), '?>') : this.html.push('<?', name, '?>');
129 this.indent && this.html.push('\n');
132 * Writes a doctype node such as <!DOCTYPE data>.
135 * @param {String} text String to write out inside the doctype.
137 doctype: function(text) {
138 this.html.push('<!DOCTYPE', text, '>', this.indent ? '\n' : '');
141 * Resets the internal buffer if one wants to reuse the writer.
146 this.html.length = 0;
149 * Returns the contents that got serialized.
152 * @return {String} HTML contents that got written down.
154 getContent: function() {
155 return this.html.join('').replace(/\n$/, '');