diff options
author | Ulrich-Matthias Schäfer <ulima.ums@googlemail.com> | 2018-11-06 13:48:05 +0100 |
---|---|---|
committer | Ulrich-Matthias Schäfer <ulima.ums@googlemail.com> | 2018-11-06 13:48:05 +0100 |
commit | a0b13ebcacfd74b9f521110c7225bb404325bcd3 (patch) | |
tree | a07c5cc422645e31d7dfef81ce4e54f03f0945f6 /src/modules/core/attr.js | |
parent | 9f2696e8a2cf7e4eebc1cc7e31027fe2070094fa (diff) | |
download | svg.js-a0b13ebcacfd74b9f521110c7225bb404325bcd3.tar.gz svg.js-a0b13ebcacfd74b9f521110c7225bb404325bcd3.zip |
reordered modules, add es6 build
Diffstat (limited to 'src/modules/core/attr.js')
-rw-r--r-- | src/modules/core/attr.js | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/src/modules/core/attr.js b/src/modules/core/attr.js new file mode 100644 index 0000000..ed34dc9 --- /dev/null +++ b/src/modules/core/attr.js @@ -0,0 +1,80 @@ +import { isImage, isNumber } from './regex.js' +import { attrs as defaults } from './defaults.js' +import Color from '../../types/Color.js' +import SVGArray from '../../types/SVGArray.js' +import SVGNumber from '../../types/SVGNumber.js' + +// Set svg element attribute +export default function attr (attr, val, ns) { + // act as full getter + if (attr == null) { + // get an object of attributes + attr = {} + val = this.node.attributes + + for (let node of val) { + attr[node.nodeName] = isNumber.test(node.nodeValue) + ? parseFloat(node.nodeValue) + : node.nodeValue + } + + return attr + } else if (Array.isArray(attr)) { + // FIXME: implement + } else if (typeof attr === 'object') { + // apply every attribute individually if an object is passed + for (val in attr) this.attr(val, attr[val]) + } else if (val === null) { + // remove value + this.node.removeAttribute(attr) + } else if (val == null) { + // act as a getter if the first and only argument is not an object + val = this.node.getAttribute(attr) + return val == null ? defaults[attr] // FIXME: do we need to return defaults? + : isNumber.test(val) ? parseFloat(val) + : val + } else { + // convert image fill and stroke to patterns + if (attr === 'fill' || attr === 'stroke') { + if (isImage.test(val)) { + val = this.doc().defs().image(val) + } + } + + // FIXME: This is fine, but what about the lines above? + // How does attr know about image()? + while (typeof val.attrHook === 'function') { + val = val.attrHook(this, attr) + } + + // ensure correct numeric values (also accepts NaN and Infinity) + if (typeof val === 'number') { + val = new SVGNumber(val) + } else if (Color.isColor(val)) { + // ensure full hex color + val = new Color(val) + } else if (val.constructor === Array) { + // Check for plain arrays and parse array values + val = new SVGArray(val) + } + + // if the passed attribute is leading... + if (attr === 'leading') { + // ... call the leading method instead + if (this.leading) { + this.leading(val) + } + } else { + // set given attribute on node + typeof ns === 'string' ? this.node.setAttributeNS(ns, attr, val.toString()) + : this.node.setAttribute(attr, val.toString()) + } + + // rebuild if required + if (this.rebuild && (attr === 'font-size' || attr === 'x')) { + this.rebuild() + } + } + + return this +} |