-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrowser.js
139 lines (123 loc) · 2.51 KB
/
browser.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import { nextTick, bind } from '../signal.js'
const reverseMap = (keyValsMap) => {
const reversed = {}
for (let [key, vals] of Object.entries(keyValsMap)) {
for (let val of vals) {
reversed[val] = key
}
}
return reversed
}
const prefix = (prefix, keyArr) => Object.fromEntries(keyArr.map((i) => [i, `${prefix}${i}`]))
export const namespaces = {
xml: 'http://www.w3.org/XML/1998/namespace',
html: 'http://www.w3.org/1999/xhtml',
svg: 'http://www.w3.org/2000/svg',
math: 'http://www.w3.org/1998/Math/MathML',
xlink: 'http://www.w3.org/1999/xlink'
}
export const tagAliases = {}
const attributes = ['class', 'style', 'viewBox', 'd', 'tabindex', 'role']
const namespaceToTagsMap = {
svg: [
'animate',
'animateMotion',
'animateTransform',
'circle',
'clipPath',
'defs',
'desc',
'discard',
'ellipse',
'feBlend',
'feColorMatrix',
'feComponentTransfer',
'feComposite',
'feConvolveMatrix',
'feDiffuseLighting',
'feDisplacementMap',
'feDistantLight',
'feDropShadow',
'feFlood',
'feFuncA',
'feFuncB',
'feFuncG',
'feFuncR',
'feGaussianBlur',
'feImage',
'feMerge',
'feMergeNode',
'feMorphology',
'feOffset',
'fePointLight',
'feSpecularLighting',
'feSpotLight',
'feTile',
'feTurbulence',
'filter',
'foreignObject',
'g',
'line',
'linearGradient',
'marker',
'mask',
'metadata',
'mpath',
'path',
'pattern',
'polygon',
'polyline',
'radialGradient',
'rect',
'set',
'stop',
'svg',
'switch',
'symbol',
'text',
'textPath',
'title',
'tspan',
'unknown',
'use',
'view'
]
}
export const tagNamespaceMap = reverseMap(namespaceToTagsMap)
export const propAliases = prefix('attr:', attributes)
export const directives = {
style(key) {
return (node, val) => {
if (val === undefined || val === null) return
const styleObj = node.style
const handler = (newVal) => nextTick(() => {
if (newVal === undefined || val === null || val === false) styleObj[key] = 'unset'
else styleObj[key] = newVal
})
bind(handler, val)
}
},
class(key) {
return (node, val) => {
if (val === undefined || val === null) return
const classList = node.classList
const handler = (newVal) => nextTick(() => {
if (newVal) classList.add(key)
else classList.remove(key)
})
bind(handler, val)
}
}
}
const onDirective = (prefix, key) => {
const handler = directives[prefix]
if (handler) return handler(key)
}
export const defaults = {
doc: document,
namespaces,
tagNamespaceMap,
tagAliases,
propAliases,
onDirective
}