-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathattributes.ts
50 lines (44 loc) · 1.6 KB
/
attributes.ts
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
import { VNode, Module } from '../types';
const booleanAttrs = [
'allowfullscreen', 'async', 'autofocus', 'autoplay', 'checked', 'compact', 'controls', 'declare',
'default', 'defaultchecked', 'defaultmuted', 'defaultselected', 'defer', 'disabled', 'draggable',
'enabled', 'formnovalidate', 'hidden', 'indeterminate', 'inert', 'ismap', 'itemscope', 'loop', 'multiple',
'muted', 'nohref', 'noresize', 'noshade', 'novalidate', 'nowrap', 'open', 'pauseonexit', 'readonly',
'required', 'reversed', 'scoped', 'seamless', 'selected', 'sortable', 'spellcheck', 'translate',
'truespeed', 'typemustmatch', 'visible',
];
const booleanAttrsDict: any = {};
for (let i = 0, len = booleanAttrs.length; i < len; i++) {
booleanAttrsDict[booleanAttrs[i]] = true;
}
function updateAttrs(oldVnode: VNode, vnode: VNode) {
let key: any;
let cur: any;
let old: any;
let elm = vnode.elm as HTMLElement;
let oldAttrs = oldVnode.data && oldVnode.data.attrs || {};
let attrs = vnode.data && vnode.data.attrs || {};
// update modified attributes, add new attributes
for (key in attrs) {
cur = attrs[key];
old = oldAttrs[key];
if (old !== cur) {
// TODO: add support to namespaced attributes (setAttributeNS)
if (!cur && booleanAttrsDict[key]) {
(<HTMLElement> elm).removeAttribute(key);
} else {
(<HTMLElement> elm).setAttribute(key, cur);
}
}
}
//remove removed attributes
for (key in oldAttrs) {
if (!(key in attrs)) {
(<HTMLElement> elm).removeAttribute(key);
}
}
}
export const AttrsModule: Module = {
update: updateAttrs,
create: updateAttrs,
};