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