attributes.js 2.0 KB

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