-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
index.es
60 lines (45 loc) · 1.43 KB
/
index.es
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
//Template =>
// Template.foo
// Template ['...']
// document.getElementsByTagName (tag)[name]
// innerHTML issues
// http://kieranpotts.com/blog/javascript-html-to-dom
// https://lists.w3.org/Archives/Public/public-webapps/2012AprJun/0334.html#start334
// investigate `Text.splitText ()`
// Recurse through elements and bind event handlers
// https://developer.mozilla.org/en-US/docs/Web/API/Text/splitText
//
// Greatly improve <template> implementaiton
// https://github.com/tmpvar/jsdom/commit/ceb79457dd01a19f56a615cf6a78598be8ed36b8
const Template = template => {
let
range = document.createRange ()
template
= typeof template === 'string'
? document.querySelector ( 'template[name=' + template + ']' )
: template
range.selectNodeContents ( template.content )
let
fragment = range.cloneContents ()
, tokenize = (context, index) => {
let
clone = fragment.cloneNode (true)
typeof context != 'object'
&& ( context = { self: context })
context ['#'] = index
void (new TokenList (clone))
.bind (context)
return clone
} // tokenize
, bind = context => {
range.deleteContents ()
context && []
.concat (context)
.map (tokenize)
.reverse () // Range.insertNode does prepend
.map (fragment => range.insertNode (fragment))
}
range.setStartAfter (template)
template.bind = bind
return template
} // Template