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 * - not tested much with 'PRE' formated elements.
13 Roo.htmleditor.TidyWriter = function(settings)
16 // indent, indentBefore, indentAfter, encode, htmlOutput, html = [];
17 Roo.apply(this, settings);
21 this.encode = Roo.htmleditor.TidyEntities.getEncodeFunc(settings.entity_encoding || 'raw', settings.entities);
24 Roo.htmleditor.TidyWriter.prototype = {
28 makeMap : function (items, delim, map) {
32 if (typeof items == "string") {
33 items = items.split(delim);
56 * Writes the a start element such as <p id="a">.
59 * @param {String} name Name of the element.
60 * @param {Array} attrs Optional attribute array or undefined if it hasn't any.
61 * @param {Boolean} empty Optional empty state if the tag should end like <br />.
63 start: function(name, attrs, empty, node)
65 var i, l, attr, value;
67 // there are some situations where adding line break && indentation will not work. will not work.
68 // <span / b / i ... formating?
70 var in_inline = this.in_inline || Roo.htmleditor.TidyWriter.inline_elements.indexOf(name) > -1;
71 var in_pre = this.in_pre || Roo.htmleditor.TidyWriter.whitespace_elements.indexOf(name) > -1;
73 var is_short = empty ? Roo.htmleditor.TidyWriter.shortend_elements.indexOf(name) > -1 : false;
76 var indentstr = in_inline || this.in_pre ? '' : this.indentstr;
78 // if this element is inline - then don't add stuff beforehand..
79 if (!in_inline && !this.in_pre) {
83 this.html.push(indentstr + '<', name.toLowerCase());
86 for (i = 0, l = attrs.length; i < l; i++) {
88 this.html.push(' ', attr.name, '="', this.encode(attr.value, true), '"');
94 this.html[this.html.length] = '/>';
96 this.html[this.html.length] = '></' + name.toLowerCase() + '>';
99 if (!this.in_inline && !this.in_pre) {
106 this.html[this.html.length] = '>';
108 // there is a special situation, where we need to turn on in_inline - if any of the imediate chidlren are one of these.
110 if (!in_inline && !in_pre) {
111 var cn = node.firstChild;
113 if (Roo.htmleditor.TidyWriter.inline_elements.indexOf(cn.nodeName) > -1) {
125 indentstr : in_pre || in_inline ? '' : (this.indentstr + this.indent),
127 in_inline : in_inline
129 // add a line after if we are not in a
131 if (!in_inline && !in_pre) {
140 * Writes the a end element such as </p>.
143 * @param {String} name Name of the element.
145 end: function(name) {
149 var in_inline = this.in_inline || Roo.htmleditor.TidyWriter.inline_elements.indexOf(name) > -1;
151 if (!this.in_pre && !in_inline) {
153 indentstr = this.indentstr;
155 this.html.push(indentstr + '</', name.toLowerCase(), '>');
156 this.last_inline = in_inline;
158 // pop the indent state..
161 * Writes a text node.
163 * In pre - we should not mess with the contents.
167 * @param {String} text String to write out.
168 * @param {Boolean} raw Optional raw state if true the contents wont get encoded.
170 text: function(text, node)
172 // if not in whitespace critical
173 if (text.length < 1) {
176 if (this.in_pre || this.in_inline) {
177 this.html[this.html.length] = text;
180 // see if last element was a inline element.
181 var indentstr = this.indentstr;
182 if (node.previousSibling &&
183 node.previousSibling.nodeType == 1 &&
184 Roo.htmleditor.TidyWriter.inline_elements.indexOf(node.previousSibling.nodeName) > -1)
193 text = text.replace(/\s/g," ") // all line breaks to ' '
194 .replace(/^\s+/,'') // leding white space
195 .replace(/\s+$/,''); // clean trailing white space
197 if (text.length < 1) {
200 if (!text.match(/\n/)) {
201 this.html.push(indentstr + text);
205 text = this.indentstr + text.replace(
206 /(?![^\n]{1,64}$)([^\n]{1,64})\s/g, '$1\n' + this.indentstr
208 // remoeve the last whitespace / line break.
209 text = text.replace(/\s+$/,'');
211 this.html.push(text);
213 // split and indent..
218 * Writes a cdata node such as <![CDATA[data]]>.
221 * @param {String} text String to write out inside the cdata.
223 cdata: function(text) {
224 this.html.push('<![CDATA[', text, ']]>');
227 * Writes a comment node such as <!-- Comment -->.
230 * @param {String} text String to write out inside the comment.
232 comment: function(text) {
233 this.html.push('<!--', text, '-->');
236 * Writes a PI node such as <?xml attr="value" ?>.
239 * @param {String} name Name of the pi.
240 * @param {String} text String to write out inside the pi.
242 pi: function(name, text) {
243 text ? this.html.push('<?', name, ' ', this.encode(text), '?>') : this.html.push('<?', name, '?>');
244 this.indent != '' && this.html.push('\n');
247 * Writes a doctype node such as <!DOCTYPE data>.
250 * @param {String} text String to write out inside the doctype.
252 doctype: function(text) {
253 this.html.push('<!DOCTYPE', text, '>', this.indent != '' ? '\n' : '');
256 * Resets the internal buffer if one wants to reuse the writer.
261 this.html.length = 0;
270 * Returns the contents that got serialized.
273 * @return {String} HTML contents that got written down.
275 getContent: function() {
276 return this.html.join('').replace(/\n$/, '');
279 pushState : function(cfg)
281 this.state.push(cfg);
282 Roo.apply(this, cfg);
285 popState : function()
287 if (this.state.length < 1) {
288 return; // nothing to push
295 if (this.state.length > 0) {
296 cfg = this.state[this.state.length-1];
298 Roo.apply(this, cfg);
303 if (this.html.length < 1) {
308 var value = this.html[this.html.length - 1];
309 if (value.length > 0 && '\n' !== value) {
310 this.html.push('\n');
315 //'pre script noscript style textarea video audio iframe object code'
316 // shortended... 'area base basefont br col frame hr img input isindex link meta param embed source wbr track');
320 Roo.htmleditor.TidyWriter.inline_elements = [
321 'SPAN','STRONG','B','EM','I','FONT','STRIKE','U','VAR',
322 'CITE','DFN','CODE','MARK','Q','SUP','SUB','SAMP'
324 Roo.htmleditor.TidyWriter.shortend_elements = [
325 'AREA','BASE','BASEFONT','BR','COL','FRAME','HR','IMG','INPUT',
326 'ISINDEX','LINK','','META','PARAM','EMBED','SOURCE','WBR','TRACK'
329 Roo.htmleditor.TidyWriter.whitespace_elements = [
330 'PRE','SCRIPT','NOSCRIPT','STYLE','TEXTAREA','VIDEO','AUDIO','IFRAME','OBJECT','CODE'