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     * Serializes the specified node into a string.
26     *
27     * @example
28     * new tinymce.html.Serializer().serialize(new tinymce.html.DomParser().parse('<p>text</p>'));
29     * @method serialize
30     * @param {DomElement} node Node instance to serialize.
31     * @return {String} String with HTML based on DOM tree.
32     */
33     serialize : function(node) {
34         
35         // = settings.validate;
36         var writer = this.writer;
37         this.handlers = {
38             // #text
39             3: function(node) {
40                 writer.text(node.value, node.raw);
41             },
42             // #comment
43             8: function(node) {
44                 writer.comment(node.value);
45             },
46             // Processing instruction
47             7: function(node) {
48                 writer.pi(node.name, node.value);
49             },
50             // Doctype
51             10: function(node) {
52                 writer.doctype(node.value);
53             },
54             // CDATA
55             4: function(node) {
56                 writer.cdata(node.value);
57             },
58             // Document fragment
59             11: function(node) {
60                 node = node.firstChild;
61                 if (!node) {
62                     return;
63                 }
64                 while(node) {
65                     walk(node);
66                     node = node.nextSibling
67                 }
68             }
69         };
70         writer.reset();
71
72     function walk(node) {
73         var name, isEmpty, attrs, attrName, attrValue, sortedAttrs, i, l, elementRule, handler = handlers[node.type];
74         if (handler) {
75             handler(node);
76         } else {
77             name = node.name;
78             isEmpty = node.shortEnded;
79             attrs = node.attributes;
80             // Sort attributes
81             if (validate && attrs && attrs.length > 1) {
82                 sortedAttrs = [];
83                 sortedAttrs.map = {};
84                 elementRule = schema.getElementRule(node.name);
85                 if (elementRule) {
86                     for (i = 0, l = elementRule.attributesOrder.length; i < l; i++) {
87                         attrName = elementRule.attributesOrder[i];
88                         if (attrName in attrs.map) {
89                             attrValue = attrs.map[attrName];
90                             sortedAttrs.map[attrName] = attrValue;
91                             sortedAttrs.push({
92                                 name: attrName,
93                                 value: attrValue
94                             });
95                         }
96                     }
97                     for (i = 0, l = attrs.length; i < l; i++) {
98                         attrName = attrs[i].name;
99                         if (!(attrName in sortedAttrs.map)) {
100                             attrValue = attrs.map[attrName];
101                             sortedAttrs.map[attrName] = attrValue;
102                             sortedAttrs.push({
103                                 name: attrName,
104                                 value: attrValue
105                             });
106                         }
107                     }
108                     attrs = sortedAttrs;
109                 }
110             }
111             writer.start(node.name, attrs, isEmpty);
112             if (!isEmpty) {
113                 if (node = node.firstChild) {
114                     do {
115                         walk(node);
116                     } while (node = node.next);
117                 }
118                 writer.end(name);
119             }
120         }
121     }
122     // Serialize element and treat all non elements as fragments
123     1 != node.type || settings.inner ? handlers[11](node) : walk(node);
124     return writer.getContent();
125 };
126 };
127