-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
43 lines (34 loc) · 999 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
const { setPrototypeOf:setProto, assign } = Object
const log = console.log.bind (console)
// Pseudo DOM
// ----------
const createElementNS = (ns, tagName) =>
setProto ({ tagName, attributes:{}, children:[] }, Elem.prototype)
class Elem {
append (...children) { this.children.splice (Infinity, 0, ...children) }
setAttribute (k, v) { this.attributes [k] = v }
toSVGString () { return toSVGString (this) }
}
function toSVGString (elem) {
return [...renderIt (elem)] .join ('')
}
function* renderIt ({ tagName, attributes:atts, children }) {
yield `<${tagName}`
for (const k in atts) yield ` ${k}="${atts[k]}"` // TODO: escapes
if (children.length) {
yield '>'
for (const child of children) yield* renderIt (child)
yield `</${tagName}>`
}
else yield '/>'
yield ('\n')
}
// Exports
// -------
const svg = require ('./src/svg')
assign (svg, svg._renderers (createElementNS))
module.exports = {
version: '1.0.0-beta',
hvif: require ('./src/hvif'),
svg
}