fix(core): determine required DOMParser feature availability (#36578) (#36578)

Verify that HTML parsing is supported in addition to DOMParser existence.
This maybe wasn't as important before when DOMParser was used just as a
fallback on Firefox, but now that DOMParser is the default choice, we need
to be more accurate.

PR Close #36578
This commit is contained in:
Harri Lehtola 2020-04-25 14:39:54 +03:00 committed by Andrew Kushnir
parent d4544da804
commit c509243af5
2 changed files with 6 additions and 20 deletions

View File

@ -120,15 +120,15 @@ class InertDocumentHelper implements InertBodyHelper {
} }
/** /**
* We need to determine whether the DOMParser exists in the global context. * We need to determine whether the DOMParser exists in the global context and
* The try-catch is because, on some browsers, trying to access this property * supports parsing HTML; HTML parsing support is not as wide as other formats, see
* on window can actually throw an error. * https://developer.mozilla.org/en-US/docs/Web/API/DOMParser#Browser_compatibility.
* *
* @suppress {uselessCode} * @suppress {uselessCode}
*/ */
function isDOMParserAvailable() { export function isDOMParserAvailable() {
try { try {
return !!(window as any).DOMParser; return !!new (window as any).DOMParser().parseFromString('', 'text/html');
} catch { } catch {
return false; return false;
} }

View File

@ -9,6 +9,7 @@
import {browserDetection} from '@angular/platform-browser/testing/src/browser_util'; import {browserDetection} from '@angular/platform-browser/testing/src/browser_util';
import {_sanitizeHtml} from '../../src/sanitization/html_sanitizer'; import {_sanitizeHtml} from '../../src/sanitization/html_sanitizer';
import {isDOMParserAvailable} from '../../src/sanitization/inert_body';
{ {
describe('HTML sanitizer', () => { describe('HTML sanitizer', () => {
@ -229,18 +230,3 @@ import {_sanitizeHtml} from '../../src/sanitization/html_sanitizer';
} }
}); });
} }
/**
* We need to determine whether the DOMParser exists in the global context.
* The try-catch is because, on some browsers, trying to access this property
* on window can actually throw an error.
*
* @suppress {uselessCode}
*/
function isDOMParserAvailable() {
try {
return !!(window as any).DOMParser;
} catch (e) {
return false;
}
}