Changed Roo/htmleditor/FilterKeepChildren.js
[roojs1] / Roo / htmleditor / FilterKeepChildren.js
1 /**
2  * @class Roo.htmleditor.FilterKeepChildren
3  * remove tags but keep children
4  * @constructor
5  * Run a new Keep Children Filter
6  * @param {Object} config Configuration options
7  */
8
9 Roo.htmleditor.FilterKeepChildren = function(cfg)
10 {
11     Roo.apply(this, cfg);
12     if (this.tag === false) {
13         return; // dont walk.. (you can use this to use this just to do a child removal on a single tag )
14     }
15     // hacky?
16     if ((typeof(this.tag) == 'object' && this.tag.indexOf(":") > -1)) {
17         this.cleanNamespace = true;
18     }
19         
20     this.walk(cfg.node);
21 }
22
23 Roo.extend(Roo.htmleditor.FilterKeepChildren, Roo.htmleditor.FilterBlack,
24 {
25     cleanNamespace : false, // should really be an option, rather than using ':' inside of this tag.
26   
27     replaceTag : function(node)
28     {
29         // walk children...
30         //Roo.log(node);
31         var ar = Array.from(node.childNodes);
32         //remove first..
33         
34         for (var i = 0; i < ar.length; i++) {
35             if (ar[i].nodeType == 1) {
36                 if (
37                     (typeof(this.tag) == 'object' && this.tag.indexOf(ar[i].tagName) > -1)
38                     || // array and it matches
39                     (typeof(this.tag) == 'string' && this.tag == ar[i].tagName)
40                     ||
41                     this.cleanNameSpace && ar[i].tagName.match(/:/)
42                 ) {
43                     this.replaceTag(ar[i]); // child is blacklisted as well...
44                     continue;
45                 }
46             }
47         }  
48         ar = Array.from(node.childNodes);
49         for (var i = 0; i < ar.length; i++) {
50          
51             node.removeChild(ar[i]);
52             // what if we need to walk these???
53             node.parentNode.insertBefore(ar[i], node);
54             if (this.tag !== false) {
55                 this.walk(ar[i]);
56                 
57             }
58         }
59         node.parentNode.removeChild(node);
60         return false; // don't walk children
61         
62         
63     }
64 });