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.
8 * - long text inside of inline can be wrapped and clened?
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;
75 var add_lb = name == 'BR' ? false : in_inline;
77 if (!add_lb && !this.in_pre && this.lastElementEndsWS()) {
81 var indentstr = this.indentstr;
83 // e_inline = elements that can be inline, but still allow \n before and after?
84 // only 'BR' ??? any others?
86 // ADD LINE BEFORE tage
92 } else if (this.lastElementEndsWS()) {
95 // otherwise - no new line. (and dont indent.)
106 this.html.push(indentstr + '<', name.toLowerCase());
109 for (i = 0, l = attrs.length; i < l; i++) {
111 this.html.push(' ', attr.name, '="', this.encode(attr.value, true), '"');
117 this.html[this.html.length] = '/>';
119 this.html[this.html.length] = '></' + name.toLowerCase() + '>';
121 var e_inline = name == 'BR' ? false : this.in_inline;
123 if (!e_inline && !this.in_pre) {
130 this.html[this.html.length] = '>';
132 // there is a special situation, where we need to turn on in_inline - if any of the imediate chidlren are one of these.
134 if (!in_inline && !in_pre) {
135 var cn = node.firstChild;
137 if (Roo.htmleditor.TidyWriter.inline_elements.indexOf(cn.nodeName) > -1) {
149 indentstr : in_pre ? '' : (this.indentstr + this.indent),
151 in_inline : in_inline
153 // add a line after if we are not in a
155 if (!in_inline && !in_pre) {
164 lastElementEndsWS : function()
166 var value = this.html.length > 0 ? this.html[this.html.length-1] : false;
167 if (value === false) {
170 return value.match(/\s+$/);
175 * Writes the a end element such as </p>.
178 * @param {String} name Name of the element.
180 end: function(name) {
184 var in_inline = this.in_inline || Roo.htmleditor.TidyWriter.inline_elements.indexOf(name) > -1;
186 if (!this.in_pre && !in_inline) {
188 indentstr = this.indentstr;
190 this.html.push(indentstr + '</', name.toLowerCase(), '>');
191 this.last_inline = in_inline;
193 // pop the indent state..
196 * Writes a text node.
198 * In pre - we should not mess with the contents.
202 * @param {String} text String to write out.
203 * @param {Boolean} raw Optional raw state if true the contents wont get encoded.
205 text: function(text, node)
207 // if not in whitespace critical
208 if (text.length < 1) {
211 if (this.in_pre || this.in_inline) {
213 if (this.in_inline) {
214 text = text.replace(/\s/g,' ') // all line breaks to ' '
215 .replace(/\s+/,' ') // all white space to single white space
216 // if next tag is '<BR>', then we can trim right..
217 if (node.nextSibling &&
218 node.nextSibling.nodeType == 1 &&
219 node.nextSibling.nodeName == 'BR' )
221 text = text.replace(/\s+$/g,'');
223 // if previous tag was a BR, we can also trim..
224 if (node.previousSibling &&
225 node.previousSibling.nodeType == 1 &&
226 node.previousSibling.nodeName == 'BR' )
228 text = this.indentstr + text.replace(/^\s+/g,'');
231 text = this.indentstr + text.replace(
232 /(?![^\n]{1,64}$)([^\n]{1,64})\s/g, '$1\n' + this.indentstr
234 // remoeve the last whitespace / line break.
235 text = text.replace(/\s+$/,'');
240 this.html[this.html.length] = text;
243 // see if last element was a inline element.
244 var indentstr = this.indentstr;
245 if (node.previousSibling &&
246 node.previousSibling.nodeType == 1 &&
247 Roo.htmleditor.TidyWriter.inline_elements.indexOf(node.previousSibling.nodeName) > -1)
256 text = text.replace(/\s/g," ") // all line breaks to ' '
257 .replace(/^\s+/,'') // leding white space
258 .replace(/\s+$/,''); // clean trailing white space
260 if (text.length < 1) {
263 if (!text.match(/\n/)) {
264 this.html.push(indentstr + text);
268 text = this.indentstr + text.replace(
269 /(?![^\n]{1,64}$)([^\n]{1,64})\s/g, '$1\n' + this.indentstr
271 // remoeve the last whitespace / line break.
272 text = text.replace(/\s+$/,'');
274 this.html.push(text);
276 // split and indent..
281 * Writes a cdata node such as <![CDATA[data]]>.
284 * @param {String} text String to write out inside the cdata.
286 cdata: function(text) {
287 this.html.push('<![CDATA[', text, ']]>');
290 * Writes a comment node such as <!-- Comment -->.
293 * @param {String} text String to write out inside the comment.
295 comment: function(text) {
296 this.html.push('<!--', text, '-->');
299 * Writes a PI node such as <?xml attr="value" ?>.
302 * @param {String} name Name of the pi.
303 * @param {String} text String to write out inside the pi.
305 pi: function(name, text) {
306 text ? this.html.push('<?', name, ' ', this.encode(text), '?>') : this.html.push('<?', name, '?>');
307 this.indent != '' && this.html.push('\n');
310 * Writes a doctype node such as <!DOCTYPE data>.
313 * @param {String} text String to write out inside the doctype.
315 doctype: function(text) {
316 this.html.push('<!DOCTYPE', text, '>', this.indent != '' ? '\n' : '');
319 * Resets the internal buffer if one wants to reuse the writer.
324 this.html.length = 0;
333 * Returns the contents that got serialized.
336 * @return {String} HTML contents that got written down.
338 getContent: function() {
339 return this.html.join('').replace(/\n$/, '');
342 pushState : function(cfg)
344 this.state.push(cfg);
345 Roo.apply(this, cfg);
348 popState : function()
350 if (this.state.length < 1) {
351 return; // nothing to push
358 if (this.state.length > 0) {
359 cfg = this.state[this.state.length-1];
361 Roo.apply(this, cfg);
366 if (this.html.length < 1) {
371 var value = this.html[this.html.length - 1];
372 if (value.length > 0 && '\n' !== value) {
373 this.html.push('\n');
378 //'pre script noscript style textarea video audio iframe object code'
379 // shortended... 'area base basefont br col frame hr img input isindex link meta param embed source wbr track');
383 Roo.htmleditor.TidyWriter.inline_elements = [
384 'SPAN','STRONG','B','EM','I','FONT','STRIKE','U','VAR',
385 'CITE','DFN','CODE','MARK','Q','SUP','SUB','SAMP'
387 Roo.htmleditor.TidyWriter.shortend_elements = [
388 'AREA','BASE','BASEFONT','BR','COL','FRAME','HR','IMG','INPUT',
389 'ISINDEX','LINK','','META','PARAM','EMBED','SOURCE','WBR','TRACK'
392 Roo.htmleditor.TidyWriter.whitespace_elements = [
393 'PRE','SCRIPT','NOSCRIPT','STYLE','TEXTAREA','VIDEO','AUDIO','IFRAME','OBJECT','CODE'