This repository has been archived by the owner on Oct 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 89
/
_dom_parser.js.coffee
50 lines (43 loc) · 1.69 KB
/
_dom_parser.js.coffee
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
class DOMParser
parse: (html) ->
@_get_parser()(html)
_get_parser: ->
@_document_parser ?= @_parser_factory()
# fully taken from Turbolinks (https://github.com/rails/turbolinks)
# and changed function names style
# by @igor-alexandrov request :)
_parser_factory: ->
create_document_using_parser = (html) ->
(new DOMParser).parseFromString html, 'text/html'
create_document_using_DOM = (html) ->
doc = document.implementation.createHTMLDocument ''
doc.documentElement.innerHTML = html
doc
create_document_using_write = (html) ->
doc = document.implementation.createHTMLDocument ''
doc.open 'replace'
doc.write html
doc.close()
doc
# Use create_document_using_parser if DOMParser is defined and natively
# supports 'text/html' parsing (Firefox 12+, IE 10)
#
# Use create_document_using_DOM if create_document_using_parser throws an exception
# due to unsupported type 'text/html' (Firefox < 12, Opera)
#
# Use create_document_using_write if:
# - DOMParser isn't defined
# - create_document_using_parser returns null due to unsupported type 'text/html' (Chrome, Safari)
# - create_document_using_DOM doesn't create a valid HTML document (safeguarding against potential edge cases)
try
if window.DOMParser
testDoc = create_document_using_parser '<html><body><p>test'
create_document_using_parser
catch e
testDoc = create_document_using_DOM '<html><body><p>test'
create_document_using_DOM
finally
unless testDoc?.body?.childNodes.length is 1
return create_document_using_write
window._Wiselinks ?= {}
window._Wiselinks.DOMParser = DOMParser