Skip to content

Commit

Permalink
Hoisted decode/encode regexps mentioned in #11537.
Browse files Browse the repository at this point in the history
  • Loading branch information
mlewand committed Mar 3, 2014
1 parent b31c91e commit aa35091
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions core/tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,22 @@
*/

( function() {

var functions = [],
cssVendorPrefix =
CKEDITOR.env.gecko ? '-moz-' :
CKEDITOR.env.webkit ? '-webkit-' :
CKEDITOR.env.ie ? '-ms-' :
'';
'',
_ampRegex= /&/g,
_gtRegex= />/g,
_ltRegex= /</g,
_quoteRegex= /"/g,

_ampEscRegex= /&amp;/g,
_gtEscRegex= /&gt;/g,
_ltEscRegex= /&lt;/g,
_quoteEscRegex= /&quot;/g;

CKEDITOR.on( 'reset', function() {
functions = [];
Expand Down Expand Up @@ -321,7 +331,7 @@
* @returns {String} The encoded string.
*/
htmlEncode: function( text ) {
return String( text ).replace( /&/g, '&amp;' ).replace( />/g, '&gt;' ).replace( /</g, '&lt;' );
return String( text ).replace( _ampRegex, '&amp;' ).replace( _gtRegex, '&gt;' ).replace( _ltRegex, '&lt;' );
},

/**
Expand All @@ -333,7 +343,7 @@
* @returns {String} The decoded string.
*/
htmlDecode: function( text ) {
return text.replace( /&amp;/g, '&' ).replace( /&gt;/g, '>' ).replace( /&lt;/g, '<' );
return text.replace( _ampEscRegex, '&' ).replace( _gtEscRegex, '>' ).replace( _ltEscRegex, '<' );
},

/**
Expand All @@ -345,7 +355,7 @@
* @returns {String} The encoded value.
*/
htmlEncodeAttr: function( text ) {
return text.replace( /"/g, '&quot;' ).replace( /</g, '&lt;' ).replace( />/g, '&gt;' );
return text.replace( _quoteRegex, '&quot;' ).replace( _ltRegex, '&lt;' ).replace( _gtRegex, '&gt;' );
},

/**
Expand All @@ -359,7 +369,7 @@
* @returns {String} The decoded text.
*/
htmlDecodeAttr: function( text ) {
return text.replace( /&quot;/g, '"' ).replace( /&lt;/g, '<' ).replace( /&gt;/g, '>' );
return text.replace( _quoteEscRegex, '"' ).replace( _ltEscRegex, '<' ).replace( _gtEscRegex, '>' );
},

/**
Expand Down

0 comments on commit aa35091

Please sign in to comment.