Skip to content
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

No innerHTML and pseudo-html cleanup #1792

Merged
merged 37 commits into from
Jun 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
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
20 changes: 5 additions & 15 deletions src/components/annotations/draw.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,6 @@ function drawRaw(gd, options, index, subplotId, xa, ya) {

var annText = annTextGroupInner.append('text')
.classed('annotation-text', true)
.attr('data-unformatted', options.text)
.text(options.text);

function textLayout(s) {
Expand Down Expand Up @@ -232,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 @@ -423,14 +417,11 @@ function drawRaw(gd, options, index, subplotId, xa, ya) {
.call(Drawing.setClipUrl, isSizeConstrained ? annClipID : null);
}
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});
var texty = borderfull + yShift - anntextBB.top;
var textx = borderfull + xShift - anntextBB.left;

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

annTextClip.select('rect').call(Drawing.setRect, borderfull, borderfull,
Expand Down Expand Up @@ -693,7 +684,6 @@ function drawRaw(gd, options, index, subplotId, xa, ya) {
.call(textLayout)
.on('edit', function(_text) {
options.text = _text;
this.attr({'data-unformatted': options.text});
this.call(textLayout);

var update = {};
Expand Down
2 changes: 1 addition & 1 deletion src/components/annotations/draw_arrow_head.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ module.exports = function drawArrowHead(el3, style, ends, mag, standoff) {
function drawhead(p, rot) {
if(!headStyle.path) return;
if(style > 5) rot = 0; // don't rotate square or circle
d3.select(el.parentElement).append('path')
d3.select(el.parentNode).append('path')
.attr({
'class': el3.attr('class'),
d: headStyle.path,
Expand Down
10 changes: 5 additions & 5 deletions src/components/colorbar/draw.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ 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 LINE_SPACING = require('../../constants/alignment').LINE_SPACING;

var handleAxisDefaults = require('../../plots/cartesian/axis_defaults');
var handleAxisPositionDefaults = require('../../plots/cartesian/position_defaults');
Expand Down Expand Up @@ -296,7 +298,7 @@ module.exports = function draw(gd, id) {
lineSize = 15.6;
if(titleText.node()) {
lineSize =
parseInt(titleText.style('font-size'), 10) * 1.3;
parseInt(titleText.style('font-size'), 10) * LINE_SPACING;
}
if(mathJaxNode) {
titleHeight = Drawing.bBox(mathJaxNode).height;
Expand All @@ -308,8 +310,7 @@ module.exports = function draw(gd, id) {
}
else if(titleText.node() &&
!titleText.classed('js-placeholder')) {
titleHeight = Drawing.bBox(
titleGroup.node()).height;
titleHeight = Drawing.bBox(titleText.node()).height;
}
if(titleHeight) {
// buffer btwn colorbar and title
Expand All @@ -322,8 +323,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
147 changes: 91 additions & 56 deletions src/components/drawing/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ var Lib = require('../../lib');
var svgTextUtils = require('../../lib/svg_text_utils');

var xmlnsNamespaces = require('../../constants/xmlns_namespaces');
var alignment = require('../../constants/alignment');
var LINE_SPACING = alignment.LINE_SPACING;

var subTypes = require('../../traces/scatter/subtypes');
var makeBubbleSizeFn = require('../../traces/scatter/make_bubble_size_func');

Expand All @@ -41,6 +44,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 @@ -420,8 +429,7 @@ drawing.tryColorscale = function(marker, prefix) {
};

// draw text at points
var TEXTOFFSETSIGN = {start: 1, end: -1, middle: 0, bottom: 1, top: -1},
LINEEXPAND = 1.3;
var TEXTOFFSETSIGN = {start: 1, end: -1, middle: 0, bottom: 1, top: -1};
drawing.textPointStyle = function(s, trace, gd) {
s.each(function(d) {
var p = d3.select(this),
Expand Down Expand Up @@ -454,20 +462,15 @@ drawing.textPointStyle = function(s, trace, gd) {
.attr('text-anchor', h)
.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,
dx = TEXTOFFSETSIGN[h] * r,
dy = fontSize * 0.75 + TEXTOFFSETSIGN[v] * r +

var pgroup = d3.select(this.parentNode);
var numLines = (svgTextUtils.lineCount(p) - 1) * LINE_SPACING + 1;
var dx = TEXTOFFSETSIGN[h] * r;
var 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 @@ -606,33 +609,87 @@ drawing.makeTester = function() {
drawing.testref = testref;
};

// use our offscreen tester to get a clientRect for an element,
// in a reference frame where it isn't translated and its anchor
// point is at (0,0)
// always returns a copy of the bbox, so the caller can modify it safely
/*
* use our offscreen tester to get a clientRect for an element,
* in a reference frame where it isn't translated and its anchor
* point is at (0,0)
* always returns a copy of the bbox, so the caller can modify it safely
*/
drawing.savedBBoxes = {};
var savedBBoxesCount = 0;
var maxSavedBBoxes = 10000;

drawing.bBox = function(node) {
// cache elements we've already measured so we don't have to
// remeasure the same thing many times
var hash = nodeHash(node);
var out = drawing.savedBBoxes[hash];
if(out) return Lib.extendFlat({}, out);
drawing.bBox = function(node, hash) {
/*
* Cache elements we've already measured so we don't have to
* remeasure the same thing many times
* We have a few bBox callers though who pass a node larger than
* a <text> or a MathJax <g>, such as an axis group containing many labels.
* These will not generate a hash (unless we figure out an appropriate
* hash key for them) and thus we will not hash them.
*/
if(!hash) hash = nodeHash(node);
var out;
if(hash) {
out = drawing.savedBBoxes[hash];
if(out) return Lib.extendFlat({}, out);
}
else if(node.children.length === 1) {
/*
* If we have only one child element, which is itself hashable, make
* a new hash from this element plus its x,y,transform
* These bounding boxes *include* x,y,transform - mostly for use by
* callers trying to avoid overlaps (ie titles)
*/
var innerNode = node.children[0];

hash = nodeHash(innerNode);
if(hash) {
var x = +innerNode.getAttribute('x') || 0;
var y = +innerNode.getAttribute('y') || 0;
var transform = innerNode.getAttribute('transform');

if(!transform) {
// in this case, just varying x and y, don't bother caching
// the final bBox because the alteration is quick.
var innerBB = drawing.bBox(innerNode, hash);
if(x) {
innerBB.left += x;
innerBB.right += x;
}
if(y) {
innerBB.top += y;
innerBB.bottom += y;
}
return innerBB;
}
/*
* else we have a transform - rather than make a complicated
* (and error-prone and probably slow) transform parser/calculator,
* just continue on calculating the boundingClientRect of the group
* and use the new composite hash to cache it.
* That said, `innerNode.transform.baseVal` is an array of
* `SVGTransform` objects, that *do* seem to have a nice matrix
* multiplication interface that we could use to avoid making
* another getBoundingClientRect call...
*/
hash += '~' + x + '~' + y + '~' + transform;

out = drawing.savedBBoxes[hash];
if(out) return Lib.extendFlat({}, out);
}
}

var tester = drawing.tester.node();

// copy the node to test into the tester
var testNode = node.cloneNode(true);
tester.appendChild(testNode);

// standardize its position... do we really want to do this?
d3.select(testNode).attr({
x: 0,
y: 0,
transform: ''
});
// standardize its position (and newline tspans if any)
d3.select(testNode)
.attr('transform', null)
.call(svgTextUtils.positionText, 0, 0);

var testRect = testNode.getBoundingClientRect();
var refRect = drawing.testref
Expand All @@ -655,35 +712,23 @@ drawing.bBox = function(node) {
// by saving boxes for long-gone elements
if(savedBBoxesCount >= maxSavedBBoxes) {
drawing.savedBBoxes = {};
maxSavedBBoxes = 0;
savedBBoxesCount = 0;
}

// cache this bbox
drawing.savedBBoxes[hash] = bb;
if(hash) drawing.savedBBoxes[hash] = bb;
savedBBoxesCount++;

return Lib.extendFlat({}, bb);
};

// capture everything about a node (at least in our usage) that
// impacts its bounding box, given that bBox clears x, y, and transform
// TODO: is this really everything? Is it worth taking only parts of style,
// so we can share across more changes (like colors)? I guess we can't strip
// colors and stuff from inside innerHTML so maybe not worth bothering outside.
// TODO # 2: this can be long, so could take a lot of memory, do we want to
// hash it? But that can be slow...
// extracting this string from a typical element takes ~3 microsec, where
// doing a simple hash ala https://stackoverflow.com/questions/7616461
// adds ~15 microsec (nearly all of this is spent in charCodeAt)
// function hash(s) {
// var h = 0;
// for (var i = 0; i < s.length; i++) {
// h = (((h << 5) - h) + s.charCodeAt(i)) | 0; // codePointAt?
// }
// return h;
// }
function nodeHash(node) {
return node.innerHTML +
var inputText = node.getAttribute('data-unformatted');
if(inputText === null) return;
return inputText +
node.getAttribute('data-math') +
node.getAttribute('text-anchor') +
node.getAttribute('style');
}
Expand Down Expand Up @@ -841,13 +886,3 @@ drawing.setTextPointsScale = function(selection, xScale, yScale) {
el.attr('transform', transforms.join(' '));
});
};

drawing.measureText = function(tester, text, font) {
var dummyText = tester.append('text')
.text(text)
.call(drawing.font, font);

var bbox = drawing.bBox(dummyText.node());
dummyText.remove();
return bbox;
};
Loading