1111
1212/* global MathJax:false */
1313
14- var Plotly = require ( '../plotly' ) ;
1514var d3 = require ( 'd3' ) ;
1615
1716var Lib = require ( '../lib' ) ;
1817var xmlnsNamespaces = require ( '../constants/xmlns_namespaces' ) ;
19-
20- var util = module . exports = { } ;
18+ var stringMappings = require ( '../constants/string_mappings' ) ;
2119
2220// Append SVG
2321
@@ -45,7 +43,7 @@ d3.selection.prototype.appendSVG = function(_svgString) {
4543
4644// Text utilities
4745
48- util . html_entity_decode = function ( s ) {
46+ exports . html_entity_decode = function ( s ) {
4947 var hiddenDiv = d3 . select ( 'body' ) . append ( 'div' ) . style ( { display : 'none' } ) . html ( '' ) ;
5048 var replaced = s . replace ( / ( & [ ^ ; ] * ; ) / gi, function ( d ) {
5149 if ( d === '<' ) { return '<' ; } // special handling for brackets
@@ -56,7 +54,7 @@ util.html_entity_decode = function(s) {
5654 return replaced ;
5755} ;
5856
59- util . xml_entity_encode = function ( str ) {
57+ exports . xml_entity_encode = function ( str ) {
6058 return str . replace ( / & (? ! \w + ; | \# [ 0 - 9 ] + ; | \# x [ 0 - 9 A - F ] + ; ) / g, '&' ) ;
6159} ;
6260
@@ -66,10 +64,11 @@ function getSize(_selection, _dimension) {
6664 return _selection . node ( ) . getBoundingClientRect ( ) [ _dimension ] ;
6765}
6866
69- util . convertToTspans = function ( _context , _callback ) {
67+ exports . convertToTspans = function ( _context , _callback ) {
7068 var str = _context . text ( ) ;
7169 var converted = convertToSVG ( str ) ;
7270 var that = _context ;
71+
7372 // Until we get tex integrated more fully (so it can be used along with non-tex)
7473 // allow some elements to prohibit it by attaching 'data-notex' to the original
7574 var tex = ( ! that . attr ( 'data-notex' ) ) && converted . match ( / ( [ ^ $ ] * ) ( [ $ ] + [ ^ $ ] * [ $ ] + ) ( [ ^ $ ] * ) / ) ;
@@ -112,7 +111,7 @@ util.convertToTspans = function(_context, _callback) {
112111 }
113112
114113 if ( tex ) {
115- var td = Plotly . Lib . getPlotDiv ( that . node ( ) ) ;
114+ var td = Lib . getPlotDiv ( that . node ( ) ) ;
116115 ( ( td && td . _promises ) || [ ] ) . push ( new Promise ( function ( resolve ) {
117116 that . style ( { visibility : 'hidden' } ) ;
118117 var config = { fontSize : parseInt ( that . style ( 'font-size' ) , 10 ) } ;
@@ -195,7 +194,7 @@ function cleanEscapesForTex(s) {
195194}
196195
197196function texToSVG ( _texString , _config , _callback ) {
198- var randomID = 'math-output-' + Plotly . Lib . randstr ( [ ] , 64 ) ;
197+ var randomID = 'math-output-' + Lib . randstr ( [ ] , 64 ) ;
199198 var tmpDiv = d3 . select ( 'body' ) . append ( 'div' )
200199 . attr ( { id : randomID } )
201200 . style ( { visibility : 'hidden' , position : 'absolute' } )
@@ -236,22 +235,48 @@ var PROTOCOLS = ['http:', 'https:', 'mailto:'];
236235
237236var STRIP_TAGS = new RegExp ( '</?(' + Object . keys ( TAG_STYLES ) . join ( '|' ) + ')( [^>]*)?/?>' , 'g' ) ;
238237
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 ) {
240253 // strip out our pseudo-html so we have a readable
241254 // version to put into text fields
242255 return ( _str || '' ) . replace ( STRIP_TAGS , ' ' ) ;
243256} ;
244257
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+
245273function 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 ) ;
252275}
253276
254277function convertToSVG ( _str ) {
278+ _str = convertEntities ( _str ) ;
279+
255280 var result = _str
256281 . split ( / ( < [ ^ < > ] * > ) / ) . map ( function ( d ) {
257282 var match = d . match ( / < ( \/ ? ) ( [ ^ > ] * ) \s * ( .* ) > / i) ,
@@ -270,6 +295,7 @@ function convertToSVG(_str) {
270295 * resurrect it.
271296 */
272297 extraStyle = extra . match ( / ^ s t y l e \s * = \s * " ( [ ^ " ] + ) " \s * / i) ;
298+
273299 // anchor and br are the only ones that don't turn into a tspan
274300 if ( tag === 'a' ) {
275301 if ( close ) return '</a>' ;
@@ -316,7 +342,7 @@ function convertToSVG(_str) {
316342 }
317343 }
318344 else {
319- return Plotly . util . xml_entity_encode ( d ) . replace ( / < / g, '<' ) ;
345+ return exports . xml_entity_encode ( d ) . replace ( / < / g, '<' ) ;
320346 }
321347 } ) ;
322348
@@ -397,7 +423,7 @@ function alignHTMLWith(_base, container, options) {
397423
398424// Editable title
399425
400- util . makeEditable = function ( context , _delegate , options ) {
426+ exports . makeEditable = function ( context , _delegate , options ) {
401427 if ( ! options ) options = { } ;
402428 var that = this ;
403429 var dispatch = d3 . dispatch ( 'edit' , 'input' , 'cancel' ) ;
@@ -431,7 +457,7 @@ util.makeEditable = function(context, _delegate, options) {
431457 }
432458
433459 function appendEditable ( ) {
434- var plotDiv = d3 . select ( Plotly . Lib . getPlotDiv ( that . node ( ) ) ) ,
460+ var plotDiv = d3 . select ( Lib . getPlotDiv ( that . node ( ) ) ) ,
435461 container = plotDiv . select ( '.svg-container' ) ,
436462 div = container . append ( 'div' ) ;
437463 div . classed ( 'plugin-editable editable' , true )
0 commit comments