Skip to content

No innerHTML and pseudo-html cleanup #1792

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 37 commits into from
Jun 19, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
3ad11f6
standardize on attr over style for text-anchor
alexcjohnson Jun 15, 2017
9949092
centralize data-unformatted and use it for bBox cache key
alexcjohnson Jun 15, 2017
951a75e
let Drawing.bBox handle tspan.line position reset
alexcjohnson Jun 15, 2017
639878e
fix pseudo-html rendering in rangeselectors and updatemenus
alexcjohnson Jun 15, 2017
16d7515
call bBox on text node when possible
alexcjohnson Jun 15, 2017
0b2a7b2
clean up legend sizing and fallback on missing node
alexcjohnson Jun 15, 2017
a5a578f
minor lint to bar text
alexcjohnson Jun 15, 2017
5d3648c
cleaner data-math usage
alexcjohnson Jun 15, 2017
d29159a
fix sliders with pseudo-HTML line breaks
alexcjohnson Jun 15, 2017
6eedaa5
make `positionText` and `lineCount` fns
alexcjohnson Jun 16, 2017
76fc8b8
linting related to tspan.line centralization
alexcjohnson Jun 16, 2017
61dd3b1
use display:none instead of visibility:hidden
alexcjohnson Jun 16, 2017
936936b
remove Drawing.measureText and support pseudo-HTML in carpets
alexcjohnson Jun 16, 2017
4ae49f5
prohibit Math.sign and .innerHTML in syntax test
alexcjohnson Jun 16, 2017
9f4ec66
remove Math.sign
alexcjohnson Jun 16, 2017
1a5679e
fix title alignment
alexcjohnson Jun 16, 2017
af5b7f2
update snapshot test
alexcjohnson Jun 16, 2017
4886985
oops, we need y=0 on tspan.line after all
alexcjohnson Jun 16, 2017
717c7d8
hover labels need explicit position reset for proper positioning later
alexcjohnson Jun 16, 2017
7c3dc3b
get correct bbox for carpet tick labels
alexcjohnson Jun 16, 2017
378c805
fixed range selector mocks
alexcjohnson Jun 16, 2017
f8101b0
update binding mock - new label positions are slightly different
alexcjohnson Jun 16, 2017
3b293ac
update updatemenus test
alexcjohnson Jun 16, 2017
c908ed4
lint axes.js
alexcjohnson Jun 16, 2017
0bb38e9
reorder sliders findDimensions so dummy calculations always succeed
alexcjohnson Jun 16, 2017
6c72c77
test #984 - ensure we never override visibility
alexcjohnson Jun 16, 2017
30b9fa5
remove some obsolete code and comments from sliders/draw
alexcjohnson Jun 16, 2017
930e58e
updatemenus point in the direction they'll open
alexcjohnson Jun 16, 2017
301fba7
update updatemenus_positioning mock for new arrows
alexcjohnson Jun 16, 2017
4b87b2e
fix #1782 (second half: arrowheads broken) - :hocho: parentElement
alexcjohnson Jun 16, 2017
a68f861
note about innerHTML prohibition
alexcjohnson Jun 16, 2017
c5fa44b
pull 1.3 out into alignment constants
alexcjohnson Jun 16, 2017
82ba7ee
null transform, rather than empty string
alexcjohnson Jun 16, 2017
27b4be3
selectAll -> select because it can only be one node
alexcjohnson Jun 16, 2017
46d6e44
lint titles var statements
alexcjohnson Jun 17, 2017
8305c02
let drawing.bBox cache simple nested elements
alexcjohnson Jun 17, 2017
8e3d4fa
don't recalculate nodehash for recursive bBox
alexcjohnson Jun 17, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 3 additions & 11 deletions src/components/annotations/draw.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,11 +231,6 @@ function drawRaw(gd, options, index, subplotId, xa, ya) {
wholeLink.node().appendChild(annTextBG.node());
}


// make sure lines are aligned the way they will be
// at the end, even if their position changes
annText.selectAll('tspan.line').attr({y: 0, x: 0});

var mathjaxGroup = annTextGroupInner.select('.annotation-text-math-group');
var hasMathjax = !mathjaxGroup.empty();
var anntextBB = Drawing.bBox(
Expand Down Expand Up @@ -424,12 +419,9 @@ function drawRaw(gd, options, index, subplotId, xa, ya) {
else {
var texty = borderfull + yShift - anntextBB.top,
textx = borderfull + xShift - anntextBB.left;
annText.attr({
x: textx,
y: texty
})
.call(Drawing.setClipUrl, isSizeConstrained ? annClipID : null);
annText.selectAll('tspan.line').attr({y: texty, x: textx});

annText.call(svgTextUtils.positionText, textx, texty)
.call(Drawing.setClipUrl, isSizeConstrained ? annClipID : null);
}

annTextClip.select('rect').call(Drawing.setRect, borderfull, borderfull,
Expand Down
4 changes: 2 additions & 2 deletions src/components/colorbar/draw.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ var setCursor = require('../../lib/setcursor');
var Drawing = require('../drawing');
var Color = require('../color');
var Titles = require('../titles');
var svgTextUtils = require('../../lib/svg_text_utils');

var handleAxisDefaults = require('../../plots/cartesian/axis_defaults');
var handleAxisPositionDefaults = require('../../plots/cartesian/position_defaults');
Expand Down Expand Up @@ -321,8 +322,7 @@ module.exports = function draw(gd, id) {
}
else {
cbAxisOut.domain[0] += titleHeight / gs.h;
var nlines = Math.max(1,
titleText.selectAll('tspan.line').size());
var nlines = svgTextUtils.lineCount(titleText);
titleTrans[1] += (1 - nlines) * lineSize;
}

Expand Down
35 changes: 11 additions & 24 deletions src/components/drawing/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ drawing.font = function(s, family, size, color) {
if(color) s.call(Color.fill, color);
};

/*
* Positioning helpers
* Note: do not use `setPosition` with <text> nodes modified by
* `svgTextUtils.convertToTspans`. Use `svgTextUtils.positionText`
* instead, so that <tspan.line> elements get updated to match.
*/
drawing.setPosition = function(s, x, y) { s.attr('x', x).attr('y', y); };
drawing.setSize = function(s, w, h) { s.attr('width', w).attr('height', h); };
drawing.setRect = function(s, x, y, w, h) {
Expand Down Expand Up @@ -455,19 +461,13 @@ drawing.textPointStyle = function(s, trace, gd) {
.text(text)
.call(svgTextUtils.convertToTspans, gd);
var pgroup = d3.select(this.parentNode),
tspans = p.selectAll('tspan.line'),
numLines = ((tspans[0].length || 1) - 1) * LINEEXPAND + 1,
numLines = (svgTextUtils.lineCount(p) - 1) * LINEEXPAND + 1,
dx = TEXTOFFSETSIGN[h] * r,
dy = fontSize * 0.75 + TEXTOFFSETSIGN[v] * r +
(TEXTOFFSETSIGN[v] - 1) * numLines * fontSize / 2;

// fix the overall text group position
pgroup.attr('transform', 'translate(' + dx + ',' + dy + ')');

// then fix multiline text
if(numLines > 1) {
tspans.attr({ x: p.attr('x'), y: p.attr('y') });
}
});
};

Expand Down Expand Up @@ -633,23 +633,10 @@ drawing.bBox = function(node) {
var testNode = node.cloneNode(true);
tester.appendChild(testNode);

// standardize its position - and that of any newlines if we're getting
// the bBox of a <text> element directly (not a larger container)
// newline <tspan>s need to get `x` and `y` set to match whenever the
// containing <text> element gets repositioned, and the line offset
// is handled by the attribute `dy` (that we assume is already set)
var test3 = d3.select(testNode).attr({
x: 0,
y: 0,
transform: ''
});

if(testNode.nodeName === 'text') {
test3.selectAll('tspan.line').attr({
x: 0,
y: 0
});
}
// standardize its position (and newline tspans if any)
d3.select(testNode)
.attr('transform', '')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting. Wouldn't .attr('transform', null) be d3-esque?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, that's better -> 82ba7ee

.call(svgTextUtils.positionText, 0, 0);

var testRect = testNode.getBoundingClientRect();
var refRect = drawing.testref
Expand Down
42 changes: 9 additions & 33 deletions src/components/fx/hover.js
Original file line number Diff line number Diff line change
Expand Up @@ -595,23 +595,15 @@ function createHoverText(hoverData, opts, gd) {
.attr('data-notex', 1);

ltext.text(t0)
.call(svgTextUtils.convertToTspans, gd)
.call(Drawing.setPosition, 0, 0)
.selectAll('tspan.line')
.call(Drawing.setPosition, 0, 0);
.call(svgTextUtils.convertToTspans, gd);
label.attr('transform', '');

var tbb = ltext.node().getBoundingClientRect();
if(hovermode === 'x') {
ltext.attr('text-anchor', 'middle')
.call(Drawing.setPosition, 0, (xa.side === 'top' ?
.call(svgTextUtils.positionText, 0, (xa.side === 'top' ?
(outerTop - tbb.bottom - HOVERARROWSIZE - HOVERTEXTPAD) :
(outerTop - tbb.top + HOVERARROWSIZE + HOVERTEXTPAD)))
.selectAll('tspan.line')
.attr({
x: ltext.attr('x'),
y: ltext.attr('y')
});
(outerTop - tbb.top + HOVERARROWSIZE + HOVERTEXTPAD)));

var topsign = xa.side === 'top' ? '-' : '';
lpath.attr('d', 'M0,0' +
Expand All @@ -627,14 +619,9 @@ function createHoverText(hoverData, opts, gd) {
}
else {
ltext.attr('text-anchor', ya.side === 'right' ? 'start' : 'end')
.call(Drawing.setPosition,
.call(svgTextUtils.positionText,
(ya.side === 'right' ? 1 : -1) * (HOVERTEXTPAD + HOVERARROWSIZE),
outerTop - tbb.top - tbb.height / 2)
.selectAll('tspan.line')
.attr({
x: ltext.attr('x'),
y: ltext.attr('y')
});
outerTop - tbb.top - tbb.height / 2);

var leftsign = ya.side === 'right' ? '' : '-';
lpath.attr('d', 'M0,0' +
Expand Down Expand Up @@ -742,12 +729,9 @@ function createHoverText(hoverData, opts, gd) {
d.fontFamily || fontFamily,
d.fontSize || fontSize,
d.fontColor || contrastColor)
.call(Drawing.setPosition, 0, 0)
.text(text)
.attr('data-notex', 1)
.call(svgTextUtils.convertToTspans, gd);
tx.selectAll('tspan.line')
.call(Drawing.setPosition, 0, 0);

var tx2 = g.select('text.name'),
tx2width = 0;
Expand All @@ -759,11 +743,8 @@ function createHoverText(hoverData, opts, gd) {
d.fontSize || fontSize,
traceColor)
.text(name)
.call(Drawing.setPosition, 0, 0)
.attr('data-notex', 1)
.call(svgTextUtils.convertToTspans, gd);
tx2.selectAll('tspan.line')
.call(Drawing.setPosition, 0, 0);
tx2width = tx2.node().getBoundingClientRect().width + 2 * HOVERTEXTPAD;
}
else {
Expand Down Expand Up @@ -1031,17 +1012,12 @@ function alignHoverText(hoverLabels, rotateLabels) {
'V' + (offsetY - HOVERARROWSIZE) +
'Z'));

tx.call(Drawing.setPosition,
txx + offsetX, offsetY + d.ty0 - d.by / 2 + HOVERTEXTPAD)
.selectAll('tspan.line')
.attr({
x: tx.attr('x'),
y: tx.attr('y')
});
tx.call(svgTextUtils.positionText,
txx + offsetX, offsetY + d.ty0 - d.by / 2 + HOVERTEXTPAD);

if(d.tx2width) {
g.select('text.name, text.name tspan.line')
.call(Drawing.setPosition,
g.select('text.name')
.call(svgTextUtils.positionText,
tx2x + alignShift * HOVERTEXTPAD + offsetX,
offsetY + d.ty0 - d.by / 2 + HOVERTEXTPAD);
g.select('rect')
Expand Down
15 changes: 4 additions & 11 deletions src/components/legend/draw.js
Original file line number Diff line number Diff line change
Expand Up @@ -370,19 +370,14 @@ function drawTexts(g, gd) {
var text = g.selectAll('text.legendtext')
.data([0]);
text.enter().append('text').classed('legendtext', true);
text.attr({
x: 40,
y: 0,
'text-anchor': 'start'
})
text.attr('text-anchor', 'start')
.classed('user-select-none', true)
.call(Drawing.font, fullLayout.legend.font)
.text(name);

function textLayout(s) {
svgTextUtils.convertToTspans(s, gd, function() {
s.selectAll('tspan.line').attr({x: s.attr('x')});
g.call(computeTextDimensions, gd);
computeTextDimensions(g, gd);
});
}

Expand Down Expand Up @@ -577,8 +572,7 @@ function computeTextDimensions(g, gd) {
}
else {
var text = g.selectAll('.legendtext'),
textSpans = text.selectAll('tspan.line'),
textLines = textSpans.size() || 1,
textLines = svgTextUtils.lineCount(text),
textNode = text.node();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there always only one <.legendtext> node per g? If so, we could replace the

var text = g.selectAll('.legendtext')

line with

var text = g.select('.legendtext')

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, that's a bit cleaner -> 27b4be3


height = lineHeight * textLines;
Expand All @@ -587,8 +581,7 @@ function computeTextDimensions(g, gd) {
// approximation to height offset to center the font
// to avoid getBoundingClientRect
var textY = lineHeight * (0.3 + (1 - textLines) / 2);
text.attr('y', textY);
textSpans.attr('y', textY);
svgTextUtils.positionText(text, 40, textY);
}

height = Math.max(height, 16) + 3;
Expand Down
22 changes: 6 additions & 16 deletions src/components/rangeselector/draw.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,6 @@ function getFillColor(selectorLayout, d) {
function drawButtonText(button, selectorLayout, d, gd) {
function textLayout(s) {
svgTextUtils.convertToTspans(s, gd);

// TODO do we need anything else here?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... I guess not. 😆

}

var text = button.selectAll('text')
Expand Down Expand Up @@ -183,25 +181,22 @@ function reposition(gd, buttons, opts, axName) {

buttons.each(function() {
var button = d3.select(this),
text = button.select('.selector-text'),
tspans = text.selectAll('tspan.line');
text = button.select('.selector-text');

var tHeight = opts.font.size * 1.3,
tLines = tspans.size() || 1,
hEff = Math.max(tHeight * tLines, 16) + 3;
hEff = Math.max(tHeight * svgTextUtils.lineCount(text), 16) + 3;

opts.height = Math.max(opts.height, hEff);
});

buttons.each(function() {
var button = d3.select(this),
rect = button.select('.selector-rect'),
text = button.select('.selector-text'),
tspans = text.selectAll('tspan.line');
text = button.select('.selector-text');

var tWidth = text.node() && Drawing.bBox(text.node()).width,
tHeight = opts.font.size * 1.3,
tLines = tspans.size() || 1;
tLines = svgTextUtils.lineCount(text);

var wEff = Math.max(tWidth + 10, constants.minButtonWidth);

Expand All @@ -220,13 +215,8 @@ function reposition(gd, buttons, opts, axName) {
height: opts.height
});

var textAttrs = {
x: wEff / 2,
y: opts.height / 2 - ((tLines - 1) * tHeight / 2) + 3
};

text.attr(textAttrs);
tspans.attr(textAttrs);
svgTextUtils.positionText(text, wEff / 2,
opts.height / 2 - ((tLines - 1) * tHeight / 2) + 3);

opts.width += wEff + 5;
});
Expand Down
18 changes: 6 additions & 12 deletions src/components/sliders/draw.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ var anchorUtils = require('../legend/anchor_utils');

var constants = require('./constants');

var RESETATTRS = {x: 0, y: 0};


module.exports = function draw(gd) {
var fullLayout = gd._fullLayout,
Expand Down Expand Up @@ -165,7 +163,7 @@ function findDimensions(gd, sliderOpts) {
sliderLabels.each(function(stepOpts) {
var curValPrefix = drawCurrentValue(dummyGroup, sliderOpts, stepOpts.label);
var curValSize = (curValPrefix.node() && Drawing.bBox(curValPrefix.node())) || {width: 0, height: 0};
var lines = curValPrefix.selectAll('tspan.line').size() || 1;
var lines = svgTextUtils.lineCount(curValPrefix);
sliderOpts.currentValueMaxWidth = Math.max(sliderOpts.currentValueMaxWidth, Math.ceil(curValSize.width));
sliderOpts.currentValueHeight = Math.max(sliderOpts.currentValueHeight, Math.ceil(curValSize.height));
sliderOpts.currentValueMaxLines = Math.max(sliderOpts.currentValueMaxLines, lines);
Expand Down Expand Up @@ -313,15 +311,14 @@ function drawCurrentValue(sliderGroup, sliderOpts, valueOverride) {

text.call(Drawing.font, sliderOpts.currentvalue.font)
.text(str)
.call(svgTextUtils.convertToTspans, sliderOpts.gd)
.attr(RESETATTRS);
.call(svgTextUtils.convertToTspans, sliderOpts.gd);

var tspans = text.selectAll('tspan.line').attr(RESETATTRS);
var lines = svgTextUtils.lineCount(text);

var y0 = (sliderOpts.currentValueMaxLines + 1 - (tspans.size() || 1)) *
var y0 = (sliderOpts.currentValueMaxLines + 1 - lines) *
sliderOpts.currentvalue.font.size * constants.fontSizeToHeight;

Drawing.setTranslate(text, x0, y0);
svgTextUtils.positionText(text, x0, y0);

return text;
}
Expand Down Expand Up @@ -360,10 +357,7 @@ function drawLabel(item, data, sliderOpts) {

text.call(Drawing.font, sliderOpts.font)
.text(data.step.label)
.call(svgTextUtils.convertToTspans, sliderOpts.gd)
.attr(RESETATTRS);

text.selectAll('tspan.line').attr(RESETATTRS);
.call(svgTextUtils.convertToTspans, sliderOpts.gd);

return text;
}
Expand Down
12 changes: 4 additions & 8 deletions src/components/titles/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,9 @@ Titles.draw = function(gd, titleClass, options) {
opacity: opacity * Color.opacity(fontColor),
'font-weight': Plots.fontWeight
})
.attr(attributes)
.call(svgTextUtils.convertToTspans, gd)
.attr(attributes);
.call(svgTextUtils.positionText, attributes.x, attributes.y)
.call(svgTextUtils.convertToTspans, gd);

titleEl.selectAll('tspan.line')
.attr(attributes);
return Plots.previousPromises(gd);
}

Expand Down Expand Up @@ -226,9 +223,8 @@ Titles.draw = function(gd, titleClass, options) {
.call(titleLayout);
})
.on('input', function(d) {
this.text(d || ' ').attr(attributes)
.selectAll('tspan.line')
.attr(attributes);
this.text(d || ' ')
.call(svgTextUtils.positionText, attributes.x, attributes.y);
});
}
el.classed('js-placeholder', isplaceholder);
Expand Down
Loading