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         1 != node.type || this.inner ? handlers[11](node) : walk(node);
72     return writer.getContent();
73
74     function walk(node) {
75         var name, isEmpty, attrs, attrName, attrValue, sortedAttrs, i, l, elementRule, handler = handlers[node.type];
76         if (handler) {
77             handler(node);
78         } else {
79             name = node.name;
80             isEmpty = node.shortEnded;
81             attrs = node.attributes;
82             // Sort attributes
83             if (validate && attrs && attrs.length > 1) {
84                 sortedAttrs = [];
85                 sortedAttrs.map = {};
86                 elementRule = schema.getElementRule(node.name);
87                 if (elementRule) {
88                     for (i = 0, l = elementRule.attributesOrder.length; i < l; i++) {
89                         attrName = elementRule.attributesOrder[i];
90                         if (attrName in attrs.map) {
91                             attrValue = attrs.map[attrName];
92                             sortedAttrs.map[attrName] = attrValue;
93                             sortedAttrs.push({
94                                 name: attrName,
95                                 value: attrValue
96                             });
97                         }
98                     }
99                     for (i = 0, l = attrs.length; i < l; i++) {
100                         attrName = attrs[i].name;
101                         if (!(attrName in sortedAttrs.map)) {
102                             attrValue = attrs.map[attrName];
103                             sortedAttrs.map[attrName] = attrValue;
104                             sortedAttrs.push({
105                                 name: attrName,
106                                 value: attrValue
107                             });
108                         }
109                     }
110                     attrs = sortedAttrs;
111                 }
112             }
113             writer.start(node.name, attrs, isEmpty);
114             if (!isEmpty) {
115                 if (node = node.firstChild) {
116                     do {
117                         walk(node);
118                     } while (node = node.next);
119                 }
120                 writer.end(name);
121             }
122         }
123     }
124     // Serialize element and treat all non elements as fragments
125    
126 };
127 };
128