From fdba0e5ce75f1cf1f82f2937f351bbfdcaadbcc5 Mon Sep 17 00:00:00 2001 From: Krzysztof Kotowicz Date: Tue, 15 Oct 2019 12:41:42 +0200 Subject: [PATCH] Fixed a bug with illegal invocation for Trusted Types (#17083) * Fixed a bug with illegal invocation. * Fixed the test. --- packages/react-dom/src/client/ToStringValue.js | 11 +++++------ .../client/__tests__/trustedTypes-test.internal.js | 7 ++++++- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/packages/react-dom/src/client/ToStringValue.js b/packages/react-dom/src/client/ToStringValue.js index cf4a173e1399..4844e0685dc4 100644 --- a/packages/react-dom/src/client/ToStringValue.js +++ b/packages/react-dom/src/client/ToStringValue.js @@ -53,15 +53,14 @@ export opaque type TrustedValue: {toString(): string, valueOf(): string} = { */ export let toStringOrTrustedType: any => string | TrustedValue = toString; if (enableTrustedTypesIntegration && typeof trustedTypes !== 'undefined') { - const isHTML = trustedTypes.isHTML; - const isScript = trustedTypes.isScript; - const isScriptURL = trustedTypes.isScriptURL; - // TrustedURLs are deprecated and will be removed soon: https://github.com/WICG/trusted-types/pull/204 - const isURL = trustedTypes.isURL ? trustedTypes.isURL : value => false; toStringOrTrustedType = value => { if ( typeof value === 'object' && - (isHTML(value) || isScript(value) || isScriptURL(value) || isURL(value)) + (trustedTypes.isHTML(value) || + trustedTypes.isScript(value) || + trustedTypes.isScriptURL(value) || + /* TrustedURLs are deprecated and will be removed soon: https://github.com/WICG/trusted-types/pull/204 */ + (trustedTypes.isURL && trustedTypes.isURL(value))) ) { // Pass Trusted Types through. return value; diff --git a/packages/react-dom/src/client/__tests__/trustedTypes-test.internal.js b/packages/react-dom/src/client/__tests__/trustedTypes-test.internal.js index b3511b43dc3a..53f5759157cc 100644 --- a/packages/react-dom/src/client/__tests__/trustedTypes-test.internal.js +++ b/packages/react-dom/src/client/__tests__/trustedTypes-test.internal.js @@ -22,7 +22,12 @@ describe('when Trusted Types are available in global object', () => { container = document.createElement('div'); const fakeTTObjects = new Set(); window.trustedTypes = { - isHTML: value => fakeTTObjects.has(value), + isHTML: function(value) { + if (this !== window.trustedTypes) { + throw new Error(this); + } + return fakeTTObjects.has(value); + }, isScript: () => false, isScriptURL: () => false, };