Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

fix(ngSanitize): removed encoding of non alphanumeric on textContent #5819

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/ngSanitize/filter/linky.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@
<doc:scenario>
it('should linkify the snippet with urls', function() {
expect(using('#linky-filter').binding('snippet | linky')).
toBe('Pretty text with some links:&#10;' +
'<a href="http://angularjs.org/">http://angularjs.org/</a>,&#10;' +
'<a href="mailto:us@somewhere.org">us@somewhere.org</a>,&#10;' +
'<a href="mailto:another@somewhere.org">another@somewhere.org</a>,&#10;' +
toBe('Pretty text with some links:\n' +
'<a href="http://angularjs.org/">http://angularjs.org/</a>,\n' +
'<a href="mailto:us@somewhere.org">us@somewhere.org</a>,\n' +
'<a href="mailto:another@somewhere.org">another@somewhere.org</a>,\n' +
'and one more: <a href="ftp://127.0.0.1/">ftp://127.0.0.1/</a>.');
});

Expand Down
20 changes: 11 additions & 9 deletions src/ngSanitize/sanitize.js
Original file line number Diff line number Diff line change
Expand Up @@ -395,14 +395,16 @@ function decodeEntities(value) {
* @param value
* @returns escaped text
*/
function encodeEntities(value) {
return value.
replace(/&/g, '&amp;').
replace(NON_ALPHANUMERIC_REGEXP, function(value){
function encodeEntities(value, replace_non_alphanumeric) {
value = value.replace(/&/g, '&amp;');

if(replace_non_alphanumeric) {
value = value.replace(NON_ALPHANUMERIC_REGEXP, function(value){
return '&#' + value.charCodeAt(0) + ';';
}).
replace(/</g, '&lt;').
replace(/>/g, '&gt;');
});
}

return value.replace(/</g, '&lt;').replace(/>/g, '&gt;');
}

/**
Expand Down Expand Up @@ -435,7 +437,7 @@ function htmlSanitizeWriter(buf, uriValidator){
out(' ');
out(key);
out('="');
out(encodeEntities(value));
out(encodeEntities(value, true));
out('"');
}
});
Expand All @@ -455,7 +457,7 @@ function htmlSanitizeWriter(buf, uriValidator){
},
chars: function(chars){
if (!ignore) {
out(encodeEntities(chars));
out(encodeEntities(chars, false));
}
}
};
Expand Down
4 changes: 2 additions & 2 deletions test/ngSanitize/sanitizeSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ describe('HTML', function() {

it('should handle entities', function() {
var everything = '<div rel="!@#$%^&amp;*()_+-={}[]:&#34;;\'&lt;&gt;?,./`~ &#295;">' +
'!@#$%^&amp;*()_+-={}[]:&#34;;\'&lt;&gt;?,./`~ &#295;</div>';
'!@#$%^&amp;*()_+-={}[]:";\'&lt;&gt;?,./`~ ħ</div>';
expectHTML(everything).toEqual(everything);
});

Expand Down Expand Up @@ -191,7 +191,7 @@ describe('HTML', function() {
});

it('should allow multiline strings', function() {
expectHTML('\na\n').toEqual('&#10;a\&#10;');
expectHTML('\na\n').toEqual('\na\n');
});

describe('htmlSanitizerWriter', function() {
Expand Down