-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
210 lines (201 loc) · 7.09 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
self.JSDON = (function (exports) {
'use strict';
var ELEMENT_NODE = 1;
var ATTRIBUTE_NODE = 2;
var TEXT_NODE = 3;
var COMMENT_NODE = 8;
var DOCUMENT_NODE = 9;
var DOCUMENT_TYPE_NODE = 10;
var DOCUMENT_FRAGMENT_NODE = 11;
var NODE_END = -1;
var UNKNOWN = 42;
var SVG = 'http://www.w3.org/2000/svg';
var parse = JSON.parse;
/**
* Given a JSON string, or a JSDON compatible array, returns
* a DOM element representing such value.
* @param {string|Array} value
* @param {Document?} ownerDocument
* @returns {Document|DocumentFragment|Element|Text|Comment}
*/
var fromJSON = function fromJSON(value) {
var ownerDocument = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : document;
var unknown = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : Object;
var array = typeof value === 'string' ? parse(value) : value;
var length = array.length;
var fragment = ownerDocument.createDocumentFragment();
var parentNode = fragment;
var isFragment = false;
var doc = ownerDocument;
var skipCheck = true;
var i = 0;
while (i < length) {
var nodeType = array[i++];
switch (nodeType) {
case ELEMENT_NODE:
var localName = array[i++];
var lowerName = localName.toLowerCase();
var attributes = [];
var _length = 0;
var is = '';
while (array[i] === ATTRIBUTE_NODE) {
var name = array[++i];
var _value = typeof array[i + 1] === 'string' ? array[++i] : '';
if (name === 'is') is = _value;
_length = attributes.push({
name: name,
value: _value
});
i++;
}
// avoid re-creating the root element (html, svg, or root)
if (skipCheck || lowerName !== parentNode.localName.toLowerCase()) {
parentNode = parentNode.appendChild(lowerName === 'svg' || 'ownerSVGElement' in parentNode ? doc.createElementNS(SVG, localName) : is ? doc.createElement(localName, {
is: is
}) : doc.createElement(localName));
}
// TODO: setAttributeNS (meh?)
for (var j = 0; j < _length; j++) parentNode.setAttribute(attributes[j].name, attributes[j].value);
skipCheck = true;
break;
case TEXT_NODE:
parentNode.appendChild(doc.createTextNode(array[i++]));
break;
case COMMENT_NODE:
parentNode.appendChild(doc.createComment(array[i++]));
break;
case DOCUMENT_NODE:
var parser = new ownerDocument.defaultView.DOMParser();
if (array[i] === DOCUMENT_TYPE_NODE) {
i++;
var _name = array[i++];
var args = [_name];
while (typeof array[i] === 'string') args.push("\"".concat(array[i++], "\""));
switch (args.length) {
case 2:
args[1] = "".concat(/\.dtd"$/i.test(args[1]) ? 'SYSTEM' : 'PUBLIC', " ").concat(args[1]);
break;
case 3:
args[1] = "PUBLIC ".concat(args[1]);
break;
}
switch (_name) {
case 'html':
case 'HTML':
doc = parser.parseFromString("<!DOCTYPE ".concat(args.join(' '), "><html></html>"), 'text/html');
break;
/* c8 ignore start */
case 'svg':
case 'SVG':
doc = parser.parseFromString("<!DOCTYPE ".concat(args.join(' '), "><svg />"), 'image/svg+xml');
break;
default:
doc = parser.parseFromString('<root />', 'text/xml');
break;
/* c8 ignore stop */
}
} else doc = parser.parseFromString('<html></html>', 'text/html');
parentNode = doc.documentElement;
skipCheck = false;
break;
case DOCUMENT_FRAGMENT_NODE:
isFragment = true;
break;
case UNKNOWN:
parentNode.appendChild(unknown(array[i++]));
break;
default:
do {
nodeType -= NODE_END;
parentNode = parentNode.parentNode || fragment;
} while (nodeType < 0);
break;
}
}
if (isFragment) return fragment;
if (doc !== ownerDocument) return doc;
return fragment.firstChild;
};
var mergeClosing = function mergeClosing(output) {
var last = output.length - 1;
var value = output[last];
if (typeof value === 'number' && value < 0) output[last] += NODE_END;else output.push(NODE_END);
};
var pushAttribute = function pushAttribute(_ref, output) {
var name = _ref.name,
value = _ref.value;
output.push(ATTRIBUTE_NODE, name);
if (value) output.push(value);
};
var pushElement = function pushElement(_ref2, output, filter) {
var attributes = _ref2.attributes,
childNodes = _ref2.childNodes,
localName = _ref2.localName;
output.push(ELEMENT_NODE, localName);
for (var i = 0, length = attributes.length; i < length; i++) pushAttribute(attributes[i], output);
for (var _i = 0, _length = childNodes.length; _i < _length; _i++) pushNode(childNodes[_i], output, filter);
mergeClosing(output);
};
var pushFragment = function pushFragment(_ref3, output, filter) {
var childNodes = _ref3.childNodes;
for (var i = 0, length = childNodes.length; i < length; i++) pushNode(childNodes[i], output, filter);
mergeClosing(output);
};
var pushNode = function pushNode(node, output, filter, unknown) {
if (filter(node)) {
var nodeType = node.nodeType;
switch (nodeType) {
case ELEMENT_NODE:
{
pushElement(node, output, filter);
break;
}
case TEXT_NODE:
case COMMENT_NODE:
{
output.push(nodeType, node.data);
break;
}
case DOCUMENT_FRAGMENT_NODE:
case DOCUMENT_NODE:
{
output.push(nodeType);
pushFragment(node, output, filter);
break;
}
case DOCUMENT_TYPE_NODE:
{
var name = node.name,
publicId = node.publicId,
systemId = node.systemId;
output.push(nodeType, name);
if (publicId) output.push(publicId);
if (systemId) output.push(systemId);
break;
}
default:
{
output.push(UNKNOWN, unknown(node));
break;
}
}
}
};
var yes = function yes() {
return true;
};
/**
* Given a generic DOM element, returns a JSDON compatible array that represents it.
* @param {Document|DocumentFragment|Element|Text|Comment} node
* @param {function?} filter if provided, filters nodes by returning `true` or `false`
*/
var toJSON = function toJSON(node, filter) {
var unknown = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : Object;
var output = [];
pushNode(node, output, filter || yes, unknown);
return output;
};
exports.fromJSON = fromJSON;
exports.toJSON = toJSON;
return exports;
})({});