UNPKG

d3

Version:

A small, free JavaScript library for manipulating documents based on data.

45 lines (36 loc) 1.2 kB
d3_selectionPrototype.attr = function(name, value) { name = d3.ns.qualify(name); // If no value is specified, return the first value. if (arguments.length < 2) { var node = this.node(); return name.local ? node.getAttributeNS(name.space, name.local) : node.getAttribute(name); } function attrNull() { this.removeAttribute(name); } function attrNullNS() { this.removeAttributeNS(name.space, name.local); } function attrConstant() { this.setAttribute(name, value); } function attrConstantNS() { this.setAttributeNS(name.space, name.local, value); } function attrFunction() { var x = value.apply(this, arguments); if (x == null) this.removeAttribute(name); else this.setAttribute(name, x); } function attrFunctionNS() { var x = value.apply(this, arguments); if (x == null) this.removeAttributeNS(name.space, name.local); else this.setAttributeNS(name.space, name.local, x); } return this.each(value == null ? (name.local ? attrNullNS : attrNull) : (typeof value === "function" ? (name.local ? attrFunctionNS : attrFunction) : (name.local ? attrConstantNS : attrConstant))); };