Roo/htmleditor/TidySerializer.js
[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  * @param {tinymce.html.Schema} schema Schema instance to use.
10  */
11
12
13 Roo.htmleditor.TidySerializer = function(settings)
14 {
15     Roo.apply(this, settings);
16     
17     this.writer = new Roo.htmleditor.TidyWriter(settings);
18     
19     //settings.validate = !('validate' in settings) || settings.validate;
20     //      self.schema = schema = schema || new Schema();
21
22 };
23 Roo.htmleditor.TidySerializer.prototype = {
24     
25     /**
26      * @param {boolean} inner do the inner of the node.
27      */
28     inner : false,
29     
30     writer : false,
31     
32     /**
33     * Serializes the specified node into a string.
34     *
35     * @example
36     * new tinymce.html.Serializer().serialize(new tinymce.html.DomParser().parse('<p>text</p>'));
37     * @method serialize
38     * @param {DomElement} node Node instance to serialize.
39     * @return {String} String with HTML based on DOM tree.
40     */
41     serialize : function(node) {
42         
43         // = settings.validate;
44         var writer = this.writer;
45         var self  = this;
46         this.handlers = {
47             // #text
48             3: function(node) {
49                 
50                 writer.text(node.nodeValue, node);
51             },
52             // #comment
53             8: function(node) {
54                 writer.comment(node.nodeValue);
55             },
56             // Processing instruction
57             7: function(node) {
58                 writer.pi(node.name, node.nodeValue);
59             },
60             // Doctype
61             10: function(node) {
62                 writer.doctype(node.nodeValue);
63             },
64             // CDATA
65             4: function(node) {
66                 writer.cdata(node.nodeValue);
67             },
68             // Document fragment
69             11: function(node) {
70                 node = node.firstChild;
71                 if (!node) {
72                     return;
73                 }
74                 while(node) {
75                     self.walk(node);
76                     node = node.nextSibling
77                 }
78             }
79         };
80         writer.reset();
81         1 != node.nodeType || this.inner ? this.handlers[11](node) : this.walk(node);
82         return writer.getContent();
83     },
84
85     walk: function(node)
86     {
87         var attrName, attrValue, sortedAttrs, i, l, elementRule,
88             handler = this.handlers[node.nodeType];
89             
90         if (handler) {
91             handler(node);
92             return;
93         }
94     
95         var name = node.nodeName;
96         var isEmpty = node.childNodes.length < 1;
97       
98         var writer = this.writer;
99         var attrs = node.attributes;
100         // Sort attributes
101         /*
102         if (validate && attrs && attrs.length > 1) {
103             sortedAttrs = [];
104             sortedAttrs.map = {};
105             elementRule = schema.getElementRule(node.name);
106             if (elementRule) {
107                 for (i = 0, l = elementRule.attributesOrder.length; i < l; i++) {
108                     attrName = elementRule.attributesOrder[i];
109                     if (attrName in attrs.map) {
110                         attrValue = attrs.map[attrName];
111                         sortedAttrs.map[attrName] = attrValue;
112                         sortedAttrs.push({
113                             name: attrName,
114                             value: attrValue
115                         });
116                     }
117                 }
118                 for (i = 0, l = attrs.length; i < l; i++) {
119                     attrName = attrs[i].name;
120                     if (!(attrName in sortedAttrs.map)) {
121                         attrValue = attrs.map[attrName];
122                         sortedAttrs.map[attrName] = attrValue;
123                         sortedAttrs.push({
124                             name: attrName,
125                             value: attrValue
126                         });
127                     }
128                 }
129                 attrs = sortedAttrs;
130             }
131         }
132         */
133         writer.start(node.nodeName, attrs, isEmpty, node);
134         if (isEmpty) {
135             return;
136         }
137         node = node.firstChild;
138         if (!node) {
139             writer.end(name);
140             return;
141         }
142         while (node) {
143             this.walk(node);
144             node = node.nextSibling;
145         }
146         writer.end(name);
147         
148     
149     }
150     // Serialize element and treat all non elements as fragments
151    
152 }; 
153