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

Commit 56372a8

Browse files
committed
refactor(ngSanatize) Remove workarounds for IE8
Also provides better tests.
1 parent 1a7e9de commit 56372a8

File tree

2 files changed

+11
-49
lines changed

2 files changed

+11
-49
lines changed

src/ngSanitize/sanitize.js

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,6 @@ function htmlParser(html, handler) {
410410
}
411411

412412
var hiddenPre=document.createElement("pre");
413-
var spaceRe = /^(\s*)([\s\S]*?)(\s*)$/;
414413
/**
415414
* decodes all entities into regular string
416415
* @param value
@@ -419,22 +418,10 @@ var spaceRe = /^(\s*)([\s\S]*?)(\s*)$/;
419418
function decodeEntities(value) {
420419
if (!value) { return ''; }
421420

422-
// Note: IE8 does not preserve spaces at the start/end of innerHTML
423-
// so we must capture them and reattach them afterward
424-
var parts = spaceRe.exec(value);
425-
var spaceBefore = parts[1];
426-
var spaceAfter = parts[3];
427-
var content = parts[2];
428-
if (content) {
429-
hiddenPre.innerHTML=content.replace(/</g,"&lt;");
430-
// innerText depends on styling as it doesn't display hidden elements.
431-
// Therefore, it's better to use textContent not to cause unnecessary
432-
// reflows. However, IE<9 don't support textContent so the innerText
433-
// fallback is necessary.
434-
content = 'textContent' in hiddenPre ?
435-
hiddenPre.textContent : hiddenPre.innerText;
436-
}
437-
return spaceBefore + content + spaceAfter;
421+
hiddenPre.innerHTML = value.replace(/</g,"&lt;");
422+
// innerText depends on styling as it doesn't display hidden elements.
423+
// Therefore, it's better to use textContent not to cause unnecessary reflows.
424+
return hiddenPre.innerText;
438425
}
439426

440427
/**

test/ngSanitize/sanitizeSpec.js

Lines changed: 7 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -487,8 +487,7 @@ describe('HTML', function() {
487487
});
488488

489489
describe('decodeEntities', function() {
490-
var handler, text,
491-
origHiddenPre = window.hiddenPre;
490+
var handler, text;
492491

493492
beforeEach(function() {
494493
text = '';
@@ -503,37 +502,13 @@ describe('decodeEntities', function() {
503502
module('ngSanitize');
504503
});
505504

506-
afterEach(function() {
507-
window.hiddenPre = origHiddenPre;
505+
it('should unescape text', function() {
506+
htmlParser('a&lt;div&gt;&amp;&lt;/div&gt;c', handler)
507+
expect(text).toEqual('a<div>&</div>c');
508508
});
509509

510-
it('should use innerText if textContent is not available (IE<9)', function() {
511-
window.hiddenPre = {
512-
innerText: 'INNER_TEXT'
513-
};
514-
inject(function($sanitize) {
515-
htmlParser('<tag>text</tag>', handler);
516-
expect(text).toEqual('INNER_TEXT');
517-
});
518-
});
519-
it('should use textContent if available', function() {
520-
window.hiddenPre = {
521-
textContent: 'TEXT_CONTENT',
522-
innerText: 'INNER_TEXT'
523-
};
524-
inject(function($sanitize) {
525-
htmlParser('<tag>text</tag>', handler);
526-
expect(text).toEqual('TEXT_CONTENT');
527-
});
528-
});
529-
it('should use textContent even if empty', function() {
530-
window.hiddenPre = {
531-
textContent: '',
532-
innerText: 'INNER_TEXT'
533-
};
534-
inject(function($sanitize) {
535-
htmlParser('<tag>text</tag>', handler);
536-
expect(text).toEqual('');
537-
});
510+
it('should preserve whitespace', function() {
511+
htmlParser(' a&amp;b ', handler)
512+
expect(text).toEqual(' a&b ');
538513
});
539514
});

0 commit comments

Comments
 (0)