dagre-d3
Version:
A D3-based renderer for Dagre
45 lines (38 loc) • 939 B
JavaScript
var util = require("../util");
module.exports = addTextLabel;
/*
* Attaches a text label to the specified root. Handles escape sequences.
*/
function addTextLabel(root, node) {
var domNode = root.append("text");
var lines = processEscapeSequences(node.label).split("\n");
for (var i = 0; i < lines.length; i++) {
domNode.append("tspan")
.attr("xml:space", "preserve")
.attr("dy", "1em")
.attr("x", "1")
.text(lines[i]);
}
util.applyStyle(domNode, node.labelStyle);
return domNode;
}
function processEscapeSequences(text) {
var newText = "";
var escaped = false;
var ch;
for (var i = 0; i < text.length; ++i) {
ch = text[i];
if (escaped) {
switch(ch) {
case "n": newText += "\n"; break;
default: newText += ch;
}
escaped = false;
} else if (ch === "\\") {
escaped = true;
} else {
newText += ch;
}
}
return newText;
}