Fixed text setting and getting. Removed superflous <tspan>

master
Dmitry Baranovskiy 2013-09-09 17:22:14 +10:00
parent 5bca0ff9db
commit 7a2ebfa2ac
4 changed files with 92 additions and 36 deletions

View File

@ -102,7 +102,7 @@
18;
var t = s.text(200, 120, ["S","A","V","A","G","E"]);
t.selectAll("tspan tspan:nth-child(odd)").attr({
t.selectAll("tspan:nth-child(odd)").attr({
fill: "#900",
"font-size": "20px"
});

6
dist/savage-min.js vendored

File diff suppressed because one or more lines are too long

40
dist/savage.js vendored
View File

@ -3020,7 +3020,7 @@ function arrayFirstValue(arr) {
= (Fragment) the fragment
\*/
Savage.parse = function (svg) {
var f = document.createDocumentFragment(),
var f = glob.doc.createDocumentFragment(),
pointer = f;
eve.on("elemental.tag", function (data, extra, raw) {
var tag = $(data);
@ -3029,7 +3029,7 @@ Savage.parse = function (svg) {
pointer = tag;
});
eve.on("elemental.text", function (text) {
pointer.appendChild(document.createTextNode(text));
pointer.appendChild(glob.doc.createTextNode(text));
});
eve.on("elemental./tag", function () {
pointer = pointer.parentNode;
@ -3069,7 +3069,7 @@ Fragment.prototype.selectAll = Element.prototype.selectAll;
\*/
Savage.fragment = function () {
var args = Array.prototype.slice.call(arguments, 0),
f = document.createDocumentFragment();
f = glob.doc.createDocumentFragment();
for (var i = 0, ii = args.length; i < ii; i++) {
var item = args[i];
if (item.node && item.node.nodeType) {
@ -3893,8 +3893,8 @@ eve.on("savage.util.attr.r", function (value) {
eve.on("savage.util.attr.text", function (value) {
if (this.type == "text") {
var i = 0,
node = this.node;
var tuner = function (chunk) {
node = this.node,
tuner = function (chunk) {
var out = $("tspan");
if (is(chunk, "array")) {
for (var i = 0; i < chunk.length; i++) {
@ -3909,7 +3909,10 @@ eve.on("savage.util.attr.text", function (value) {
while (node.firstChild) {
node.removeChild(node.firstChild);
}
node.appendChild(tuner(value));
var tuned = tuner(value);
while (tuned.firstChild) {
node.appendChild(tuned.firstChild);
}
}
eve.stop();
})(-1);
@ -4366,6 +4369,31 @@ eve.on("savage.util.getattr.r", function () {
return $(this.node, "rx");
}
})(-1);
function textExtract(node) {
var out = [];
var children = node.childNodes;
for (var i = 0, ii = children.length; i < ii; i++) {
var chi = children[i];
if (chi.nodeType == 3) {
out.push(chi.nodeValue);
}
if (chi.tagName == "tspan") {
if (chi.childNodes.length == 1 && chi.firstChild.nodeType == 3) {
out.push(chi.firstChild.nodeValue);
} else {
out.push(textExtract(chi));
}
}
}
return out;
}
eve.on("savage.util.getattr.text", function () {
if (this.type == "text" || this.type == "tspan") {
eve.stop();
var out = textExtract(this.node);
return out.length == 1 ? out[0] : out;
}
})(-1);
eve.on("savage.util.getattr.#text", function () {
return this.node.textContent;
})(-1);

View File

@ -2003,7 +2003,7 @@ function arrayFirstValue(arr) {
= (Fragment) the fragment
\*/
Savage.parse = function (svg) {
var f = document.createDocumentFragment(),
var f = glob.doc.createDocumentFragment(),
pointer = f;
eve.on("elemental.tag", function (data, extra, raw) {
var tag = $(data);
@ -2012,7 +2012,7 @@ Savage.parse = function (svg) {
pointer = tag;
});
eve.on("elemental.text", function (text) {
pointer.appendChild(document.createTextNode(text));
pointer.appendChild(glob.doc.createTextNode(text));
});
eve.on("elemental./tag", function () {
pointer = pointer.parentNode;
@ -2052,7 +2052,7 @@ Fragment.prototype.selectAll = Element.prototype.selectAll;
\*/
Savage.fragment = function () {
var args = Array.prototype.slice.call(arguments, 0),
f = document.createDocumentFragment();
f = glob.doc.createDocumentFragment();
for (var i = 0, ii = args.length; i < ii; i++) {
var item = args[i];
if (item.node && item.node.nodeType) {
@ -2876,8 +2876,8 @@ eve.on("savage.util.attr.r", function (value) {
eve.on("savage.util.attr.text", function (value) {
if (this.type == "text") {
var i = 0,
node = this.node;
var tuner = function (chunk) {
node = this.node,
tuner = function (chunk) {
var out = $("tspan");
if (is(chunk, "array")) {
for (var i = 0; i < chunk.length; i++) {
@ -2892,7 +2892,10 @@ eve.on("savage.util.attr.text", function (value) {
while (node.firstChild) {
node.removeChild(node.firstChild);
}
node.appendChild(tuner(value));
var tuned = tuner(value);
while (tuned.firstChild) {
node.appendChild(tuned.firstChild);
}
}
eve.stop();
})(-1);
@ -3349,6 +3352,31 @@ eve.on("savage.util.getattr.r", function () {
return $(this.node, "rx");
}
})(-1);
function textExtract(node) {
var out = [];
var children = node.childNodes;
for (var i = 0, ii = children.length; i < ii; i++) {
var chi = children[i];
if (chi.nodeType == 3) {
out.push(chi.nodeValue);
}
if (chi.tagName == "tspan") {
if (chi.childNodes.length == 1 && chi.firstChild.nodeType == 3) {
out.push(chi.firstChild.nodeValue);
} else {
out.push(textExtract(chi));
}
}
}
return out;
}
eve.on("savage.util.getattr.text", function () {
if (this.type == "text" || this.type == "tspan") {
eve.stop();
var out = textExtract(this.node);
return out.length == 1 ? out[0] : out;
}
})(-1);
eve.on("savage.util.getattr.#text", function () {
return this.node.textContent;
})(-1);