From c509243af58e64293b16baf518494d9ca508d9e2 Mon Sep 17 00:00:00 2001 From: Harri Lehtola Date: Sat, 25 Apr 2020 14:39:54 +0300 Subject: [PATCH] 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 --- packages/core/src/sanitization/inert_body.ts | 10 +++++----- .../test/sanitization/html_sanitizer_spec.ts | 16 +--------------- 2 files changed, 6 insertions(+), 20 deletions(-) diff --git a/packages/core/src/sanitization/inert_body.ts b/packages/core/src/sanitization/inert_body.ts index 9e3530be71..62c6598df8 100644 --- a/packages/core/src/sanitization/inert_body.ts +++ b/packages/core/src/sanitization/inert_body.ts @@ -120,15 +120,15 @@ class InertDocumentHelper implements InertBodyHelper { } /** - * 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. + * We need to determine whether the DOMParser exists in the global context and + * supports parsing HTML; HTML parsing support is not as wide as other formats, see + * https://developer.mozilla.org/en-US/docs/Web/API/DOMParser#Browser_compatibility. * * @suppress {uselessCode} */ -function isDOMParserAvailable() { +export function isDOMParserAvailable() { try { - return !!(window as any).DOMParser; + return !!new (window as any).DOMParser().parseFromString('', 'text/html'); } catch { return false; } diff --git a/packages/core/test/sanitization/html_sanitizer_spec.ts b/packages/core/test/sanitization/html_sanitizer_spec.ts index 440ffb8bb0..dc83ca31f5 100644 --- a/packages/core/test/sanitization/html_sanitizer_spec.ts +++ b/packages/core/test/sanitization/html_sanitizer_spec.ts @@ -9,6 +9,7 @@ import {browserDetection} from '@angular/platform-browser/testing/src/browser_util'; import {_sanitizeHtml} from '../../src/sanitization/html_sanitizer'; +import {isDOMParserAvailable} from '../../src/sanitization/inert_body'; { 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; - } -}