diff options
author | Ulrich-Matthias Schäfer <ulima.ums@googlemail.com> | 2017-04-22 22:16:36 +0200 |
---|---|---|
committer | Ulrich-Matthias Schäfer <ulima.ums@googlemail.com> | 2017-04-22 22:16:36 +0200 |
commit | ab1c07e80bd21f2cdf773be238d4dd754db4e80a (patch) | |
tree | b1386635a4e54f29eee8cfa41151fd930610c25e /src/css.js | |
parent | 03d25548fe99c87ed74e789b894e37a7fc6caf34 (diff) | |
download | svg.js-ab1c07e80bd21f2cdf773be238d4dd754db4e80a.tar.gz svg.js-ab1c07e80bd21f2cdf773be238d4dd754db4e80a.zip |
changed `style()` to `css()`
it now accepts array as input and returns object when no argument given (#517)
also removed sub-pixel offset fix
Diffstat (limited to 'src/css.js')
-rw-r--r-- | src/css.js | 42 |
1 files changed, 31 insertions, 11 deletions
@@ -1,22 +1,42 @@ SVG.extend(SVG.Element, { // Dynamic style generator - style: function(s, v) { + css: function(s, v) { + var t, i, ret = {} if (arguments.length == 0) { - // get full style - return this.node.style.cssText || '' + // get full style as object + this.node.style.cssText.split(/\s*;\s*/).filter(function(el) { return !!el.length }).forEach(function(el) { + t = el.split(/\s*:\s*/) + ret[t[0]] = t[1] + }) + return ret + } - } else if (arguments.length < 2) { - // apply every style individually if an object is passed - if (typeof s == 'object') { - for (v in s) this.style(v, s[v]) + if (arguments.length < 2) { + // get style properties in the array + if(Array.isArray(s)) { + for(i = s.length; i--;) { + ret[camelCase(s[i])] = this.node.style[camelCase(s[i])] + } + return ret + } - } else { - // act as a getter if the first and only argument is not an object + // get style for property + if(typeof s == 'string') { return this.node.style[camelCase(s)] } - } else { - this.node.style[camelCase(s)] = v === null || SVG.regex.isBlank.test(v) ? '' : v + // set styles in object + if(typeof s == 'object') { + for(i in s) { + // set empty string if null/undefined/'' was given + this.node.style[camelCase(i)] = (s[i] == null || SVG.regex.isBlank.test(s[i])) ? '' : s[i] + } + } + } + + // set style for property + if (arguments.length == 2) { + this.node.style[camelCase(s)] = (v == null || SVG.regex.isBlank.test(v)) ? '' : v } return this |