dom-layer
Version:
Virtual DOM implementation.
57 lines (52 loc) • 1.32 kB
JavaScript
/**
* SVG namespaces.
*/
var namespaces = {
xlink: 'http://www.w3.org/1999/xlink',
xml: 'http://www.w3.org/XML/1998/namespace',
xmlns: 'http://www.w3.org/2000/xmlns/'
};
/**
* Maintains state of element namespaced attributes.
*
* @param Object element A DOM element.
* @param Object previous The previous state of attributes.
* @param Object attrs The attributes to match on.
* @return Object attrs The element attributes state.
*/
function patch(element, previous, attrs) {
if (!previous && !attrs) {
return attrs;
}
var attrName, ns, name, value, split;
previous = previous || {};
attrs = attrs || {};
for (attrName in previous) {
if (previous[attrName] && !attrs[attrName]) {
split = splitAttrName(attrName);
ns = namespaces[split[0]];
name = split[1];
element.removeAttributeNS(ns, name);
}
}
for (attrName in attrs) {
value = attrs[attrName];
if (previous[attrName] === value) {
continue;
}
if (value) {
split = splitAttrName(attrName);
ns = namespaces[split[0]];
name = split[1];
element.setAttributeNS(ns, name, value);
}
}
return attrs;
}
function splitAttrName(attrName) {
return attrName.split(':');
}
module.exports = {
patch: patch,
namespaces: namespaces
};