From a04247853d94a498528441cbe4463c7c86496511 Mon Sep 17 00:00:00 2001 From: Krzysztof Kotowicz Date: Mon, 14 Oct 2019 19:16:11 +0200 Subject: [PATCH 1/2] Fixed a bug with illegal invocation. --- 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..1a0c97986e44 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: value => { + if (this !== window.trustedTypes) { + throw new Error('Illegal invocation'); + } + return fakeTTObjects.has(value); + }, isScript: () => false, isScriptURL: () => false, }; From a43d32b1e24fe4cb195386e549c1b5a56b188550 Mon Sep 17 00:00:00 2001 From: Krzysztof Kotowicz Date: Tue, 15 Oct 2019 09:44:51 +0200 Subject: [PATCH 2/2] Fixed the test. --- .../src/client/__tests__/trustedTypes-test.internal.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 1a0c97986e44..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,9 +22,9 @@ describe('when Trusted Types are available in global object', () => { container = document.createElement('div'); const fakeTTObjects = new Set(); window.trustedTypes = { - isHTML: value => { + isHTML: function(value) { if (this !== window.trustedTypes) { - throw new Error('Illegal invocation'); + throw new Error(this); } return fakeTTObjects.has(value); },