4 * Copyright, Moxiecode Systems AB
5 * Released under LGPL License.
7 * License: http://www.tinymce.com/license
8 * Contributing: http://www.tinymce.com/contributing
12 * This class is used to serialize down the DOM tree into a string using a Writer instance.
16 * new tinymce.html.Serializer().serialize(new tinymce.html.DomParser().parse('<p>text</p>'));
17 * @class tinymce.html.Serializer
20 define("tinymce/html/Serializer", [
21 "tinymce/html/Writer",
23 ], function(Writer, Schema) {
25 * Constructs a new Serializer instance.
29 * @param {Object} settings Name/value settings object.
30 * @param {tinymce.html.Schema} schema Schema instance to use.
32 return function(settings, schema) {
33 var self = this, writer = new Writer(settings);
35 settings = settings || {};
36 settings.validate = "validate" in settings ? settings.validate : true;
38 self.schema = schema = schema || new Schema();
42 * Serializes the specified node into a string.
45 * new tinymce.html.Serializer().serialize(new tinymce.html.DomParser().parse('<p>text</p>'));
47 * @param {tinymce.html.Node} node Node instance to serialize.
48 * @return {String} String with HTML based on DOM tree.
50 self.serialize = function(node) {
51 var handlers, validate;
53 validate = settings.validate;
58 writer.text(node.value, node.raw);
63 writer.comment(node.value);
66 // Processing instruction
68 writer.pi(node.name, node.value);
73 writer.doctype(node.value);
78 writer.cdata(node.value);
83 if ((node = node.firstChild)) {
86 } while ((node = node.next));
94 var handler = handlers[node.type], name, isEmpty, attrs, attrName, attrValue, sortedAttrs, i, l, elementRule;
98 isEmpty = node.shortEnded;
99 attrs = node.attributes;
102 if (validate && attrs && attrs.length > 1) {
104 sortedAttrs.map = {};
106 elementRule = schema.getElementRule(node.name);
107 for (i = 0, l = elementRule.attributesOrder.length; i < l; i++) {
108 attrName = elementRule.attributesOrder[i];
110 if (attrName in attrs.map) {
111 attrValue = attrs.map[attrName];
112 sortedAttrs.map[attrName] = attrValue;
113 sortedAttrs.push({name: attrName, value: attrValue});
117 for (i = 0, l = attrs.length; i < l; i++) {
118 attrName = attrs[i].name;
120 if (!(attrName in sortedAttrs.map)) {
121 attrValue = attrs.map[attrName];
122 sortedAttrs.map[attrName] = attrValue;
123 sortedAttrs.push({name: attrName, value: attrValue});
130 writer.start(node.name, attrs, isEmpty);
133 if ((node = node.firstChild)) {
136 } while ((node = node.next));
146 // Serialize element and treat all non elements as fragments
147 if (node.type == 1 && !settings.inner) {
153 return writer.getContent();