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 i_inline = name == 'BR' ? false : in_inline;
76 if (i_inline && this.lastElementEndsWS()) {
80 var indentstr = i_inline || this.in_pre ? '' : this.indentstr;
82 // e_inline = elements that can be inline, but still allow \n before and after?
83 // only 'BR' ??? any others?
84 var e_inline = name == 'BR' ? false : this.in_inline;
86 // if this element is inline - then don't add stuff beforehand..
87 if (!e_inline && !this.in_pre) {
91 this.html.push(indentstr + '<', name.toLowerCase());
94 for (i = 0, l = attrs.length; i < l; i++) {
96 this.html.push(' ', attr.name, '="', this.encode(attr.value, true), '"');
102 this.html[this.html.length] = '/>';
104 this.html[this.html.length] = '></' + name.toLowerCase() + '>';
106 var e_inline = name == 'BR' ? false : this.in_inline;
108 if (!e_inline && !this.in_pre) {
115 this.html[this.html.length] = '>';
117 // there is a special situation, where we need to turn on in_inline - if any of the imediate chidlren are one of these.
119 if (!in_inline && !in_pre) {
120 var cn = node.firstChild;
122 if (Roo.htmleditor.TidyWriter.inline_elements.indexOf(cn.nodeName) > -1) {
134 indentstr : in_pre || in_inline ? '' : (this.indentstr + this.indent),
136 in_inline : in_inline
138 // add a line after if we are not in a
140 if (!in_inline && !in_pre) {
149 lastElementEndsWS : function()
151 var value = this.html.length > 0 ? this.html[this.html.length-1] : false;
152 if (value === false) {
155 return value.match(/\s+$/);
160 * Writes the a end element such as </p>.
163 * @param {String} name Name of the element.
165 end: function(name) {
169 var in_inline = this.in_inline || Roo.htmleditor.TidyWriter.inline_elements.indexOf(name) > -1;
171 if (!this.in_pre && !in_inline) {
173 indentstr = this.indentstr;
175 this.html.push(indentstr + '</', name.toLowerCase(), '>');
176 this.last_inline = in_inline;
178 // pop the indent state..
181 * Writes a text node.
183 * In pre - we should not mess with the contents.
187 * @param {String} text String to write out.
188 * @param {Boolean} raw Optional raw state if true the contents wont get encoded.
190 text: function(text, node)
192 // if not in whitespace critical
193 if (text.length < 1) {
196 if (this.in_pre || this.in_inline) {
197 this.html[this.html.length] = text;
200 // see if last element was a inline element.
201 var indentstr = this.indentstr;
202 if (node.previousSibling &&
203 node.previousSibling.nodeType == 1 &&
204 Roo.htmleditor.TidyWriter.inline_elements.indexOf(node.previousSibling.nodeName) > -1)
213 text = text.replace(/\s/g," ") // all line breaks to ' '
214 .replace(/^\s+/,'') // leding white space
215 .replace(/\s+$/,''); // clean trailing white space
217 if (text.length < 1) {
220 if (!text.match(/\n/)) {
221 this.html.push(indentstr + text);
225 text = this.indentstr + text.replace(
226 /(?![^\n]{1,64}$)([^\n]{1,64})\s/g, '$1\n' + this.indentstr
228 // remoeve the last whitespace / line break.
229 text = text.replace(/\s+$/,'');
231 this.html.push(text);
233 // split and indent..
238 * Writes a cdata node such as <![CDATA[data]]>.
241 * @param {String} text String to write out inside the cdata.
243 cdata: function(text) {
244 this.html.push('<![CDATA[', text, ']]>');
247 * Writes a comment node such as <!-- Comment -->.
250 * @param {String} text String to write out inside the comment.
252 comment: function(text) {
253 this.html.push('<!--', text, '-->');
256 * Writes a PI node such as <?xml attr="value" ?>.
259 * @param {String} name Name of the pi.
260 * @param {String} text String to write out inside the pi.
262 pi: function(name, text) {
263 text ? this.html.push('<?', name, ' ', this.encode(text), '?>') : this.html.push('<?', name, '?>');
264 this.indent != '' && this.html.push('\n');
267 * Writes a doctype node such as <!DOCTYPE data>.
270 * @param {String} text String to write out inside the doctype.
272 doctype: function(text) {
273 this.html.push('<!DOCTYPE', text, '>', this.indent != '' ? '\n' : '');
276 * Resets the internal buffer if one wants to reuse the writer.
281 this.html.length = 0;
290 * Returns the contents that got serialized.
293 * @return {String} HTML contents that got written down.
295 getContent: function() {
296 return this.html.join('').replace(/\n$/, '');
299 pushState : function(cfg)
301 this.state.push(cfg);
302 Roo.apply(this, cfg);
305 popState : function()
307 if (this.state.length < 1) {
308 return; // nothing to push
315 if (this.state.length > 0) {
316 cfg = this.state[this.state.length-1];
318 Roo.apply(this, cfg);
323 if (this.html.length < 1) {
328 var value = this.html[this.html.length - 1];
329 if (value.length > 0 && '\n' !== value) {
330 this.html.push('\n');
335 //'pre script noscript style textarea video audio iframe object code'
336 // shortended... 'area base basefont br col frame hr img input isindex link meta param embed source wbr track');
340 Roo.htmleditor.TidyWriter.inline_elements = [
341 'SPAN','STRONG','B','EM','I','FONT','STRIKE','U','VAR',
342 'CITE','DFN','CODE','MARK','Q','SUP','SUB','SAMP'
344 Roo.htmleditor.TidyWriter.shortend_elements = [
345 'AREA','BASE','BASEFONT','BR','COL','FRAME','HR','IMG','INPUT',
346 'ISINDEX','LINK','','META','PARAM','EMBED','SOURCE','WBR','TRACK'
349 Roo.htmleditor.TidyWriter.whitespace_elements = [
350 'PRE','SCRIPT','NOSCRIPT','STYLE','TEXTAREA','VIDEO','AUDIO','IFRAME','OBJECT','CODE'