Repository navigation
Expand file tree
/
Copy pathhtml-parser.js
More file actions
68 lines (59 loc) · 1.56 KB
/
Copy pathhtml-parser.js
File metadata and controls
68 lines (59 loc) · 1.56 KB
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
/*
* Set up window for Node.js
*/
var root = (typeof window !== 'undefined' ? window : {})
/*
* Parsing HTML strings
*/
function canParseHTMLNatively () {
var Parser = root.DOMParser
var canParse = false
// Adapted from https://gist.github.com/1129031
// Firefox/Opera/IE throw errors on unsupported types
try {
// WebKit returns null on unsupported types
if (new Parser().parseFromString('', 'text/html')) {
canParse = true
}
} catch (e) {}
return canParse
}
function createHTMLParser () {
var Parser = function () {}
if (process.browser) {
if (shouldUseActiveX()) {
Parser.prototype.parseFromString = function (string) {
var doc = new window.ActiveXObject('htmlfile')
doc.designMode = 'on' // disable on-page scripts
doc.open()
doc.write(string)
doc.close()
return doc
}
} else {
Parser.prototype.parseFromString = function (string) {
var doc = document.implementation.createHTMLDocument('')
doc.open()
doc.write(string)
doc.close()
return doc
}
}
} else {
var domino = require('@mixmark-io/domino')
Parser.prototype.parseFromString = function (string) {
return domino.createDocument(string)
}
}
return Parser
}
function shouldUseActiveX () {
var useActiveX = false
try {
document.implementation.createHTMLDocument('').open()
} catch (e) {
if (root.ActiveXObject) useActiveX = true
}
return useActiveX
}
export default canParseHTMLNatively() ? root.DOMParser : createHTMLParser()