From 64a5c17b95393c0914670b1b9e2c8b4d63707968 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Ulrich-Matthias=20Sch=C3=A4fer?= Date: Sun, 11 Feb 2018 15:29:23 +0100 Subject: [PATCH] merge SVG.Doc and SVG.Nested. Add isRoot() method, update doc methods to decide between doc and nested --- gulpfile.js | 1 - src/doc.js | 35 ++++++++++++++++++++++++----------- src/element.js | 2 +- src/nested.js | 16 ---------------- src/svg.js | 6 +++--- 5 files changed, 28 insertions(+), 32 deletions(-) delete mode 100644 src/nested.js diff --git a/gulpfile.js b/gulpfile.js index 545a27f..d96451b 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -69,7 +69,6 @@ var parts = [ 'src/image.js', 'src/text.js', 'src/textpath.js', - 'src/nested.js', 'src/hyperlink.js', 'src/marker.js', 'src/sugar.js', diff --git a/src/doc.js b/src/doc.js index 2097747..843ff03 100644 --- a/src/doc.js +++ b/src/doc.js @@ -12,22 +12,37 @@ SVG.Doc = SVG.invent({ // Add class methods extend: { + isRoot: function() { + return !this.node.parentNode || !this.node.parentNode instanceof window.SVGElement || this.node.parentNode.nodeName == '#document' + }, + doc: function() { + if(this.isRoot()) return this + + var parent + while(parent = this.parent(SVG.Doc)) { + if(parent.isRoot()) return parent + } + + throw new Error('This should never be reached') + }, // Add namespaces - namespace: function () { + namespace: function() { + if(!this.isRoot()) return this.doc().namespace() return this .attr({ xmlns: SVG.ns, version: '1.1' }) .attr('xmlns:xlink', SVG.xlink, SVG.xmlns) .attr('xmlns:svgjs', SVG.svgjs, SVG.xmlns) }, // Creates and returns defs element - defs: function () { + defs: function() { + if(!this.isRoot()) return this.doc().defs() return SVG.adopt(this.node.getElementsByTagName('defs')[0]) || this.put(new SVG.Defs()) }, // custom parent method parent: function () { return this.node.parentNode.nodeName === '#document' ? null : this.node.parentNode }, - // Removes the doc from the DOM + // Removes the doc from the DOM remove: function () { if (this.parent()) { this.parent().removeChild(this.node) @@ -41,14 +56,12 @@ SVG.Doc = SVG.invent({ this.node.removeChild(this.node.lastChild) } return this - }, - toNested: function () { - var el = SVG.create('svg') - this.node.instance = null - el.appendChild(this.node) - - return SVG.adopt(this.node) + } + }, + construct: { + // Create nested svg document + nested: function() { + return this.put(new SVG.Doc) } } - }) diff --git a/src/element.js b/src/element.js index f35dc19..e7b5326 100644 --- a/src/element.js +++ b/src/element.js @@ -218,7 +218,7 @@ SVG.Element = SVG.invent({ // Get parent document doc: function () { - return this instanceof SVG.Doc ? this : this.parent(SVG.Doc) + return this.parent(SVG.Doc).doc() }, // Get defs diff --git a/src/nested.js b/src/nested.js deleted file mode 100644 index 217d59a..0000000 --- a/src/nested.js +++ /dev/null @@ -1,16 +0,0 @@ - -SVG.Nested = SVG.invent({ - // Initialize node - create: 'svg', - - // Inherit from - inherit: SVG.Container, - - // Add parent method - construct: { - // Create nested svg document - nested: function () { - return this.put(new SVG.Nested()) - } - } -}) diff --git a/src/svg.js b/src/svg.js index 55cb88d..4cb6f26 100644 --- a/src/svg.js +++ b/src/svg.js @@ -87,9 +87,9 @@ SVG.adopt = function (node) { var element // adopt with element-specific settings - if (node.nodeName === 'svg') { - element = node.parentNode instanceof window.SVGElement ? new SVG.Nested(node) : new SVG.Doc(node) - } else if (node.nodeName === 'linearGradient' || node.nodeName === 'radialGradient') { + if (node.nodeName == 'svg') + element = new SVG.Doc(node) + else if (node.nodeName === 'linearGradient' || node.nodeName === 'radialGradient') element = new SVG.Gradient(node) } else if (SVG[capitalize(node.nodeName)]) { element = new SVG[capitalize(node.nodeName)](node) -- 2.39.5