| 1 | import namespace from "../namespace.js";
|
|---|
| 2 |
|
|---|
| 3 | function attrRemove(name) {
|
|---|
| 4 | return function() {
|
|---|
| 5 | this.removeAttribute(name);
|
|---|
| 6 | };
|
|---|
| 7 | }
|
|---|
| 8 |
|
|---|
| 9 | function attrRemoveNS(fullname) {
|
|---|
| 10 | return function() {
|
|---|
| 11 | this.removeAttributeNS(fullname.space, fullname.local);
|
|---|
| 12 | };
|
|---|
| 13 | }
|
|---|
| 14 |
|
|---|
| 15 | function attrConstant(name, value) {
|
|---|
| 16 | return function() {
|
|---|
| 17 | this.setAttribute(name, value);
|
|---|
| 18 | };
|
|---|
| 19 | }
|
|---|
| 20 |
|
|---|
| 21 | function attrConstantNS(fullname, value) {
|
|---|
| 22 | return function() {
|
|---|
| 23 | this.setAttributeNS(fullname.space, fullname.local, value);
|
|---|
| 24 | };
|
|---|
| 25 | }
|
|---|
| 26 |
|
|---|
| 27 | function attrFunction(name, value) {
|
|---|
| 28 | return function() {
|
|---|
| 29 | var v = value.apply(this, arguments);
|
|---|
| 30 | if (v == null) this.removeAttribute(name);
|
|---|
| 31 | else this.setAttribute(name, v);
|
|---|
| 32 | };
|
|---|
| 33 | }
|
|---|
| 34 |
|
|---|
| 35 | function attrFunctionNS(fullname, value) {
|
|---|
| 36 | return function() {
|
|---|
| 37 | var v = value.apply(this, arguments);
|
|---|
| 38 | if (v == null) this.removeAttributeNS(fullname.space, fullname.local);
|
|---|
| 39 | else this.setAttributeNS(fullname.space, fullname.local, v);
|
|---|
| 40 | };
|
|---|
| 41 | }
|
|---|
| 42 |
|
|---|
| 43 | export default function(name, value) {
|
|---|
| 44 | var fullname = namespace(name);
|
|---|
| 45 |
|
|---|
| 46 | if (arguments.length < 2) {
|
|---|
| 47 | var node = this.node();
|
|---|
| 48 | return fullname.local
|
|---|
| 49 | ? node.getAttributeNS(fullname.space, fullname.local)
|
|---|
| 50 | : node.getAttribute(fullname);
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | return this.each((value == null
|
|---|
| 54 | ? (fullname.local ? attrRemoveNS : attrRemove) : (typeof value === "function"
|
|---|
| 55 | ? (fullname.local ? attrFunctionNS : attrFunction)
|
|---|
| 56 | : (fullname.local ? attrConstantNS : attrConstant)))(fullname, value));
|
|---|
| 57 | }
|
|---|