1
1
function htmlentities ( string , quote_style , charset , double_encode ) {
2
- // discuss at: http://phpjs.org/functions/htmlentities/
2
+ // discuss at: http://phpjs.org/functions/htmlentities/
3
3
// original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
4
- // revised by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
5
- // revised by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
4
+ // revised by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
5
+ // revised by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
6
6
// improved by: nobbler
7
7
// improved by: Jack
8
8
// improved by: Rafał Kukawski (http://blog.kukawski.pl)
9
9
// improved by: Dj (http://phpjs.org/functions/htmlentities:425#comment_134018)
10
10
// bugfixed by: Onno Marsman
11
11
// bugfixed by: Brett Zamir (http://brett-zamir.me)
12
- // input by: Ratheous
13
- // depends on: get_html_translation_table
14
- // example 1: htmlentities('Kevin & van Zonneveld');
15
- // returns 1: 'Kevin & van Zonneveld'
16
- // example 2: htmlentities("foo'bar","ENT_QUOTES");
17
- // returns 2: ' foo'bar'
18
-
12
+ // input by: Ratheous
13
+ // depends on: get_html_translation_table
14
+ // note: function is compatible with PHP 5.2 and older
15
+ // example 1: htmlentities( 'Kevin & van Zonneveld');
16
+ // returns 1: 'Kevin & van Zonneveld'
17
+ // example 2: htmlentities(" foo'bar","ENT_QUOTES");
18
+ // returns 2: 'foo'bar'
19
19
var hash_map = this . get_html_translation_table ( 'HTML_ENTITIES' , quote_style ) ,
20
- symbol = '' ;
20
+ symbol = '' ;
21
+
21
22
string = string == null ? '' : string + '' ;
22
23
23
24
if ( ! hash_map ) {
@@ -28,25 +29,21 @@ function htmlentities(string, quote_style, charset, double_encode) {
28
29
hash_map [ "'" ] = ''' ;
29
30
}
30
31
31
- if ( ! ! double_encode || double_encode == null ) {
32
- for ( symbol in hash_map ) {
33
- if ( hash_map . hasOwnProperty ( symbol ) ) {
34
- string = string . split ( symbol )
35
- . join ( hash_map [ symbol ] ) ;
36
- }
37
- }
38
- } else {
39
- string = string . replace ( / ( [ \s \S ] * ?) ( & (?: # \d + | # x [ \d a - f ] + | [ a - z A - Z ] [ \d a - z ] * ) ; | $ ) / g, function ( ignore , text , entity ) {
40
- for ( symbol in hash_map ) {
41
- if ( hash_map . hasOwnProperty ( symbol ) ) {
42
- text = text . split ( symbol )
43
- . join ( hash_map [ symbol ] ) ;
44
- }
45
- }
32
+ double_encode = double_encode == null || ! ! double_encode ;
46
33
47
- return text + entity ;
48
- } ) ;
49
- }
34
+ var regex = new RegExp ( "&(?:#\\d+|#x[\\da-f]+|[a-zA-Z][\\da-z]*);|[" +
35
+ Object . keys ( hash_map )
36
+ . join ( "" )
37
+ // replace regexp special chars
38
+ . replace ( / ( [ ( ) [ \] { } \- . * + ? ^ $ | \/ \\ ] ) / g, "\\$1" )
39
+ + "]" ,
40
+ "g" ) ;
41
+
42
+ return string . replace ( regex , function ( ent ) {
43
+ if ( ent . length > 1 ) {
44
+ return double_encode ? hash_map [ "&" ] + ent . substr ( 1 ) : ent ;
45
+ }
50
46
51
- return string ;
52
- }
47
+ return hash_map [ ent ] ;
48
+ } ) ;
49
+ }
0 commit comments