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.)
107 this.html.push(indentstr + '<', name.toLowerCase());
110 for (i = 0, l = attrs.length; i < l; i++) {
112 this.html.push(' ', attr.name, '="', this.encode(attr.value, true), '"');
118 this.html[this.html.length] = '/>';
120 this.html[this.html.length] = '></' + name.toLowerCase() + '>';
122 var e_inline = name == 'BR' ? false : this.in_inline;
124 if (!e_inline && !this.in_pre) {
131 this.html[this.html.length] = '>';
133 // there is a special situation, where we need to turn on in_inline - if any of the imediate chidlren are one of these.
135 if (!in_inline && !in_pre) {
136 var cn = node.firstChild;
138 if (Roo.htmleditor.TidyWriter.inline_elements.indexOf(cn.nodeName) > -1) {
150 indentstr : in_pre || in_inline ? '' : (this.indentstr + this.indent),
152 in_inline : in_inline
154 // add a line after if we are not in a
156 if (!in_inline && !in_pre) {
165 lastElementEndsWS : function()
167 var value = this.html.length > 0 ? this.html[this.html.length-1] : false;
168 if (value === false) {
171 return value.match(/\s+$/);
176 * Writes the a end element such as </p>.
179 * @param {String} name Name of the element.
181 end: function(name) {
185 var in_inline = this.in_inline || Roo.htmleditor.TidyWriter.inline_elements.indexOf(name) > -1;
187 if (!this.in_pre && !in_inline) {
189 indentstr = this.indentstr;
191 this.html.push(indentstr + '</', name.toLowerCase(), '>');
192 this.last_inline = in_inline;
194 // pop the indent state..
197 * Writes a text node.
199 * In pre - we should not mess with the contents.
203 * @param {String} text String to write out.
204 * @param {Boolean} raw Optional raw state if true the contents wont get encoded.
206 text: function(text, node)
208 // if not in whitespace critical
209 if (text.length < 1) {
212 if (this.in_pre || this.in_inline) {
213 this.html[this.html.length] = text;
216 // see if last element was a inline element.
217 var indentstr = this.indentstr;
218 if (node.previousSibling &&
219 node.previousSibling.nodeType == 1 &&
220 Roo.htmleditor.TidyWriter.inline_elements.indexOf(node.previousSibling.nodeName) > -1)
229 text = text.replace(/\s/g," ") // all line breaks to ' '
230 .replace(/^\s+/,'') // leding white space
231 .replace(/\s+$/,''); // clean trailing white space
233 if (text.length < 1) {
236 if (!text.match(/\n/)) {
237 this.html.push(indentstr + text);
241 text = this.indentstr + text.replace(
242 /(?![^\n]{1,64}$)([^\n]{1,64})\s/g, '$1\n' + this.indentstr
244 // remoeve the last whitespace / line break.
245 text = text.replace(/\s+$/,'');
247 this.html.push(text);
249 // split and indent..
254 * Writes a cdata node such as <![CDATA[data]]>.
257 * @param {String} text String to write out inside the cdata.
259 cdata: function(text) {
260 this.html.push('<![CDATA[', text, ']]>');
263 * Writes a comment node such as <!-- Comment -->.
266 * @param {String} text String to write out inside the comment.
268 comment: function(text) {
269 this.html.push('<!--', text, '-->');
272 * Writes a PI node such as <?xml attr="value" ?>.
275 * @param {String} name Name of the pi.
276 * @param {String} text String to write out inside the pi.
278 pi: function(name, text) {
279 text ? this.html.push('<?', name, ' ', this.encode(text), '?>') : this.html.push('<?', name, '?>');
280 this.indent != '' && this.html.push('\n');
283 * Writes a doctype node such as <!DOCTYPE data>.
286 * @param {String} text String to write out inside the doctype.
288 doctype: function(text) {
289 this.html.push('<!DOCTYPE', text, '>', this.indent != '' ? '\n' : '');
292 * Resets the internal buffer if one wants to reuse the writer.
297 this.html.length = 0;
306 * Returns the contents that got serialized.
309 * @return {String} HTML contents that got written down.
311 getContent: function() {
312 return this.html.join('').replace(/\n$/, '');
315 pushState : function(cfg)
317 this.state.push(cfg);
318 Roo.apply(this, cfg);
321 popState : function()
323 if (this.state.length < 1) {
324 return; // nothing to push
331 if (this.state.length > 0) {
332 cfg = this.state[this.state.length-1];
334 Roo.apply(this, cfg);
339 if (this.html.length < 1) {
344 var value = this.html[this.html.length - 1];
345 if (value.length > 0 && '\n' !== value) {
346 this.html.push('\n');
351 //'pre script noscript style textarea video audio iframe object code'
352 // shortended... 'area base basefont br col frame hr img input isindex link meta param embed source wbr track');
356 Roo.htmleditor.TidyWriter.inline_elements = [
357 'SPAN','STRONG','B','EM','I','FONT','STRIKE','U','VAR',
358 'CITE','DFN','CODE','MARK','Q','SUP','SUB','SAMP'
360 Roo.htmleditor.TidyWriter.shortend_elements = [
361 'AREA','BASE','BASEFONT','BR','COL','FRAME','HR','IMG','INPUT',
362 'ISINDEX','LINK','','META','PARAM','EMBED','SOURCE','WBR','TRACK'
365 Roo.htmleditor.TidyWriter.whitespace_elements = [
366 'PRE','SCRIPT','NOSCRIPT','STYLE','TEXTAREA','VIDEO','AUDIO','IFRAME','OBJECT','CODE'