From: Ulrich-Matthias Schäfer Date: Thu, 1 Mar 2018 22:55:53 +0000 (+0100) Subject: make ungroup working on groups and nested for one depths X-Git-Tag: 3.0.0~64^2 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=refs%2Fpull%2F827%2Fhead;p=svg.js.git make ungroup working on groups and nested for one depths --- diff --git a/src/flatten.js b/src/flatten.js index 3ba6e22..19eebd7 100644 --- a/src/flatten.js +++ b/src/flatten.js @@ -1,6 +1,9 @@ SVG.extend(SVG.Parent, { flatten: function (parent) { - if (this instanceof SVG.Defs) return this + // flattens is only possible for nested svgs and groups + if (!(this instanceof SVG.G || this instanceof SVG.Doc)) { + return this + } parent = parent || (this instanceof SVG.Doc && this.isRoot() ? this : this.parent(SVG.Parent)) @@ -13,6 +16,23 @@ SVG.extend(SVG.Parent, { // we need this so that SVG.Doc does not get removed this.node.firstElementChild || this.remove() + return this + }, + ungroup: function (parent) { + // ungroup is only possible for nested svgs and groups + if (!(this instanceof SVG.G || (this instanceof SVG.Doc && !this.isRoot()))) { + return this + } + + parent = parent || this.parent(SVG.Parent) + + this.each(function () { + return this.toParent(parent) + }) + + // we need this so that SVG.Doc does not get removed + this.remove() + return this } })