| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
const root = (typeof window !== 'undefined' ? window : {}) |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
function canParseHTMLNatively () { |
| 12 |
const Parser = root.DOMParser |
| 13 |
let canParse = false |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
try { |
| 18 |
|
| 19 |
if (new Parser().parseFromString('', 'text/html')) { |
| 20 |
canParse = true |
| 21 |
} |
| 22 |
} catch (e) {} |
| 23 |
|
| 24 |
return canParse |
| 25 |
} |
| 26 |
|
| 27 |
function createHTMLParser () { |
| 28 |
const Parser = function () {} |
| 29 |
|
| 30 |
if (process.browser) { |
| 31 |
if (shouldUseActiveX()) { |
| 32 |
Parser.prototype.parseFromString = function (string) { |
| 33 |
const doc = new window.ActiveXObject('htmlfile') |
| 34 |
doc.designMode = 'on' |
| 35 |
doc.open() |
| 36 |
doc.write(string) |
| 37 |
doc.close() |
| 38 |
return doc |
| 39 |
} |
| 40 |
} else { |
| 41 |
Parser.prototype.parseFromString = function (string) { |
| 42 |
const doc = document.implementation.createHTMLDocument('') |
| 43 |
doc.open() |
| 44 |
doc.write(string) |
| 45 |
doc.close() |
| 46 |
return doc |
| 47 |
} |
| 48 |
} |
| 49 |
} else { |
| 50 |
const domino = require('@mixmark-io/domino') |
| 51 |
Parser.prototype.parseFromString = function (string) { |
| 52 |
return domino.createDocument(string) |
| 53 |
} |
| 54 |
} |
| 55 |
return Parser |
| 56 |
} |
| 57 |
|
| 58 |
function shouldUseActiveX () { |
| 59 |
let useActiveX = false |
| 60 |
try { |
| 61 |
document.implementation.createHTMLDocument('').open() |
| 62 |
} catch (e) { |
| 63 |
if (root.ActiveXObject) useActiveX = true |
| 64 |
} |
| 65 |
return useActiveX |
| 66 |
} |
| 67 |
|
| 68 |
export default canParseHTMLNatively() ? root.DOMParser : createHTMLParser() |