Skip to content

Commit 46866fd

Browse files
authored
Merge pull request #4176 from plotly/scattermapbox-text-br
Properly handle <br> and \n in scattermapbox text
2 parents 4ef2f80 + b5a3f25 commit 46866fd

File tree

5 files changed

+37
-6
lines changed

5 files changed

+37
-6
lines changed

src/lib/svg_text_utils.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ var ZERO_WIDTH_SPACE = '\u200b';
259259
*/
260260
var PROTOCOLS = ['http:', 'https:', 'mailto:', '', undefined, ':'];
261261

262-
var NEWLINES = /(\r\n?|\n)/g;
262+
var NEWLINES = exports.NEWLINES = /(\r\n?|\n)/g;
263263

264264
var SPLIT_TAGS = /(<[^<>]*>)/;
265265

src/traces/scattermapbox/convert.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ var makeBubbleSizeFn = require('../scatter/make_bubble_size_func');
2020
var subTypes = require('../scatter/subtypes');
2121
var convertTextOpts = require('../../plots/mapbox/convert_text_opts');
2222

23+
var NEWLINES = require('../../lib/svg_text_utils').NEWLINES;
24+
var BR_TAG_ALL = require('../../lib/svg_text_utils').BR_TAG_ALL;
25+
2326
module.exports = function convert(gd, calcTrace) {
2427
var trace = calcTrace[0].trace;
2528

@@ -234,14 +237,13 @@ function makeSymbolGeoJSON(calcTrace, gd) {
234237

235238
var marker = trace.marker || {};
236239
var symbol = marker.symbol;
237-
var text = trace.text;
238240

239241
var fillSymbol = (symbol !== 'circle') ?
240242
getFillFunc(symbol) :
241243
blankFillFunc;
242244

243245
var fillText = subTypes.hasText(trace) ?
244-
getFillFunc(text) :
246+
getFillFunc(trace.text) :
245247
blankFillFunc;
246248

247249
var features = [];
@@ -261,6 +263,9 @@ function makeSymbolGeoJSON(calcTrace, gd) {
261263
calcPt.txt = Lib.texttemplateString(txti, {}, gd._fullLayout._d3locale, calcPt, trace._meta || {});
262264
}
263265

266+
var text = txt ? calcPt.txt : fillText(calcPt.tx);
267+
if(text) text = text.replace(NEWLINES, '').replace(BR_TAG_ALL, '\n');
268+
264269
features.push({
265270
type: 'Feature',
266271
geometry: {
@@ -269,7 +274,7 @@ function makeSymbolGeoJSON(calcTrace, gd) {
269274
},
270275
properties: {
271276
symbol: fillSymbol(calcPt.mx),
272-
text: txt ? calcPt.txt : fillText(calcPt.tx)
277+
text: text
273278
}
274279
});
275280
}
-16.7 KB
Loading

test/image/mocks/mapbox_bubbles-text.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
},
2828
"text": [
2929
"A",
30-
"B",
31-
"C"
30+
"B \n b",
31+
"C <br> c"
3232
],
3333
"textposition": "top left",
3434
"textfont": {

test/jasmine/tests/scattermapbox_test.js

+26
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,32 @@ describe('scattermapbox convert', function() {
493493
expect(actualText).toEqual(['A', 'B', 'C', 'F', '']);
494494
});
495495

496+
it('should convert \\n to \'\' and <br> to \\n', function() {
497+
var opts = _convert(Lib.extendFlat({}, base, {
498+
mode: 'text',
499+
text: ['one\nline', 'two<br>lines', 'three<BR>lines<br />yep']
500+
}));
501+
502+
var actualText = opts.symbol.geojson.features.map(function(f) {
503+
return f.properties.text;
504+
});
505+
506+
expect(actualText).toEqual(['oneline', 'two\nlines', 'three\nlines\nyep', undefined, undefined]);
507+
});
508+
509+
it('should convert \\n to \'\' and <br> to \\n - texttemplate case', function() {
510+
var opts = _convert(Lib.extendFlat({}, base, {
511+
mode: 'text',
512+
texttemplate: ['%{lon}\none\nline', '%{lat}<br>two<br>lines', '%{lon}\n%{lat}<br>more<br>lines']
513+
}));
514+
515+
var actualText = opts.symbol.geojson.features.map(function(f) {
516+
return f.properties.text;
517+
});
518+
519+
expect(actualText).toEqual(['10oneline', '20\ntwo\nlines', '3010\nmore\nlines', '', '']);
520+
});
521+
496522
it('should generate correct output for texttemplate', function() {
497523
var mock = {
498524
'type': 'scattermapbox',

0 commit comments

Comments
 (0)