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

Commit bf1972d

Browse files
mgoltbosch
authored andcommittedDec 3, 2013
fix(ngSanitize): prefer textContent to innerText to avoid layout trashing
innerText depends on styling as it doesn't display hidden elements. Therefore, it's better to use textContent not to cause unnecessary reflows. However, IE<9 don't support textContent so the innerText fallback is necessary.
1 parent 689dfb1 commit bf1972d

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed
 

‎src/ngSanitize/sanitize.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,12 @@ function decodeEntities(value) {
378378
var content = parts[2];
379379
if (content) {
380380
hiddenPre.innerHTML=content.replace(/</g,"&lt;");
381-
content = hiddenPre.innerText || hiddenPre.textContent;
381+
// innerText depends on styling as it doesn't display hidden elements.
382+
// Therefore, it's better to use textContent not to cause unnecessary
383+
// reflows. However, IE<9 don't support textContent so the innerText
384+
// fallback is necessary.
385+
content = 'textContent' in hiddenPre ?
386+
hiddenPre.textContent : hiddenPre.innerText;
382387
}
383388
return spaceBefore + content + spaceAfter;
384389
}

‎test/ngSanitize/sanitizeSpec.js

+52
Original file line numberDiff line numberDiff line change
@@ -411,3 +411,55 @@ describe('HTML', function() {
411411
});
412412
});
413413
});
414+
415+
describe('decodeEntities', function() {
416+
var handler, text,
417+
origHiddenPre = window.hiddenPre;
418+
419+
beforeEach(function() {
420+
text = '';
421+
handler = {
422+
start: function() {},
423+
chars: function(text_){
424+
text = text_;
425+
},
426+
end: function() {},
427+
comment: function() {}
428+
};
429+
module('ngSanitize');
430+
});
431+
432+
afterEach(function() {
433+
window.hiddenPre = origHiddenPre;
434+
});
435+
436+
it('should use innerText if textContent is not available (IE<9)', function() {
437+
window.hiddenPre = {
438+
innerText: 'INNER_TEXT'
439+
};
440+
inject(function($sanitize) {
441+
htmlParser('<tag>text</tag>', handler);
442+
expect(text).toEqual('INNER_TEXT');
443+
});
444+
});
445+
it('should use textContent if available', function() {
446+
window.hiddenPre = {
447+
textContent: 'TEXT_CONTENT',
448+
innerText: 'INNER_TEXT'
449+
};
450+
inject(function($sanitize) {
451+
htmlParser('<tag>text</tag>', handler);
452+
expect(text).toEqual('TEXT_CONTENT');
453+
});
454+
});
455+
it('should use textContent even if empty', function() {
456+
window.hiddenPre = {
457+
textContent: '',
458+
innerText: 'INNER_TEXT'
459+
};
460+
inject(function($sanitize) {
461+
htmlParser('<tag>text</tag>', handler);
462+
expect(text).toEqual('');
463+
});
464+
});
465+
});

0 commit comments

Comments
 (0)
This repository has been archived.