diff options
author | Ulrich-Matthias Schäfer <ulima.ums@googlemail.com> | 2018-11-08 11:31:28 +0100 |
---|---|---|
committer | Ulrich-Matthias Schäfer <ulima.ums@googlemail.com> | 2018-11-08 11:31:28 +0100 |
commit | 24111571324badc5282cf835c6565576d7fcb90f (patch) | |
tree | d5ea4653588a2ecd5f6a578fd6378fe2745e3478 /src/elements | |
parent | 834c05b0a251f38043a7961dab920f20720c1144 (diff) | |
download | svg.js-24111571324badc5282cf835c6565576d7fcb90f.tar.gz svg.js-24111571324badc5282cf835c6565576d7fcb90f.zip |
add styletags
Diffstat (limited to 'src/elements')
-rw-r--r-- | src/elements/Style.js | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/src/elements/Style.js b/src/elements/Style.js new file mode 100644 index 0000000..5c25893 --- /dev/null +++ b/src/elements/Style.js @@ -0,0 +1,51 @@ +import { nodeOrNew } from '../utils/adopter.js' +import { registerMethods } from '../utils/methods.js' +import { unCamelCase } from '../utils/utils.js' +import Element from './Element.js' + +function cssRule (selector, rule) { + if (!selector) return '' + if (!rule) return selector + + var ret = selector + '{' + + for (var i in rule) { + ret += unCamelCase(i) + ':' + rule[i] + ';' + } + + ret += '}' + + return ret +} + +export default class Style extends Element { + constructor (node) { + super(nodeOrNew('style', node), Style) + } + + words (w) { + this.node.textContent += (w || '') + return this + } + + font (name, src, params = {}) { + return this.rule('@font-face', { + fontFamily: name, + src: src, + ...params + }) + } + + rule (selector, obj) { + return this.words(cssRule(selector, obj)) + } +} + +registerMethods('Element', { + style (selector, obj) { + return this.put(new Style()).rule(selector, obj) + }, + fontface (name, src, params) { + return this.put(new Style()).font(name, src, params) + } +}) |