-
Notifications
You must be signed in to change notification settings - Fork 29
/
jsonml-dom.js
222 lines (193 loc) · 5.07 KB
/
jsonml-dom.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
/*
jsonml-dom.js
HTML to JsonML utility
Created: 2007-02-15-2235
Modified: 2012-11-03-2051
Copyright (c)2006-2012 Stephen M. McKamey
Distributed under The MIT License: http://jsonml.org/license
*/
var JsonML = JsonML || {};
if (typeof module === 'object') {
module.exports = JsonML;
}
(function(JsonML, document){
'use strict';
var addChildren = function(/*DOM*/ elem, /*function*/ filter, /*JsonML*/ jml) {
if (elem.hasChildNodes()) {
for (var i=0; i<elem.childNodes.length; i++) {
var child = elem.childNodes[i];
child = fromHTML(child, filter);
if (child) {
jml.push(child);
}
}
return true;
}
return false;
};
/**
* @param {Node} elem
* @param {function} filter
* @return {array} JsonML
*/
var fromHTML = JsonML.fromHTML = function(elem, filter) {
if (!elem || !elem.nodeType) {
// free references
return (elem = null);
}
var i, jml;
switch (elem.nodeType) {
case 1: // element
case 9: // document
case 11: // documentFragment
jml = [elem.tagName||''];
var attr = elem.attributes,
props = {},
hasAttrib = false;
for (i=0; attr && i<attr.length; i++) {
if (attr[i].specified) {
if (attr[i].name === 'style') {
props.style = elem.style.cssText || attr[i].value;
} else if ('string' === typeof attr[i].value) {
props[attr[i].name] = attr[i].value;
}
hasAttrib = true;
}
}
if (hasAttrib) {
jml.push(props);
}
var child;
switch (jml[0].toLowerCase()) {
case 'frame':
case 'iframe':
try {
if ('undefined' !== typeof elem.contentDocument) {
// W3C
child = elem.contentDocument;
} else if ('undefined' !== typeof elem.contentWindow) {
// Microsoft
child = elem.contentWindow.document;
} else if ('undefined' !== typeof elem.document) {
// deprecated
child = elem.document;
}
child = fromHTML(child, filter);
if (child) {
jml.push(child);
}
} catch (ex) {}
break;
case 'style':
child = elem.styleSheet && elem.styleSheet.cssText;
if (child && 'string' === typeof child) {
// unwrap comment blocks
child = child.replace('<!--', '').replace('-->', '');
jml.push(child);
} else if (elem.hasChildNodes()) {
for (i=0; i<elem.childNodes.length; i++) {
child = elem.childNodes[i];
child = fromHTML(child, filter);
if (child && 'string' === typeof child) {
// unwrap comment blocks
child = child.replace('<!--', '').replace('-->', '');
jml.push(child);
}
}
}
break;
case 'input':
addChildren(elem, filter, jml);
child = (elem.type !== 'password') && elem.value;
if (child) {
if (!hasAttrib) {
// need to add an attribute object
jml.shift();
props = {};
jml.unshift(props);
jml.unshift(elem.tagName||'');
}
props.value = child;
}
break;
case 'textarea':
if (!addChildren(elem, filter, jml)) {
child = elem.value || elem.innerHTML;
if (child && 'string' === typeof child) {
jml.push(child);
}
}
break;
default:
addChildren(elem, filter, jml);
break;
}
// filter result
if ('function' === typeof filter) {
jml = filter(jml, elem);
}
// free references
elem = null;
return jml;
case 3: // text node
case 4: // CDATA node
var str = String(elem.nodeValue);
// free references
elem = null;
return str;
case 10: // doctype
jml = ['!'];
var type = ['DOCTYPE', (elem.name || 'html').toLowerCase()];
if (elem.publicId) {
type.push('PUBLIC', '"' + elem.publicId + '"');
}
if (elem.systemId) {
type.push('"' + elem.systemId + '"');
}
jml.push(type.join(' '));
// filter result
if ('function' === typeof filter) {
jml = filter(jml, elem);
}
// free references
elem = null;
return jml;
case 8: // comment node
if ((elem.nodeValue||'').indexOf('DOCTYPE') !== 0) {
// free references
elem = null;
return null;
}
jml = ['!',
elem.nodeValue];
// filter result
if ('function' === typeof filter) {
jml = filter(jml, elem);
}
// free references
elem = null;
return jml;
default: // etc.
// free references
return (elem = null);
}
};
/**
* @param {string} html HTML text
* @param {function} filter
* @return {array} JsonML
*/
JsonML.fromHTMLText = function(html, filter) {
var elem = document.createElement('div');
elem.innerHTML = html;
var jml = fromHTML(elem, filter);
// free references
elem = null;
if (jml.length === 2) {
return jml[1];
}
// make wrapper a document fragment
jml[0] = '';
return jml;
};
})(JsonML, document);