Skip to content

Commit c1d19b4

Browse files
committed
fixes non-wrapping wrappable case by including it in the alignment-baseline styling; better structure for text handling logic
1 parent b9c8de6 commit c1d19b4

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

src/traces/table/plot.js

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -463,22 +463,30 @@ function sizeAndStyleRect(cellRect) {
463463
function populateCellText(cellText, tableControlView, allColumnBlock, gd) {
464464
cellText
465465
.text(function(d) {
466+
466467
var col = d.column.specIndex;
467468
var row = d.rowNumber;
469+
468470
var userSuppliedContent = d.value;
471+
var stringSupplied = (typeof userSuppliedContent === 'string');
472+
var hasBreaks = stringSupplied && userSuppliedContent.match(/<br>/i);
473+
var userBrokenText = !stringSupplied || hasBreaks;
474+
d.mayHaveMarkup = stringSupplied && userSuppliedContent.match(/[<&>]/);
475+
469476
var latex = isLatex(userSuppliedContent);
470-
var userBrokenText = (typeof userSuppliedContent !== 'string') || userSuppliedContent.match(/<br>/i);
471-
var userBrokenText2 = (typeof userSuppliedContent === 'string') && userSuppliedContent.match(/<br>/i);
477+
d.latex = latex;
478+
472479
var prefix = latex ? '' : gridPick(d.calcdata.cells.prefix, col, row) || '';
473480
var suffix = latex ? '' : gridPick(d.calcdata.cells.suffix, col, row) || '';
474481
var format = latex ? null : gridPick(d.calcdata.cells.format, col, row) || null;
482+
475483
var prefixSuffixedText = prefix + (format ? d3.format(format)(d.value) : d.value) + suffix;
476-
d.latex = latex;
477-
d.mayHaveMarkup = (typeof userSuppliedContent === 'string') && userSuppliedContent.match(/[<&>]/);
484+
478485
var hasWrapSplitCharacter;
479-
var hwsc = function(prefixSuffixedText) {return prefixSuffixedText.indexOf(c.wrapSplitCharacter) !== -1;};
480-
d.wrappingNeeded = !d.wrapped && !userBrokenText && !latex && (hasWrapSplitCharacter = hwsc(prefixSuffixedText));
481-
d.cellHeightMayIncrease = userBrokenText2 || latex || d.mayHaveMarkup || (hasWrapSplitCharacter === void(0) ? hwsc(prefixSuffixedText) : hasWrapSplitCharacter);
486+
d.wrappingNeeded = !d.wrapped && !userBrokenText && !latex && (hasWrapSplitCharacter = hasWrapCharacter(prefixSuffixedText));
487+
d.cellHeightMayIncrease = hasBreaks || latex || d.mayHaveMarkup || (hasWrapSplitCharacter === void(0) ? hasWrapCharacter(prefixSuffixedText) : hasWrapSplitCharacter);
488+
d.needsConvertToTspans = d.mayHaveMarkup || d.wrappingNeeded || d.latex;
489+
482490
var textToRender;
483491
if(d.wrappingNeeded) {
484492
var hrefPreservedText = c.wrapSplitCharacter === ' ' ? prefixSuffixedText.replace(/<a href=/ig, '<a_href=') : prefixSuffixedText;
@@ -494,7 +502,7 @@ function populateCellText(cellText, tableControlView, allColumnBlock, gd) {
494502
return textToRender;
495503
})
496504
.attr('alignment-baseline', function(d) {
497-
return d.cellHeightMayIncrease ? null : 'hanging';
505+
return d.needsConvertToTspans ? null : 'hanging';
498506
})
499507
.each(function(d) {
500508

@@ -504,7 +512,7 @@ function populateCellText(cellText, tableControlView, allColumnBlock, gd) {
504512
// finalize what's in the DOM
505513

506514
var renderCallback = d.wrappingNeeded ? wrapTextMaker : updateYPositionMaker;
507-
if(d.mayHaveMarkup || d.wrappingNeeded || d.latex) {
515+
if(d.needsConvertToTspans) {
508516
svgUtil.convertToTspans(selection, gd, renderCallback(allColumnBlock, element, tableControlView, gd, d));
509517
} else {
510518
d3.select(element.parentNode)
@@ -525,6 +533,8 @@ function isLatex(content) {
525533
return typeof content === 'string' && content.match(c.latexCheck);
526534
}
527535

536+
function hasWrapCharacter(text) {return text.indexOf(c.wrapSplitCharacter) !== -1;}
537+
528538
function columnMoved(gd, calcdata, i, indices) {
529539
var o = calcdata[i][0].gdColumnsOriginalOrder;
530540
calcdata[i][0].gdColumns.sort(function(a, b) {
@@ -609,7 +619,7 @@ function splitToCells(d) {
609619
// But it has to be busted when `svgUtil.convertToTspans` is used as it reshapes cell subtrees asynchronously,
610620
// and by that time the user may have scrolled away, resulting in stale overwrites. The real solution will be
611621
// to turn `svgUtil.convertToTspans` into a cancelable request, in which case no key busting is needed.
612-
var buster = (typeof v === 'string') && v.match(/[<$&>]/) ? '_keybuster_' + Math.random() : '';
622+
var buster = (typeof v === 'string') && v.match(/[<$&> ]/) ? '_keybuster_' + Math.random() : '';
613623
return {
614624
// keyWithinBlock: /*fromTo[0] + */i, // optimized future version - no busting
615625
// keyWithinBlock: fromTo[0] + i, // initial always-unoptimized version - janky scrolling with 5+ columns

0 commit comments

Comments
 (0)