-
Notifications
You must be signed in to change notification settings - Fork 13
/
index.js
233 lines (192 loc) · 5.58 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/**
* Module dependencies.
*/
var extend = require('extend');
var encode = require('ent/encode');
var CustomEvent = require('custom-event');
var voidElements = require('void-elements').reduce(function (obj, name) {
obj[name] = true;
return obj;
}, {});
/**
* Module exports.
*/
exports = module.exports = serialize;
exports.serializeElement = serializeElement;
exports.serializeAttribute = serializeAttribute;
exports.serializeText = serializeText;
exports.serializeComment = serializeComment;
exports.serializeDocument = serializeDocument;
exports.serializeDoctype = serializeDoctype;
exports.serializeDocumentFragment = serializeDocumentFragment;
exports.serializeNodeList = serializeNodeList;
/**
* Serializes any DOM node. Returns a string.
*
* @param {Node} node - DOM Node to serialize
* @param {String} [context] - optional arbitrary "context" string to use (useful for event listeners)
* @param {Function} [fn] - optional callback function to use in the "serialize" event for this call
* @param {EventTarget} [eventTarget] - optional EventTarget instance to emit the "serialize" event on (defaults to `node`)
* return {String}
* @public
*/
function serialize (node, context, fn, eventTarget) {
if (!node) return '';
if ('function' === typeof context) {
fn = context;
context = null;
}
if (!context) context = null;
var rtn;
var nodeType = node.nodeType;
if (!nodeType && 'number' === typeof node.length) {
// assume it's a NodeList or Array of Nodes
rtn = exports.serializeNodeList(node, context, fn);
} else {
if ('function' === typeof fn) {
// one-time "serialize" event listener
node.addEventListener('serialize', fn, false);
}
// emit a custom "serialize" event on `node`, in case there
// are event listeners for custom serialization of this node
var e = new CustomEvent('serialize', {
bubbles: true,
cancelable: true,
detail: {
serialize: null,
context: context
}
});
e.serializeTarget = node;
var target = eventTarget || node;
var cancelled = !target.dispatchEvent(e);
// `e.detail.serialize` can be set to a:
// String - returned directly
// Node - goes through serializer logic instead of `node`
// Anything else - get Stringified first, and then returned directly
var s = e.detail.serialize;
if (s != null) {
if ('string' === typeof s) {
rtn = s;
} else if ('number' === typeof s.nodeType) {
// make it go through the serialization logic
rtn = serialize(s, context, null, target);
} else {
rtn = String(s);
}
} else if (!cancelled) {
// default serialization logic
switch (nodeType) {
case 1 /* element */:
rtn = exports.serializeElement(node, context, eventTarget);
break;
case 2 /* attribute */:
rtn = exports.serializeAttribute(node);
break;
case 3 /* text */:
rtn = exports.serializeText(node);
break;
case 8 /* comment */:
rtn = exports.serializeComment(node);
break;
case 9 /* document */:
rtn = exports.serializeDocument(node, context, eventTarget);
break;
case 10 /* doctype */:
rtn = exports.serializeDoctype(node);
break;
case 11 /* document fragment */:
rtn = exports.serializeDocumentFragment(node, context, eventTarget);
break;
}
}
if ('function' === typeof fn) {
node.removeEventListener('serialize', fn, false);
}
}
return rtn || '';
}
/**
* Serialize an Attribute node.
*/
function serializeAttribute (node, opts) {
return node.name + '="' + encode(node.value, extend({
named: true
}, opts)) + '"';
}
/**
* Serialize a DOM element.
*/
function serializeElement (node, context, eventTarget) {
var c, i, l;
var name = node.nodeName.toLowerCase();
// opening tag
var r = '<' + name;
// attributes
for (i = 0, c = node.attributes, l = c.length; i < l; i++) {
r += ' ' + exports.serializeAttribute(c[i]);
}
r += '>';
// child nodes
r += exports.serializeNodeList(node.childNodes, context, null, eventTarget);
// closing tag, only for non-void elements
if (!voidElements[name]) {
r += '</' + name + '>';
}
return r;
}
/**
* Serialize a text node.
*/
function serializeText (node, opts) {
return encode(node.nodeValue, extend({
named: true,
special: { '<': true, '>': true, '&': true }
}, opts));
}
/**
* Serialize a comment node.
*/
function serializeComment (node) {
return '<!--' + node.nodeValue + '-->';
}
/**
* Serialize a Document node.
*/
function serializeDocument (node, context, eventTarget) {
return exports.serializeNodeList(node.childNodes, context, null, eventTarget);
}
/**
* Serialize a DOCTYPE node.
* See: http://stackoverflow.com/a/10162353
*/
function serializeDoctype (node) {
var r = '<!DOCTYPE ' + node.name;
if (node.publicId) {
r += ' PUBLIC "' + node.publicId + '"';
}
if (!node.publicId && node.systemId) {
r += ' SYSTEM';
}
if (node.systemId) {
r += ' "' + node.systemId + '"';
}
r += '>';
return r;
}
/**
* Serialize a DocumentFragment instance.
*/
function serializeDocumentFragment (node, context, eventTarget) {
return exports.serializeNodeList(node.childNodes, context, null, eventTarget);
}
/**
* Serialize a NodeList/Array of nodes.
*/
function serializeNodeList (list, context, fn, eventTarget) {
var r = '';
for (var i = 0, l = list.length; i < l; i++) {
r += serialize(list[i], context, fn, eventTarget);
}
return r;
}