attributes.js 1.8 KB

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