diff options
Diffstat (limited to 'src/elemnts-svg.js')
-rw-r--r-- | src/elemnts-svg.js | 88 |
1 files changed, 61 insertions, 27 deletions
diff --git a/src/elemnts-svg.js b/src/elemnts-svg.js index 082ccd5..b5b2542 100644 --- a/src/elemnts-svg.js +++ b/src/elemnts-svg.js @@ -1,34 +1,68 @@ - // Import raw svg - svg: function (svg) { - var well, len - - // act as getter if no svg string is given - if(svg == null || svg === true) { - // write svgjs data to the dom - this.writeDataToDom() - - // return outer or inner content - return svg - ? this.node.innerHTML - : this.node.outerHTML - } +import { ns } from './namespaces.js' + +/* eslint no-unused-vars: "off" */ +var a = { + // Import raw svg + svg (svg, fn = false) { + var well, len, fragment + + // act as getter if no svg string is given + if (svg == null || svg === true || typeof svg === 'function') { + // write svgjs data to the dom + this.writeDataToDom() + let current = this - // act as setter if we got a string + // An export modifier was passed + if (typeof svg === 'function') { + // Juggle arguments + [fn, svg] = [svg, fn] - // 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') + // If the user wants outerHTML we need to process this node, too + if (!svg) { + current = fn(current) - // create temporary holder - well = document.createElementNS(SVG.ns, 'svg') + // The user does not want this node? Well, then he gets nothing + if (current === false) return '' + } - // dump raw svg - well.innerHTML = svg + // Deep loop through all children and apply modifier + current.each(function () { + let result = fn(this) - // transplant nodes - for (len = well.children.length; len--;) { - this.node.appendChild(well.firstElementChild) + // 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 this - },
\ No newline at end of file + // Return outer or inner content + return svg + ? current.node.innerHTML + : current.node.outerHTML + } + + // Act as setter if we got a string + + // Create temporary holder + well = document.createElementNS(ns, 'svg') + fragment = document.createDocumentFragment() + + // Dump raw svg + well.innerHTML = svg + + // Transplant nodes into the fragment + for (len = well.children.length; len--;) { + fragment.appendChild(well.firstElementChild) + } + + // Add the whole fragment at once + this.node.appendChild(fragment) + + return this + } +} |