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?
85 var e_inline = name == 'BR' ? false : this.in_inline;
87 // ADD LINE BEFORE tage
93 } else if (this.lastElementEndsWS()) {
96 // otherwise - no new line. (and dont indent.)
108 this.html.push(indentstr + '<', name.toLowerCase());
111 for (i = 0, l = attrs.length; i < l; i++) {
113 this.html.push(' ', attr.name, '="', this.encode(attr.value, true), '"');
119 this.html[this.html.length] = '/>';
121 this.html[this.html.length] = '></' + name.toLowerCase() + '>';
123 var e_inline = name == 'BR' ? false : this.in_inline;
125 if (!e_inline && !this.in_pre) {
132 this.html[this.html.length] = '>';
134 // there is a special situation, where we need to turn on in_inline - if any of the imediate chidlren are one of these.
136 if (!in_inline && !in_pre) {
137 var cn = node.firstChild;
139 if (Roo.htmleditor.TidyWriter.inline_elements.indexOf(cn.nodeName) > -1) {
151 indentstr : in_pre || in_inline ? '' : (this.indentstr + this.indent),
153 in_inline : in_inline
155 // add a line after if we are not in a
157 if (!in_inline && !in_pre) {
166 lastElementEndsWS : function()
168 var value = this.html.length > 0 ? this.html[this.html.length-1] : false;
169 if (value === false) {
172 return value.match(/\s+$/);
177 * Writes the a end element such as </p>.
180 * @param {String} name Name of the element.
182 end: function(name) {
186 var in_inline = this.in_inline || Roo.htmleditor.TidyWriter.inline_elements.indexOf(name) > -1;
188 if (!this.in_pre && !in_inline) {
190 indentstr = this.indentstr;
192 this.html.push(indentstr + '</', name.toLowerCase(), '>');
193 this.last_inline = in_inline;
195 // pop the indent state..
198 * Writes a text node.
200 * In pre - we should not mess with the contents.
204 * @param {String} text String to write out.
205 * @param {Boolean} raw Optional raw state if true the contents wont get encoded.
207 text: function(text, node)
209 // if not in whitespace critical
210 if (text.length < 1) {
213 if (this.in_pre || this.in_inline) {
214 this.html[this.html.length] = text;
217 // see if last element was a inline element.
218 var indentstr = this.indentstr;
219 if (node.previousSibling &&
220 node.previousSibling.nodeType == 1 &&
221 Roo.htmleditor.TidyWriter.inline_elements.indexOf(node.previousSibling.nodeName) > -1)
230 text = text.replace(/\s/g," ") // all line breaks to ' '
231 .replace(/^\s+/,'') // leding white space
232 .replace(/\s+$/,''); // clean trailing white space
234 if (text.length < 1) {
237 if (!text.match(/\n/)) {
238 this.html.push(indentstr + text);
242 text = this.indentstr + text.replace(
243 /(?![^\n]{1,64}$)([^\n]{1,64})\s/g, '$1\n' + this.indentstr
245 // remoeve the last whitespace / line break.
246 text = text.replace(/\s+$/,'');
248 this.html.push(text);
250 // split and indent..
255 * Writes a cdata node such as <![CDATA[data]]>.
258 * @param {String} text String to write out inside the cdata.
260 cdata: function(text) {
261 this.html.push('<![CDATA[', text, ']]>');
264 * Writes a comment node such as <!-- Comment -->.
267 * @param {String} text String to write out inside the comment.
269 comment: function(text) {
270 this.html.push('<!--', text, '-->');
273 * Writes a PI node such as <?xml attr="value" ?>.
276 * @param {String} name Name of the pi.
277 * @param {String} text String to write out inside the pi.
279 pi: function(name, text) {
280 text ? this.html.push('<?', name, ' ', this.encode(text), '?>') : this.html.push('<?', name, '?>');
281 this.indent != '' && this.html.push('\n');
284 * Writes a doctype node such as <!DOCTYPE data>.
287 * @param {String} text String to write out inside the doctype.
289 doctype: function(text) {
290 this.html.push('<!DOCTYPE', text, '>', this.indent != '' ? '\n' : '');
293 * Resets the internal buffer if one wants to reuse the writer.
298 this.html.length = 0;
307 * Returns the contents that got serialized.
310 * @return {String} HTML contents that got written down.
312 getContent: function() {
313 return this.html.join('').replace(/\n$/, '');
316 pushState : function(cfg)
318 this.state.push(cfg);
319 Roo.apply(this, cfg);
322 popState : function()
324 if (this.state.length < 1) {
325 return; // nothing to push
332 if (this.state.length > 0) {
333 cfg = this.state[this.state.length-1];
335 Roo.apply(this, cfg);
340 if (this.html.length < 1) {
345 var value = this.html[this.html.length - 1];
346 if (value.length > 0 && '\n' !== value) {
347 this.html.push('\n');
352 //'pre script noscript style textarea video audio iframe object code'
353 // shortended... 'area base basefont br col frame hr img input isindex link meta param embed source wbr track');
357 Roo.htmleditor.TidyWriter.inline_elements = [
358 'SPAN','STRONG','B','EM','I','FONT','STRIKE','U','VAR',
359 'CITE','DFN','CODE','MARK','Q','SUP','SUB','SAMP'
361 Roo.htmleditor.TidyWriter.shortend_elements = [
362 'AREA','BASE','BASEFONT','BR','COL','FRAME','HR','IMG','INPUT',
363 'ISINDEX','LINK','','META','PARAM','EMBED','SOURCE','WBR','TRACK'
366 Roo.htmleditor.TidyWriter.whitespace_elements = [
367 'PRE','SCRIPT','NOSCRIPT','STYLE','TEXTAREA','VIDEO','AUDIO','IFRAME','OBJECT','CODE'