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

Commit 81b8185

Browse files
fix($sanitize): don't rely on YARR regex engine executing immediately
In Safari 7 (and other browsers potentially using the latest YARR JIT library) regular expressions are not always executed immediately that they are called. The regex is only evaluated (lazily) when you first access properties on the `matches` result object returned from the regex call. In the case of `decodeEntities()`, we were updating this returned object, `parts[0] = ''`, before accessing it, `if (parts[2])', and so our change was overwritten by the result of executing the regex. The solution here is not to modify the match result object at all. We only need to make use of the three match results directly in code. Developers should be aware, in the future, when using regex, to read from the result object before making modifications to it. There is no additional test committed here, because when run against Safari 7, this bug caused numerous specs to fail, which are all fixed by this commit. Closes #5193 Closes #5192
1 parent fd4b999 commit 81b8185

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

src/ngSanitize/sanitize.js

+12-10
Original file line numberDiff line numberDiff line change
@@ -360,25 +360,27 @@ function htmlParser( html, handler ) {
360360
}
361361
}
362362

363+
var hiddenPre=document.createElement("pre");
364+
var spaceRe = /^(\s*)([\s\S]*?)(\s*)$/;
363365
/**
364366
* decodes all entities into regular string
365367
* @param value
366368
* @returns {string} A string with decoded entities.
367369
*/
368-
var hiddenPre=document.createElement("pre");
369370
function decodeEntities(value) {
370-
if (!value) {
371-
return '';
372-
}
371+
if (!value) { return ''; }
372+
373373
// Note: IE8 does not preserve spaces at the start/end of innerHTML
374-
var spaceRe = /^(\s*)([\s\S]*?)(\s*)$/;
374+
// so we must capture them and reattach them afterward
375375
var parts = spaceRe.exec(value);
376-
parts[0] = '';
377-
if (parts[2]) {
378-
hiddenPre.innerHTML=parts[2].replace(/</g,"&lt;");
379-
parts[2] = hiddenPre.innerText || hiddenPre.textContent;
376+
var spaceBefore = parts[1];
377+
var spaceAfter = parts[3];
378+
var content = parts[2];
379+
if (content) {
380+
hiddenPre.innerHTML=content.replace(/</g,"&lt;");
381+
content = hiddenPre.innerText || hiddenPre.textContent;
380382
}
381-
return parts.join('');
383+
return spaceBefore + content + spaceAfter;
382384
}
383385

384386
/**

0 commit comments

Comments
 (0)