From 12bd7dd7703d173682634cdb65eba7c468c02e73 Mon Sep 17 00:00:00 2001 From: Matt Kaemmerer Date: Mon, 17 Oct 2016 13:09:59 -0500 Subject: [PATCH 1/3] Call setAttributeNS instead of setAttribute for keys with a known namespace --- modules/attributes.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/modules/attributes.js b/modules/attributes.js index 9a50259..7456277 100644 --- a/modules/attributes.js +++ b/modules/attributes.js @@ -1,3 +1,7 @@ +var NamespaceURIs = { + "xlink": "http://www.w3.org/1999/xlink" +}; + var 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", @@ -12,7 +16,7 @@ for(var i=0, len = booleanAttrs.length; i < len; i++) { function updateAttrs(oldVnode, vnode) { var key, cur, old, elm = vnode.elm, - oldAttrs = oldVnode.data.attrs, attrs = vnode.data.attrs; + oldAttrs = oldVnode.data.attrs, attrs = vnode.data.attrs, namespace; if (!oldAttrs && !attrs) return; oldAttrs = oldAttrs || {}; @@ -22,10 +26,12 @@ function updateAttrs(oldVnode, vnode) { for (key in attrs) { cur = attrs[key]; old = oldAttrs[key]; + namespace = key.split(":")[0]; if (old !== cur) { - // TODO: add support to namespaced attributes (setAttributeNS) if(!cur && booleanAttrsDict[key]) elm.removeAttribute(key); + else if(key.match(/:/) && NamespaceURIs.hasOwnProperty(namespace)) + elm.setAttributeNS(NamespaceURIs[namespace], key, cur); else elm.setAttribute(key, cur); } From 73996327956ebe7581ee8e919d0847a4c8d2bfa4 Mon Sep 17 00:00:00 2001 From: Matt Kaemmerer Date: Mon, 17 Oct 2016 13:16:07 -0500 Subject: [PATCH 2/3] Add test for namespaced attributes --- test/attributes.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test/attributes.js b/test/attributes.js index 1ab372c..532ac42 100644 --- a/test/attributes.js +++ b/test/attributes.js @@ -26,6 +26,11 @@ describe('attributes', function() { assert.strictEqual(elm.getAttribute('minlength'), '0'); assert.strictEqual(elm.getAttribute('value'), 'false'); }); + it('are set correctly when namespaced', function() { + var vnode1 = h('div', {attrs: {'xlink:href': '#foo'}}); + elm = patch(vnode0, vnode1).elm; + assert.strictEqual(elm.getAttributeNS('http://www.w3.org/1999/xlink', 'href'), '#foo'); + }); describe('boolean attribute', function() { it('is present if the value is truthy', function() { var vnode1 = h('div', {attrs: {required: true, readonly: 1, noresize: 'truthy'}}); From e1d93dca976c26ce33e7be98e5011c7b81b91c02 Mon Sep 17 00:00:00 2001 From: Matt Kaemmerer Date: Wed, 19 Oct 2016 22:24:43 -0500 Subject: [PATCH 3/3] Defer splitting namespace until needed --- modules/attributes.js | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/modules/attributes.js b/modules/attributes.js index 7456277..e486bc7 100644 --- a/modules/attributes.js +++ b/modules/attributes.js @@ -16,7 +16,7 @@ for(var i=0, len = booleanAttrs.length; i < len; i++) { function updateAttrs(oldVnode, vnode) { var key, cur, old, elm = vnode.elm, - oldAttrs = oldVnode.data.attrs, attrs = vnode.data.attrs, namespace; + oldAttrs = oldVnode.data.attrs, attrs = vnode.data.attrs, namespaceSplit; if (!oldAttrs && !attrs) return; oldAttrs = oldAttrs || {}; @@ -26,14 +26,16 @@ function updateAttrs(oldVnode, vnode) { for (key in attrs) { cur = attrs[key]; old = oldAttrs[key]; - namespace = key.split(":")[0]; if (old !== cur) { if(!cur && booleanAttrsDict[key]) elm.removeAttribute(key); - else if(key.match(/:/) && NamespaceURIs.hasOwnProperty(namespace)) - elm.setAttributeNS(NamespaceURIs[namespace], key, cur); - else - elm.setAttribute(key, cur); + else { + namespaceSplit = key.split(":"); + if(namespaceSplit.length > 1 && NamespaceURIs.hasOwnProperty(namespaceSplit[0])) + elm.setAttributeNS(NamespaceURIs[namespaceSplit[0]], key, cur); + else + elm.setAttribute(key, cur); + } } } //remove removed attributes