From 6a57b8cd9da1d0abc77b6ba4a2ce3d15fe675048 Mon Sep 17 00:00:00 2001 From: Ulrich-Matthias Schäfer Date: Thu, 9 Apr 2020 11:01:56 +1000 Subject: fixes and tests - fixed flatten and ungroup - added position argument to ungroup, toParent and toRoot - added tests for Container --- src/elements/Container.js | 24 +++++++++++------------- src/elements/Svg.js | 2 -- 2 files changed, 11 insertions(+), 15 deletions(-) (limited to 'src/elements') diff --git a/src/elements/Container.js b/src/elements/Container.js index ebaba50..9278435 100644 --- a/src/elements/Container.js +++ b/src/elements/Container.js @@ -2,28 +2,26 @@ import { register } from '../utils/adopter.js' import Element from './Element.js' export default class Container extends Element { - flatten (parent) { + flatten (parent = this, index) { this.each(function () { - if (this instanceof Container) return this.flatten(parent).ungroup(parent) - return this.toParent(parent) + if (this instanceof Container) { + return this.flatten().ungroup() + } }) - // we need this so that the root does not get removed - this.node.firstElementChild || this.remove() - return this } - ungroup (parent) { - parent = parent || this.parent() + ungroup (parent = this.parent(), index = parent.index(this)) { + // when parent != this, we want append all elements to the end + index = index === -1 ? parent.children().length : index - this.each(function () { - return this.toParent(parent) + this.each(function (i, children) { + // reverse each + return children[children.length - i - 1].toParent(parent, index) }) - this.remove() - - return this + return this.remove() } } diff --git a/src/elements/Svg.js b/src/elements/Svg.js index 8699327..51f4202 100644 --- a/src/elements/Svg.js +++ b/src/elements/Svg.js @@ -25,10 +25,8 @@ export default class Svg extends Container { } isRoot () { - return !this.node.parentNode || (!(this.node.parentNode instanceof globals.window.SVGElement) && this.node.parentNode.nodeName !== '#document-fragment') - // || this.node.parentNode.nodeName === '#document' } // Add namespaces -- cgit v1.2.3