-
Notifications
You must be signed in to change notification settings - Fork 83
/
element.js
23 lines (22 loc) · 1.04 KB
/
element.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
var namespaces = {
math: "http://www.w3.org/1998/Math/MathML",
svg: "http://www.w3.org/2000/svg",
xhtml: "http://www.w3.org/1999/xhtml",
xlink: "http://www.w3.org/1999/xlink",
xml: "http://www.w3.org/XML/1998/namespace",
xmlns: "http://www.w3.org/2000/xmlns/"
};
export function element(name, attributes) {
var prefix = name += "", i = prefix.indexOf(":"), value;
if (i >= 0 && (prefix = name.slice(0, i)) !== "xmlns") name = name.slice(i + 1);
var element = namespaces.hasOwnProperty(prefix) // eslint-disable-line no-prototype-builtins
? document.createElementNS(namespaces[prefix], name)
: document.createElement(name);
if (attributes) for (var key in attributes) {
prefix = key, i = prefix.indexOf(":"), value = attributes[key];
if (i >= 0 && (prefix = key.slice(0, i)) !== "xmlns") key = key.slice(i + 1);
if (namespaces.hasOwnProperty(prefix)) element.setAttributeNS(namespaces[prefix], key, value); // eslint-disable-line no-prototype-builtins
else element.setAttribute(key, value);
}
return element;
}