Skip to content

Commit f37253f

Browse files
author
Rafal Kukawski
committed
Fixes issue locutusjs#206: Double encoding bug
1 parent 34b3353 commit f37253f

File tree

1 file changed

+28
-31
lines changed

1 file changed

+28
-31
lines changed

functions/strings/htmlentities.js

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
11
function htmlentities(string, quote_style, charset, double_encode) {
2-
// discuss at: http://phpjs.org/functions/htmlentities/
2+
// discuss at: http://phpjs.org/functions/htmlentities/
33
// 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)
66
// improved by: nobbler
77
// improved by: Jack
88
// improved by: Rafał Kukawski (http://blog.kukawski.pl)
99
// improved by: Dj (http://phpjs.org/functions/htmlentities:425#comment_134018)
1010
// bugfixed by: Onno Marsman
1111
// 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'
1919
var hash_map = this.get_html_translation_table('HTML_ENTITIES', quote_style),
20-
symbol = '';
20+
symbol = '';
21+
2122
string = string == null ? '' : string + '';
2223

2324
if (!hash_map) {
@@ -28,25 +29,21 @@ function htmlentities(string, quote_style, charset, double_encode) {
2829
hash_map["'"] = ''';
2930
}
3031

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[\da-f]+|[a-zA-Z][\da-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;
4633

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+
}
5046

51-
return string;
52-
}
47+
return hash_map[ent];
48+
});
49+
}

0 commit comments

Comments
 (0)