more styling
[roojs1] / Roo / htmleditor / TidySerializer.js
1
2 /***
3  * This is based loosely on tinymce 
4  * @class Roo.htmleditor.TidySerializer
5  * https://github.com/thorn0/tinymce.html/blob/master/tinymce.html.js
6  * @constructor
7  * @method Serializer
8  * @param {Object} settings Name/value settings object.
9  */
10
11
12 Roo.htmleditor.TidySerializer = function(settings)
13 {
14     Roo.apply(this, settings);
15     
16     this.writer = new Roo.htmleditor.TidyWriter(settings);
17     
18     
19
20 };
21 Roo.htmleditor.TidySerializer.prototype = {
22     
23     /**
24      * @param {boolean} inner do the inner of the node.
25      */
26     inner : false,
27     
28     writer : false,
29     
30     /**
31     * Serializes the specified node into a string.
32     *
33     * @example
34     * new tinymce.html.Serializer().serialize(new tinymce.html.DomParser().parse('<p>text</p>'));
35     * @method serialize
36     * @param {DomElement} node Node instance to serialize.
37     * @return {String} String with HTML based on DOM tree.
38     */
39     serialize : function(node) {
40         
41         // = settings.validate;
42         var writer = this.writer;
43         var self  = this;
44         this.handlers = {
45             // #text
46             3: function(node) {
47                 
48                 writer.text(node.nodeValue, node);
49             },
50             // #comment
51             8: function(node) {
52                 writer.comment(node.nodeValue);
53             },
54             // Processing instruction
55             7: function(node) {
56                 writer.pi(node.name, node.nodeValue);
57             },
58             // Doctype
59             10: function(node) {
60                 writer.doctype(node.nodeValue);
61             },
62             // CDATA
63             4: function(node) {
64                 writer.cdata(node.nodeValue);
65             },
66             // Document fragment
67             11: function(node) {
68                 node = node.firstChild;
69                 if (!node) {
70                     return;
71                 }
72                 while(node) {
73                     self.walk(node);
74                     node = node.nextSibling
75                 }
76             }
77         };
78         writer.reset();
79         1 != node.nodeType || this.inner ? this.handlers[11](node) : this.walk(node);
80         return writer.getContent();
81     },
82
83     walk: function(node)
84     {
85         var attrName, attrValue, sortedAttrs, i, l, elementRule,
86             handler = this.handlers[node.nodeType];
87             
88         if (handler) {
89             handler(node);
90             return;
91         }
92     
93         var name = node.nodeName;
94         var isEmpty = node.childNodes.length < 1;
95       
96         var writer = this.writer;
97         var attrs = node.attributes;
98         // Sort attributes
99         
100         writer.start(node.nodeName, attrs, isEmpty, node);
101         if (isEmpty) {
102             return;
103         }
104         node = node.firstChild;
105         if (!node) {
106             writer.end(name);
107             return;
108         }
109         while (node) {
110             this.walk(node);
111             node = node.nextSibling;
112         }
113         writer.end(name);
114         
115     
116     }
117     // Serialize element and treat all non elements as fragments
118    
119 }; 
120