UNPKG

1.95 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var xlinkNS = 'http://www.w3.org/1999/xlink';
4var xmlNS = 'http://www.w3.org/XML/1998/namespace';
5var colonChar = 58;
6var xChar = 120;
7function updateAttrs(oldVnode, vnode) {
8 var key, elm = vnode.elm, oldAttrs = oldVnode.data.attrs, attrs = vnode.data.attrs;
9 if (!oldAttrs && !attrs)
10 return;
11 if (oldAttrs === attrs)
12 return;
13 oldAttrs = oldAttrs || {};
14 attrs = attrs || {};
15 // update modified attributes, add new attributes
16 for (key in attrs) {
17 var cur = attrs[key];
18 var old = oldAttrs[key];
19 if (old !== cur) {
20 if (cur === true) {
21 elm.setAttribute(key, "");
22 }
23 else if (cur === false) {
24 elm.removeAttribute(key);
25 }
26 else {
27 if (key.charCodeAt(0) !== xChar) {
28 elm.setAttribute(key, cur);
29 }
30 else if (key.charCodeAt(3) === colonChar) {
31 // Assume xml namespace
32 elm.setAttributeNS(xmlNS, key, cur);
33 }
34 else if (key.charCodeAt(5) === colonChar) {
35 // Assume xlink namespace
36 elm.setAttributeNS(xlinkNS, key, cur);
37 }
38 else {
39 elm.setAttribute(key, cur);
40 }
41 }
42 }
43 }
44 // remove removed attributes
45 // use `in` operator since the previous `for` iteration uses it (.i.e. add even attributes with undefined value)
46 // the other option is to remove all attributes with value == undefined
47 for (key in oldAttrs) {
48 if (!(key in attrs)) {
49 elm.removeAttribute(key);
50 }
51 }
52}
53exports.attributesModule = { create: updateAttrs, update: updateAttrs };
54exports.default = exports.attributesModule;
55//# sourceMappingURL=attributes.js.map
\No newline at end of file