diff options
author | Ulrich-Matthias Schäfer <ulima.ums@googlemail.com> | 2018-02-11 15:29:23 +0100 |
---|---|---|
committer | Ulrich-Matthias Schäfer <ulima.ums@googlemail.com> | 2018-03-01 02:06:05 +0100 |
commit | 64a5c17b95393c0914670b1b9e2c8b4d63707968 (patch) | |
tree | d474bc95c101a44c91ee0ae8f7ccd73bb3acb48d | |
parent | 3d1b02f878756dece8a20c0110497ccc70b4f363 (diff) | |
download | svg.js-64a5c17b95393c0914670b1b9e2c8b4d63707968.tar.gz svg.js-64a5c17b95393c0914670b1b9e2c8b4d63707968.zip |
merge SVG.Doc and SVG.Nested. Add isRoot() method, update doc methods to decide between doc and nested
-rw-r--r-- | gulpfile.js | 1 | ||||
-rw-r--r-- | src/doc.js | 35 | ||||
-rw-r--r-- | src/element.js | 2 | ||||
-rw-r--r-- | src/nested.js | 16 | ||||
-rw-r--r-- | src/svg.js | 6 |
5 files changed, 28 insertions, 32 deletions
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',
@@ -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()) - } - } -}) @@ -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) |