aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorwout <wout@impinc.co.uk>2014-06-26 08:16:14 +0200
committerwout <wout@impinc.co.uk>2014-06-26 08:16:14 +0200
commit58b1a0b92ba78e0ce2047e41caa02f65d16cd557 (patch)
treeaf6b54d85b9affcf8e1b78612d17d620bc7db17f /src
parent2bc1909290d90ba9c38474361d62d7f6bcc3e95b (diff)
downloadsvg.js-58b1a0b92ba78e0ce2047e41caa02f65d16cd557.tar.gz
svg.js-58b1a0b92ba78e0ce2047e41caa02f65d16cd557.zip
Complete new clone() method
Diffstat (limited to 'src')
-rwxr-xr-xsrc/element.js31
-rw-r--r--src/helpers.js17
2 files changed, 16 insertions, 32 deletions
diff --git a/src/element.js b/src/element.js
index 753ac33..069c487 100755
--- a/src/element.js
+++ b/src/element.js
@@ -67,36 +67,7 @@ SVG.Element = SVG.invent({
}
// Clone element
, clone: function() {
- var clone , attr
- , type = this.type
-
- /* invoke shape method with shape-specific arguments */
- clone = type == 'rect' || type == 'ellipse' ?
- this.parent()[type](0,0) :
- type == 'line' ?
- this.parent()[type](0,0,0,0) :
- type == 'image' ?
- this.parent()[type](this.src) :
- type == 'text' ?
- this.parent()[type](this.content) :
- type == 'path' ?
- this.parent()[type](this.attr('d')) :
- type == 'polyline' || type == 'polygon' ?
- this.parent()[type](this.attr('points')) :
- type == 'g' ?
- this.parent().group() :
- this.parent()[type]()
-
- /* apply attributes attributes */
- attr = this.attr()
- delete attr.id
- clone.attr(attr)
-
- /* copy transformations */
- clone.trans = this.trans
-
- /* apply attributes and translations */
- return clone.transform({})
+ return assignNewId(this.node.cloneNode(true))
}
// Remove element
, remove: function() {
diff --git a/src/helpers.js b/src/helpers.js
index 492039c..ad73fde 100644
--- a/src/helpers.js
+++ b/src/helpers.js
@@ -91,6 +91,19 @@ function arrayToString(a) {
return s + ' '
}
+// Deep new id assignment
+function assignNewId(node) {
+ // Adopt element and assign new id
+ var element = SVG.adopt(node).id(SVG.eid(node.nodeName))
+
+ // Do the same for SVG child nodes as well
+ for (var i = node.childNodes.length - 1; i >= 0; i--)
+ if (node.childNodes[i] instanceof SVGElement)
+ assignNewId(node.childNodes[i])
+
+ return element
+}
+
// Add more bounding box properties
function boxProperties(b) {
b.x2 = b.x + b.width
@@ -102,10 +115,10 @@ function boxProperties(b) {
// Parse a matrix string
function parseMatrix(o) {
if (o.matrix) {
- /* split matrix string */
+ // Split matrix string
var m = o.matrix.replace(/\s/g, '').split(',')
- /* pasrse values */
+ // Pasrse values
if (m.length == 6) {
o.a = parseFloat(m[0])
o.b = parseFloat(m[1])