11
11
12
12
/* global MathJax:false */
13
13
14
- var Plotly = require ( '../plotly' ) ;
15
14
var d3 = require ( 'd3' ) ;
16
15
17
16
var Lib = require ( '../lib' ) ;
18
17
var xmlnsNamespaces = require ( '../constants/xmlns_namespaces' ) ;
19
-
20
- var util = module . exports = { } ;
18
+ var stringMappings = require ( '../constants/string_mappings' ) ;
21
19
22
20
// Append SVG
23
21
@@ -45,7 +43,7 @@ d3.selection.prototype.appendSVG = function(_svgString) {
45
43
46
44
// Text utilities
47
45
48
- util . html_entity_decode = function ( s ) {
46
+ exports . html_entity_decode = function ( s ) {
49
47
var hiddenDiv = d3 . select ( 'body' ) . append ( 'div' ) . style ( { display : 'none' } ) . html ( '' ) ;
50
48
var replaced = s . replace ( / ( & [ ^ ; ] * ; ) / gi, function ( d ) {
51
49
if ( d === '<' ) { return '<' ; } // special handling for brackets
@@ -56,7 +54,7 @@ util.html_entity_decode = function(s) {
56
54
return replaced ;
57
55
} ;
58
56
59
- util . xml_entity_encode = function ( str ) {
57
+ exports . xml_entity_encode = function ( str ) {
60
58
return str . replace ( / & (? ! \w + ; | \# [ 0 - 9 ] + ; | \# x [ 0 - 9 A - F ] + ; ) / g, '&' ) ;
61
59
} ;
62
60
@@ -66,10 +64,11 @@ function getSize(_selection, _dimension) {
66
64
return _selection . node ( ) . getBoundingClientRect ( ) [ _dimension ] ;
67
65
}
68
66
69
- util . convertToTspans = function ( _context , _callback ) {
67
+ exports . convertToTspans = function ( _context , _callback ) {
70
68
var str = _context . text ( ) ;
71
69
var converted = convertToSVG ( str ) ;
72
70
var that = _context ;
71
+
73
72
// Until we get tex integrated more fully (so it can be used along with non-tex)
74
73
// allow some elements to prohibit it by attaching 'data-notex' to the original
75
74
var tex = ( ! that . attr ( 'data-notex' ) ) && converted . match ( / ( [ ^ $ ] * ) ( [ $ ] + [ ^ $ ] * [ $ ] + ) ( [ ^ $ ] * ) / ) ;
@@ -112,7 +111,7 @@ util.convertToTspans = function(_context, _callback) {
112
111
}
113
112
114
113
if ( tex ) {
115
- var td = Plotly . Lib . getPlotDiv ( that . node ( ) ) ;
114
+ var td = Lib . getPlotDiv ( that . node ( ) ) ;
116
115
( ( td && td . _promises ) || [ ] ) . push ( new Promise ( function ( resolve ) {
117
116
that . style ( { visibility : 'hidden' } ) ;
118
117
var config = { fontSize : parseInt ( that . style ( 'font-size' ) , 10 ) } ;
@@ -195,7 +194,7 @@ function cleanEscapesForTex(s) {
195
194
}
196
195
197
196
function texToSVG ( _texString , _config , _callback ) {
198
- var randomID = 'math-output-' + Plotly . Lib . randstr ( [ ] , 64 ) ;
197
+ var randomID = 'math-output-' + Lib . randstr ( [ ] , 64 ) ;
199
198
var tmpDiv = d3 . select ( 'body' ) . append ( 'div' )
200
199
. attr ( { id : randomID } )
201
200
. style ( { visibility : 'hidden' , position : 'absolute' } )
@@ -236,22 +235,48 @@ var PROTOCOLS = ['http:', 'https:', 'mailto:'];
236
235
237
236
var STRIP_TAGS = new RegExp ( '</?(' + Object . keys ( TAG_STYLES ) . join ( '|' ) + ')( [^>]*)?/?>' , 'g' ) ;
238
237
239
- util . plainText = function ( _str ) {
238
+ var ENTITY_TO_UNICODE = Object . keys ( stringMappings . entityToUnicode ) . map ( function ( k ) {
239
+ return {
240
+ regExp : new RegExp ( '&' + k + ';' , 'g' ) ,
241
+ sub : stringMappings . entityToUnicode [ k ]
242
+ } ;
243
+ } ) ;
244
+
245
+ var UNICODE_TO_ENTITY = Object . keys ( stringMappings . unicodeToEntity ) . map ( function ( k ) {
246
+ return {
247
+ regExp : new RegExp ( k , 'g' ) ,
248
+ sub : '&' + stringMappings . unicodeToEntity [ k ] + ';'
249
+ } ;
250
+ } ) ;
251
+
252
+ exports . plainText = function ( _str ) {
240
253
// strip out our pseudo-html so we have a readable
241
254
// version to put into text fields
242
255
return ( _str || '' ) . replace ( STRIP_TAGS , ' ' ) ;
243
256
} ;
244
257
258
+ function replaceFromMapObject ( _str , list ) {
259
+ var out = _str || '' ;
260
+
261
+ for ( var i = 0 ; i < list . length ; i ++ ) {
262
+ var item = list [ i ] ;
263
+ out = out . replace ( item . regExp , item . sub ) ;
264
+ }
265
+
266
+ return out ;
267
+ }
268
+
269
+ function convertEntities ( _str ) {
270
+ return replaceFromMapObject ( _str , ENTITY_TO_UNICODE ) ;
271
+ }
272
+
245
273
function encodeForHTML ( _str ) {
246
- return ( _str || '' ) . replace ( / & / g, '&' )
247
- . replace ( / < / g, '<' )
248
- . replace ( / > / g, '>' )
249
- . replace ( / " / g, '"' )
250
- . replace ( / ' / g, ''' )
251
- . replace ( / \/ / g, '/' ) ;
274
+ return replaceFromMapObject ( _str , UNICODE_TO_ENTITY ) ;
252
275
}
253
276
254
277
function convertToSVG ( _str ) {
278
+ _str = convertEntities ( _str ) ;
279
+
255
280
var result = _str
256
281
. split ( / ( < [ ^ < > ] * > ) / ) . map ( function ( d ) {
257
282
var match = d . match ( / < ( \/ ? ) ( [ ^ > ] * ) \s * ( .* ) > / i) ,
@@ -270,6 +295,7 @@ function convertToSVG(_str) {
270
295
* resurrect it.
271
296
*/
272
297
extraStyle = extra . match ( / ^ s t y l e \s * = \s * " ( [ ^ " ] + ) " \s * / i) ;
298
+
273
299
// anchor and br are the only ones that don't turn into a tspan
274
300
if ( tag === 'a' ) {
275
301
if ( close ) return '</a>' ;
@@ -316,7 +342,7 @@ function convertToSVG(_str) {
316
342
}
317
343
}
318
344
else {
319
- return Plotly . util . xml_entity_encode ( d ) . replace ( / < / g, '<' ) ;
345
+ return exports . xml_entity_encode ( d ) . replace ( / < / g, '<' ) ;
320
346
}
321
347
} ) ;
322
348
@@ -397,7 +423,7 @@ function alignHTMLWith(_base, container, options) {
397
423
398
424
// Editable title
399
425
400
- util . makeEditable = function ( context , _delegate , options ) {
426
+ exports . makeEditable = function ( context , _delegate , options ) {
401
427
if ( ! options ) options = { } ;
402
428
var that = this ;
403
429
var dispatch = d3 . dispatch ( 'edit' , 'input' , 'cancel' ) ;
@@ -431,7 +457,7 @@ util.makeEditable = function(context, _delegate, options) {
431
457
}
432
458
433
459
function appendEditable ( ) {
434
- var plotDiv = d3 . select ( Plotly . Lib . getPlotDiv ( that . node ( ) ) ) ,
460
+ var plotDiv = d3 . select ( Lib . getPlotDiv ( that . node ( ) ) ) ,
435
461
container = plotDiv . select ( '.svg-container' ) ,
436
462
div = container . append ( 'div' ) ;
437
463
div . classed ( 'plugin-editable editable' , true )
0 commit comments