Roo/htmleditor/Tidy.js
[roojs1] / Roo / htmleditor / Tidy.js
1
2 /**
3  * @class Roo.htmleditor.Tidy
4  * Tidy HTML 
5  * @cfg {Roo.HtmlEditorCore} core the editor.
6  * @constructor
7  * Create a new Filter.
8  * @param {Object} config Configuration options
9  */
10
11
12 Roo.htmleditor.Tidy = function(cfg) {
13     Roo.apply(this, cfg);
14     
15     this.core.doc.body.innerHTML = this.tidy(this.core.doc.body, '');
16      
17 }
18 Roo.htmleditor.Tidy.prototype = {
19     
20     
21     wrap : function(s) {
22         return s.replace(/\n/g, " ").replace(/(?![^\n]{1,80}$)([^\n]{1,80})\s/g, '$1\n');
23     },
24
25     
26     /* ?? why ?? */
27     tidy : function(node, indent) {
28         
29        
30         //Roo.log(currentElement);
31        
32         var nodeName = node.nodeName;
33         //var tagName = Roo.util.Format.htmlEncode(currentElement.tagName);
34         var tagName = node.tagName; /// why encode tagname?
35         
36         if  (node.nodeType == 3) {
37             // text.
38             return indent === false ? node.nodeValue :
39                 this.wrap(node.nodeValue.trim()).split("\n").join("\n" + indent)
40             
41         }
42         
43         if  (node.nodeType != 1) {
44             return '';
45         }
46         
47         
48         
49         if (node.tagName == 'BODY') {
50             
51             return this.cn(node, '');
52         }
53              
54              // Prints the node tagName, such as <A>, <IMG>, etc
55         var ret = (indent === false ? '' : indent ) + "<"+ node.node.tagName +  _this.attr(node) 
56         
57         // elements with no children..
58         if (['IMG', 'BR', 'HR', 'INPUT'].indexOf(tagName) > -1) {
59                 return ret + '/>';
60         }
61         ret += '>';
62         
63         var cindent = indent + '  ';
64         if (['PRE', 'TEXTAREA', 'TD', 'A', 'SPAN'].indexOf(tagName) > -1) { // or code?
65             cindent = false;
66         }
67         
68         return ret +
69                 this.cn(node, cindent )  +
70             '</' + node.tagName + '>' +
71             (indent === false ? '' : "\n");
72         
73     },
74     cn: function(node, indent)
75     {
76         var ret = [];
77         var allText = true;
78         var ar = Array.from(node.childNodes);
79         for (var i = 0 ; i < ar.length ; i++) {
80             ret.push(this.tidy(ar[i], indent));
81             if (ar[i].nodeType != 3) { //text
82                 allText = false;
83             }
84         }
85         return ret.join('');
86     },
87     
88          
89         
90     attr : function(node)
91     {
92         var attr = [];
93         for(i = 0; i < node.attributes.length;i++) {
94             
95             // skip empty values?
96             if (!node.attributes.item(i).value.length) {
97                 continue;
98             }
99             attr.push(  node.attributes.item(i).name + '="' +
100                     Roo.util.Format.htmlEncode(currentElement.attributes.item(i).value) + '"'
101             );
102         }
103         return attr.length ? (' ' + attr.join(' ') ) : '';
104         
105     }
106     
107     
108     
109 }