Skip to content

Commit 61761e6

Browse files
author
Rafal Kukawski
committed
Added support for double_encode param
1 parent 958bbe9 commit 61761e6

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

functions/strings/htmlentities.js

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function htmlentities (string, quote_style) {
1+
function htmlentities (string, quote_style, charset, double_encode) {
22
// http://kevin.vanzonneveld.net
33
// + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
44
// + revised by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
@@ -8,25 +8,34 @@ function htmlentities (string, quote_style) {
88
// + revised by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
99
// + bugfixed by: Brett Zamir (http://brett-zamir.me)
1010
// + input by: Ratheous
11+
// + improved by: Rafał Kukawski (http://blog.kukawski.pl)
1112
// - depends on: get_html_translation_table
1213
// * example 1: htmlentities('Kevin & van Zonneveld');
1314
// * returns 1: 'Kevin & van Zonneveld'
1415
// * example 2: htmlentities("foo'bar","ENT_QUOTES");
1516
// * returns 2: 'foo'bar'
1617
var hash_map = {},
1718
symbol = '',
18-
tmp_str = '',
19-
entity = '';
20-
tmp_str = string.toString();
19+
entity = '',
20+
self = this;
21+
string += '';
22+
double_encode = !!double_encode || double_encode == null;
2123

2224
if (false === (hash_map = this.get_html_translation_table('HTML_ENTITIES', quote_style))) {
2325
return false;
2426
}
2527
hash_map["'"] = ''';
26-
for (symbol in hash_map) {
27-
entity = hash_map[symbol];
28-
tmp_str = tmp_str.split(symbol).join(entity);
28+
29+
if (double_encode) {
30+
for (symbol in hash_map) {
31+
entity = hash_map[symbol];
32+
string = string.split(symbol).join(entity);
33+
}
34+
} else {
35+
string = string.replace(/([\s\S]*?)(&(?:#\d+|#x[\da-f]+|[a-z][\da-z]*);|$)/g, function (ignore, text, entity) {
36+
return self.htmlentities(text, quote_style, charset) + entity;
37+
});
2938
}
3039

31-
return tmp_str;
40+
return string;
3241
}

0 commit comments

Comments
 (0)