@@ -9,31 +9,35 @@ function htmlentities (string, quote_style, charset, double_encode) {
99 // + bugfixed by: Brett Zamir (http://brett-zamir.me)
1010 // + input by: Ratheous
1111 // + improved by: Rafał Kukawski (http://blog.kukawski.pl)
12+ // + improved by: Dj (http://phpjs.org/functions/htmlentities:425#comment_134018)
1213 // - depends on: get_html_translation_table
1314 // * example 1: htmlentities('Kevin & van Zonneveld');
1415 // * returns 1: 'Kevin & van Zonneveld'
1516 // * example 2: htmlentities("foo'bar","ENT_QUOTES");
1617 // * returns 2: 'foo'bar'
17- var hash_map = { } ,
18- symbol = '' ,
19- entity = '' ,
20- self = this ;
21- string += '' ;
22- double_encode = ! ! double_encode || double_encode == null ;
18+ var hash_map = this . get_html_translation_table ( 'HTML_ENTITIES' , quote_style ) ,
19+ symbol = '' ;
20+ string = string == null ? '' : string + '' ;
2321
24- if ( false === ( hash_map = this . get_html_translation_table ( 'HTML_ENTITIES' , quote_style ) ) ) {
22+ if ( ! hash_map ) {
2523 return false ;
2624 }
27- hash_map [ "'" ] = ''' ;
2825
29- if ( double_encode ) {
26+ if ( quote_style && quote_style === 'ENT_QUOTES' ) {
27+ hash_map [ "'" ] = ''' ;
28+ }
29+
30+ if ( ! ! double_encode || double_encode == null ) {
3031 for ( symbol in hash_map ) {
31- entity = hash_map [ symbol ] ;
32- string = string . split ( symbol ) . join ( entity ) ;
32+ string = string . split ( symbol ) . join ( hash_map [ symbol ] ) ;
3333 }
3434 } else {
35- string = string . replace ( / ( [ \s \S ] * ?) ( & (?: # \d + | # x [ \d a - f ] + | [ a - z ] [ \d a - z ] * ) ; | $ ) / g, function ( ignore , text , entity ) {
36- return self . htmlentities ( text , quote_style , charset ) + entity ;
35+ string = string . replace ( / ( [ \s \S ] * ?) ( & (?: # \d + | # x [ \d a - f ] + | [ a - z A - Z ] [ \d a - z ] * ) ; | $ ) / g, function ( ignore , text , entity ) {
36+ for ( symbol in hash_map ) {
37+ text = text . split ( symbol ) . join ( hash_map [ symbol ] ) ;
38+ }
39+
40+ return text + entity ;
3741 } ) ;
3842 }
3943
0 commit comments