3 * @class Roo.htmleditor.Tidy
5 * @cfg {Roo.HtmlEditorCore} core the editor.
8 * @param {Object} config Configuration options
12 Roo.htmleditor.Tidy = function(cfg) {
15 this.core.doc.body.innerHTML = this.tidy(this.core.doc.body, '');
19 Roo.htmleditor.Tidy.toString = function(node)
21 return Roo.htmleditor.Tidy.prototype.tidy(node, '');
24 Roo.htmleditor.Tidy.prototype = {
28 return s.replace(/\n/g, " ").replace(/(?![^\n]{1,80}$)([^\n]{1,80})\s/g, '$1\n');
32 tidy : function(node, indent) {
34 if (node.nodeType == 3) {
38 return indent === false ? node.nodeValue : this.wrap(node.nodeValue.trim()).split("\n").join("\n" + indent);
43 if (node.nodeType != 1) {
49 if (node.tagName == 'BODY') {
51 return this.cn(node, '');
54 // Prints the node tagName, such as <A>, <IMG>, etc
55 var ret = "<" + node.tagName + this.attr(node) ;
57 // elements with no children..
58 if (['IMG', 'BR', 'HR', 'INPUT'].indexOf(node.tagName) > -1) {
64 var cindent = indent === false ? '' : (indent + ' ');
65 // tags where we will not pad the children.. (inline text tags etc..)
66 if (['PRE', 'TEXTAREA', 'TD', 'A', 'SPAN', 'B', 'I', 'S'].indexOf(node.tagName) > -1) { // or code?
72 var cn = this.cn(node, cindent );
74 return ret + cn + '</' + node.tagName + '>';
77 cn: function(node, indent)
81 var ar = Array.from(node.childNodes);
82 for (var i = 0 ; i < ar.length ; i++) {
86 if (indent !== false // indent==false preservies everything
88 && ar[i].nodeType == 3
89 && ar[i].nodeValue.length > 0
90 && ar[i].nodeValue.match(/^\s+/)
92 if (ret.length && ret[ret.length-1] == "\n" + indent) {
93 ret.pop(); // remove line break from last?
96 ret.push(" "); // add a space if i'm a text item with a space at the front, as tidy will strip spaces.
99 && ar[i].nodeType == 1 // element - and indent is not set...
101 ret.push("\n" + indent);
104 ret.push(this.tidy(ar[i], indent));
105 // text + trailing indent
107 && ar[i].nodeType == 3
108 && ar[i].nodeValue.length > 0
109 && ar[i].nodeValue.match(/\s+$/)
111 ret.push("\n" + indent);
126 attr : function(node)
129 for(i = 0; i < node.attributes.length;i++) {
131 // skip empty values?
132 if (!node.attributes.item(i).value.length) {
135 attr.push( node.attributes.item(i).name + '="' +
136 Roo.util.Format.htmlEncode(node.attributes.item(i).value) + '"'
139 return attr.length ? (' ' + attr.join(' ') ) : '';