@@ -15,6 +15,7 @@ var d3 = require('d3');
1515
1616var Lib = require ( '../lib' ) ;
1717var xmlnsNamespaces = require ( '../constants/xmlns_namespaces' ) ;
18+ var stringMappings = require ( '../constants/string_mappings' ) ;
1819
1920// Append SVG
2021
@@ -67,6 +68,7 @@ exports.convertToTspans = function(_context, _callback) {
6768 var str = _context . text ( ) ;
6869 var converted = convertToSVG ( str ) ;
6970 var that = _context ;
71+
7072 // Until we get tex integrated more fully (so it can be used along with non-tex)
7173 // allow some elements to prohibit it by attaching 'data-notex' to the original
7274 var tex = ( ! that . attr ( 'data-notex' ) ) && converted . match ( / ( [ ^ $ ] * ) ( [ $ ] + [ ^ $ ] * [ $ ] + ) ( [ ^ $ ] * ) / ) ;
@@ -233,22 +235,48 @@ var PROTOCOLS = ['http:', 'https:', 'mailto:'];
233235
234236var STRIP_TAGS = new RegExp ( '</?(' + Object . keys ( TAG_STYLES ) . join ( '|' ) + ')( [^>]*)?/?>' , 'g' ) ;
235237
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+
236252exports . plainText = function ( _str ) {
237253 // strip out our pseudo-html so we have a readable
238254 // version to put into text fields
239255 return ( _str || '' ) . replace ( STRIP_TAGS , ' ' ) ;
240256} ;
241257
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+
242273function encodeForHTML ( _str ) {
243- return ( _str || '' ) . replace ( / & / g, '&' )
244- . replace ( / < / g, '<' )
245- . replace ( / > / g, '>' )
246- . replace ( / " / g, '"' )
247- . replace ( / ' / g, ''' )
248- . replace ( / \/ / g, '/' ) ;
274+ return replaceFromMapObject ( _str , UNICODE_TO_ENTITY ) ;
249275}
250276
251277function convertToSVG ( _str ) {
278+ _str = convertEntities ( _str ) ;
279+
252280 var result = _str
253281 . split ( / ( < [ ^ < > ] * > ) / ) . map ( function ( d ) {
254282 var match = d . match ( / < ( \/ ? ) ( [ ^ > ] * ) \s * ( .* ) > / i) ,
@@ -267,6 +295,7 @@ function convertToSVG(_str) {
267295 * resurrect it.
268296 */
269297 extraStyle = extra . match ( / ^ s t y l e \s * = \s * " ( [ ^ " ] + ) " \s * / i) ;
298+
270299 // anchor and br are the only ones that don't turn into a tspan
271300 if ( tag === 'a' ) {
272301 if ( close ) return '</a>' ;
0 commit comments