aboutsummaryrefslogtreecommitdiffstats
path: root/src/elemnts-svg.js
diff options
context:
space:
mode:
authorUlrich-Matthias Schäfer <ulima.ums@googlemail.com>2018-11-01 16:59:51 +0100
committerUlrich-Matthias Schäfer <ulima.ums@googlemail.com>2018-11-01 16:59:51 +0100
commitc40d7ffdfb95cb4db067463bb9259644aacbb876 (patch)
treee6f6c960c747dc90c2dea4b161f8a085894af67f /src/elemnts-svg.js
parentac84c9be8051567cfcb28ccd7ea2652524bb8a6f (diff)
downloadsvg.js-c40d7ffdfb95cb4db067463bb9259644aacbb876.tar.gz
svg.js-c40d7ffdfb95cb4db067463bb9259644aacbb876.zip
fix a few mistakes. Make sugar work. Roll back to childNodes because children is 10x slower
Diffstat (limited to 'src/elemnts-svg.js')
-rw-r--r--src/elemnts-svg.js61
1 files changed, 47 insertions, 14 deletions
diff --git a/src/elemnts-svg.js b/src/elemnts-svg.js
index 082ccd5..39fb22b 100644
--- a/src/elemnts-svg.js
+++ b/src/elemnts-svg.js
@@ -1,34 +1,67 @@
// Import raw svg
- svg: function (svg) {
+ svg: function (svg, fn = false) {
var well, len
// act as getter if no svg string is given
- if(svg == null || svg === true) {
+ if(svg == null || svg === true || typeof svg == 'function') {
// write svgjs data to the dom
this.writeDataToDom()
+ let current = this
- // return outer or inner content
+ // An export modifier was passed
+ if (typeof svg == 'function') {
+ // Juggle arguments
+ [fn, svg] = [svg, fn]
+
+ // If the user wants outerHTML we need to process this node, too
+ if (!svg) {
+ current = fn(current)
+
+ // The user does not want this node? Well, then he gets nothing
+ if (current === false) return ''
+ }
+
+ // Deep loop through all children and apply modifier
+ current.each(function () {
+ let result = fn(this)
+
+ // If modifier returns false, discard node
+ if (result === false) {
+ this.remove()
+
+ // If modifier returns new node, use it
+ } else if (result !== this) {
+ this.replace(result)
+ }
+ }, true)
+ }
+
+ // Return outer or inner content
return svg
- ? this.node.innerHTML
- : this.node.outerHTML
+ ? current.node.innerHTML
+ : current.node.outerHTML
}
- // act as setter if we got a string
+ // Act as setter if we got a string
- // make sure we are on a parent when trying to import
- if(!(this instanceof SVG.Parent))
- throw Error('Cannot import svg into non-parent element')
+ // Make sure we are on a current when trying to import
+ if(!(this instanceof SVG.current))
+ throw Error('Cannot import svg into non-current element')
- // create temporary holder
+ // Create temporary holder
well = document.createElementNS(SVG.ns, 'svg')
+ fragment = document.createDocumentFragment()
- // dump raw svg
+ // Dump raw svg
well.innerHTML = svg
- // transplant nodes
+ // Transplant nodes into the fragment
for (len = well.children.length; len--;) {
- this.node.appendChild(well.firstElementChild)
+ fragment.appendChild(well.firstElementChild)
}
+ // Add the whole fragment at once
+ this.node.appendChild(fragment)
+
return this
- }, \ No newline at end of file
+ },