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++) {
83 if (indent !== false // indent==false preservies everything
85 && ar[i].nodeType == 3
86 && ar[i].nodeValue.length > 0
87 && ar[i].nodeValue.match(/^\s+/)
89 ret.push(" "); // add a space if i'm a text item with a space at the front, as tidy will strip spaces.
92 && ar[i].nodeType == 1 // element - and indent is not set...
94 ret.push("\n" + indent);
97 ret.push(this.tidy(ar[i], indent));
98 // text + trailing indent
100 && ar[i].nodeType == 3
101 && ar[i].nodeValue.length > 0
102 && ar[i].nodeValue.match(/\s+$/)
104 ret.push("\n" + indent);
119 attr : function(node)
122 for(i = 0; i < node.attributes.length;i++) {
124 // skip empty values?
125 if (!node.attributes.item(i).value.length) {
128 attr.push( node.attributes.item(i).name + '="' +
129 Roo.util.Format.htmlEncode(node.attributes.item(i).value) + '"'
132 return attr.length ? (' ' + attr.join(' ') ) : '';