-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
55 lines (46 loc) · 975 Bytes
/
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
/**
* dom-parser
*
* Browser compatible DOM parser.
*/
// Thanks to rails/turbolinks
function useParser(html) {
var d = new DOMParser();
return d.parseFromString(html, 'text/html');
}
function useDom(html) {
var d = document.implementation.createHTMLDocument('');
d.documentElement.innerHTML = html;
return d;
}
function useWrite(html) {
var d = document.implementation.createHTMLDocument('');
d.open('replace');
d.write(html);
d.close();
return d;
}
var snippet = '<html><body><p>test';
function getParser() {
var ref ;
try {
if (window.DOMParser) {
ref = useParser(snippet);
return useParser;
}
} catch (err) {
ref = useDom(snippet);
return useDom;
} finally {
if (!(ref && ref.body && ref.body.childNodes.length === 1)) {
return useWrite;
}
}
}
var parser = getParser();
function dom(html) {
if (parser) return parser(html);
return null;
}
dom.parser = parser;
module.exports = dom;