Commit 69097d56 by Adrian Phinney

Fix SVG insertBefore/insertAfter when using <a> wrapper node.

When an element has an "href" the node is wrapped with an <a>.
insertBefore/insertAfter did not take this into account (#631).
Additionally, remove, toFront and toBack now use the new function.
parent 1fb02dae
...@@ -604,6 +604,13 @@ window.Raphael && window.Raphael.svg && function(R) { ...@@ -604,6 +604,13 @@ window.Raphael && window.Raphael.svg && function(R) {
dif = a.y - (bb.y + bb.height / 2); dif = a.y - (bb.y + bb.height / 2);
dif && R.is(dif, "finite") && $(tspans[0], {dy: dif}); dif && R.is(dif, "finite") && $(tspans[0], {dy: dif});
}, },
getRealNode = function (node) {
if (node.parentNode && node.parentNode.tagName.toLowerCase() === "a") {
return node.parentNode;
} else {
return node;
}
}
Element = function (node, svg) { Element = function (node, svg) {
var X = 0, var X = 0,
Y = 0; Y = 0;
...@@ -881,7 +888,8 @@ window.Raphael && window.Raphael.svg && function(R) { ...@@ -881,7 +888,8 @@ window.Raphael && window.Raphael.svg && function(R) {
* Removes element from the paper. * Removes element from the paper.
\*/ \*/
elproto.remove = function () { elproto.remove = function () {
if (this.removed || !this.node.parentNode) { var node = getRealNode(this.node);
if (this.removed || !node.parentNode) {
return; return;
} }
var paper = this.paper; var paper = this.paper;
...@@ -891,11 +899,7 @@ window.Raphael && window.Raphael.svg && function(R) { ...@@ -891,11 +899,7 @@ window.Raphael && window.Raphael.svg && function(R) {
paper.defs.removeChild(this.gradient); paper.defs.removeChild(this.gradient);
} }
R._tear(this, paper); R._tear(this, paper);
if (this.node.parentNode.tagName.toLowerCase() == "a") { node.parentNode.removeChild(node);
this.node.parentNode.parentNode.removeChild(this.node.parentNode);
} else {
this.node.parentNode.removeChild(this.node);
}
for (var i in this) { for (var i in this) {
this[i] = typeof this[i] == "function" ? R._removedFactory(i) : null; this[i] = typeof this[i] == "function" ? R._removedFactory(i) : null;
} }
...@@ -1069,11 +1073,8 @@ window.Raphael && window.Raphael.svg && function(R) { ...@@ -1069,11 +1073,8 @@ window.Raphael && window.Raphael.svg && function(R) {
if (this.removed) { if (this.removed) {
return this; return this;
} }
if (this.node.parentNode.tagName.toLowerCase() == "a") { var node = getRealNode(this.node);
this.node.parentNode.parentNode.appendChild(this.node.parentNode); node.parentNode.appendChild(node);
} else {
this.node.parentNode.appendChild(this.node);
}
var svg = this.paper; var svg = this.paper;
svg.top != this && R._tofront(this, svg); svg.top != this && R._tofront(this, svg);
return this; return this;
...@@ -1089,12 +1090,9 @@ window.Raphael && window.Raphael.svg && function(R) { ...@@ -1089,12 +1090,9 @@ window.Raphael && window.Raphael.svg && function(R) {
if (this.removed) { if (this.removed) {
return this; return this;
} }
var parent = this.node.parentNode; var node = getRealNode(this.node);
if (parent.tagName.toLowerCase() == "a") { var parentNode = node.parentNode;
parent.parentNode.insertBefore(this.node.parentNode, this.node.parentNode.parentNode.firstChild); parentNode.insertBefore(node, parentNode.firstChild);
} else if (parent.firstChild != this.node) {
parent.insertBefore(this.node, this.node.parentNode.firstChild);
}
R._toback(this, this.paper); R._toback(this, this.paper);
var svg = this.paper; var svg = this.paper;
return this; return this;
...@@ -1107,14 +1105,16 @@ window.Raphael && window.Raphael.svg && function(R) { ...@@ -1107,14 +1105,16 @@ window.Raphael && window.Raphael.svg && function(R) {
= (object) @Element = (object) @Element
\*/ \*/
elproto.insertAfter = function (element) { elproto.insertAfter = function (element) {
if (this.removed) { if (this.removed || !element) {
return this; return this;
} }
var node = element.node || element[element.length - 1].node;
if (node.nextSibling) { var node = getRealNode(this.node);
node.parentNode.insertBefore(this.node, node.nextSibling); var afterNode = getRealNode(element.node || element[element.length - 1].node);
if (afterNode.nextSibling) {
afterNode.parentNode.insertBefore(node, afterNode.nextSibling);
} else { } else {
node.parentNode.appendChild(this.node); afterNode.parentNode.appendChild(node);
} }
R._insertafter(this, element, this.paper); R._insertafter(this, element, this.paper);
return this; return this;
...@@ -1127,11 +1127,13 @@ window.Raphael && window.Raphael.svg && function(R) { ...@@ -1127,11 +1127,13 @@ window.Raphael && window.Raphael.svg && function(R) {
= (object) @Element = (object) @Element
\*/ \*/
elproto.insertBefore = function (element) { elproto.insertBefore = function (element) {
if (this.removed) { if (this.removed || !element) {
return this; return this;
} }
var node = element.node || element[0].node;
node.parentNode.insertBefore(this.node, node); var node = getRealNode(this.node);
var beforeNode = getRealNode(element.node || element[0].node);
beforeNode.parentNode.insertBefore(node, beforeNode);
R._insertbefore(this, element, this.paper); R._insertbefore(this, element, this.paper);
return this; return this;
}; };
......
This source diff could not be displayed because it is too large. You can view the blob instead.
...@@ -6389,6 +6389,13 @@ ...@@ -6389,6 +6389,13 @@
dif = a.y - (bb.y + bb.height / 2); dif = a.y - (bb.y + bb.height / 2);
dif && R.is(dif, "finite") && $(tspans[0], {dy: dif}); dif && R.is(dif, "finite") && $(tspans[0], {dy: dif});
}, },
getRealNode = function (node) {
if (node.parentNode && node.parentNode.tagName.toLowerCase() === "a") {
return node.parentNode;
} else {
return node;
}
}
Element = function (node, svg) { Element = function (node, svg) {
var X = 0, var X = 0,
Y = 0; Y = 0;
...@@ -6666,7 +6673,8 @@ ...@@ -6666,7 +6673,8 @@
* Removes element from the paper. * Removes element from the paper.
\*/ \*/
elproto.remove = function () { elproto.remove = function () {
if (this.removed || !this.node.parentNode) { var node = getRealNode(this.node);
if (this.removed || !node.parentNode) {
return; return;
} }
var paper = this.paper; var paper = this.paper;
...@@ -6676,11 +6684,7 @@ ...@@ -6676,11 +6684,7 @@
paper.defs.removeChild(this.gradient); paper.defs.removeChild(this.gradient);
} }
R._tear(this, paper); R._tear(this, paper);
if (this.node.parentNode.tagName.toLowerCase() == "a") { node.parentNode.removeChild(node);
this.node.parentNode.parentNode.removeChild(this.node.parentNode);
} else {
this.node.parentNode.removeChild(this.node);
}
for (var i in this) { for (var i in this) {
this[i] = typeof this[i] == "function" ? R._removedFactory(i) : null; this[i] = typeof this[i] == "function" ? R._removedFactory(i) : null;
} }
...@@ -6854,11 +6858,8 @@ ...@@ -6854,11 +6858,8 @@
if (this.removed) { if (this.removed) {
return this; return this;
} }
if (this.node.parentNode.tagName.toLowerCase() == "a") { var node = getRealNode(this.node);
this.node.parentNode.parentNode.appendChild(this.node.parentNode); node.parentNode.appendChild(node);
} else {
this.node.parentNode.appendChild(this.node);
}
var svg = this.paper; var svg = this.paper;
svg.top != this && R._tofront(this, svg); svg.top != this && R._tofront(this, svg);
return this; return this;
...@@ -6874,12 +6875,9 @@ ...@@ -6874,12 +6875,9 @@
if (this.removed) { if (this.removed) {
return this; return this;
} }
var parent = this.node.parentNode; var node = getRealNode(this.node);
if (parent.tagName.toLowerCase() == "a") { var parentNode = node.parentNode;
parent.parentNode.insertBefore(this.node.parentNode, this.node.parentNode.parentNode.firstChild); parentNode.insertBefore(node, parentNode.firstChild);
} else if (parent.firstChild != this.node) {
parent.insertBefore(this.node, this.node.parentNode.firstChild);
}
R._toback(this, this.paper); R._toback(this, this.paper);
var svg = this.paper; var svg = this.paper;
return this; return this;
...@@ -6892,14 +6890,16 @@ ...@@ -6892,14 +6890,16 @@
= (object) @Element = (object) @Element
\*/ \*/
elproto.insertAfter = function (element) { elproto.insertAfter = function (element) {
if (this.removed) { if (this.removed || !element) {
return this; return this;
} }
var node = element.node || element[element.length - 1].node;
if (node.nextSibling) { var node = getRealNode(this.node);
node.parentNode.insertBefore(this.node, node.nextSibling); var afterNode = getRealNode(element.node || element[element.length - 1].node);
if (afterNode.nextSibling) {
afterNode.parentNode.insertBefore(node, afterNode.nextSibling);
} else { } else {
node.parentNode.appendChild(this.node); afterNode.parentNode.appendChild(node);
} }
R._insertafter(this, element, this.paper); R._insertafter(this, element, this.paper);
return this; return this;
...@@ -6912,11 +6912,13 @@ ...@@ -6912,11 +6912,13 @@
= (object) @Element = (object) @Element
\*/ \*/
elproto.insertBefore = function (element) { elproto.insertBefore = function (element) {
if (this.removed) { if (this.removed || !element) {
return this; return this;
} }
var node = element.node || element[0].node;
node.parentNode.insertBefore(this.node, node); var node = getRealNode(this.node);
var beforeNode = getRealNode(element.node || element[0].node);
beforeNode.parentNode.insertBefore(node, beforeNode);
R._insertbefore(this, element, this.paper); R._insertbefore(this, element, this.paper);
return this; return this;
}; };
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment