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.apply(Roo.htmleditor.TidySerializer.prototype, {
24     
25     /**
26      * @param {boolean} inner do the inner of the node.
27      */
28     inner : 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         this.handlers = {
44             // #text
45             3: function(node) {
46                 writer.text(node.value, node.raw);
47             },
48             // #comment
49             8: function(node) {
50                 writer.comment(node.value);
51             },
52             // Processing instruction
53             7: function(node) {
54                 writer.pi(node.name, node.value);
55             },
56             // Doctype
57             10: function(node) {
58                 writer.doctype(node.value);
59             },
60             // CDATA
61             4: function(node) {
62                 writer.cdata(node.value);
63             },
64             // Document fragment
65             11: function(node) {
66                 node = node.firstChild;
67                 if (!node) {
68                     return;
69                 }
70                 while(node) {
71                     walk(node);
72                     node = node.nextSibling
73                 }
74             }
75         };
76         writer.reset();
77         1 != node.nodeType || this.inner ? handlers[11](node) : walk(node);
78     return writer.getContent();
79
80     function walk(node) {
81         var name, isEmpty, attrs, attrName, attrValue, sortedAttrs, i, l, elementRule, handler = handlers[node.type];
82         if (handler) {
83             handler(node);
84         } else {
85             name = node.name;
86             isEmpty = node.shortEnded;
87             attrs = node.attributes;
88             // Sort attributes
89             if (validate && attrs && attrs.length > 1) {
90                 sortedAttrs = [];
91                 sortedAttrs.map = {};
92                 elementRule = schema.getElementRule(node.name);
93                 if (elementRule) {
94                     for (i = 0, l = elementRule.attributesOrder.length; i < l; i++) {
95                         attrName = elementRule.attributesOrder[i];
96                         if (attrName in attrs.map) {
97                             attrValue = attrs.map[attrName];
98                             sortedAttrs.map[attrName] = attrValue;
99                             sortedAttrs.push({
100                                 name: attrName,
101                                 value: attrValue
102                             });
103                         }
104                     }
105                     for (i = 0, l = attrs.length; i < l; i++) {
106                         attrName = attrs[i].name;
107                         if (!(attrName in sortedAttrs.map)) {
108                             attrValue = attrs.map[attrName];
109                             sortedAttrs.map[attrName] = attrValue;
110                             sortedAttrs.push({
111                                 name: attrName,
112                                 value: attrValue
113                             });
114                         }
115                     }
116                     attrs = sortedAttrs;
117                 }
118             }
119             writer.start(node.name, attrs, isEmpty);
120             if (!isEmpty) {
121                 if (node = node.firstChild) {
122                     do {
123                         walk(node);
124                     } while (node = node.next);
125                 }
126                 writer.end(name);
127             }
128         }
129     }
130     // Serialize element and treat all non elements as fragments
131    
132 };
133 };
134