Fixed a bug with illegal invocation for Trusted Types (#17083)
* Fixed a bug with illegal invocation. * Fixed the test.
This commit is contained in:
parent
a8c6a1b34e
commit
fdba0e5ce7
|
@ -53,15 +53,14 @@ export opaque type TrustedValue: {toString(): string, valueOf(): string} = {
|
||||||
*/
|
*/
|
||||||
export let toStringOrTrustedType: any => string | TrustedValue = toString;
|
export let toStringOrTrustedType: any => string | TrustedValue = toString;
|
||||||
if (enableTrustedTypesIntegration && typeof trustedTypes !== 'undefined') {
|
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 => {
|
toStringOrTrustedType = value => {
|
||||||
if (
|
if (
|
||||||
typeof value === 'object' &&
|
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.
|
// Pass Trusted Types through.
|
||||||
return value;
|
return value;
|
||||||
|
|
|
@ -22,7 +22,12 @@ describe('when Trusted Types are available in global object', () => {
|
||||||
container = document.createElement('div');
|
container = document.createElement('div');
|
||||||
const fakeTTObjects = new Set();
|
const fakeTTObjects = new Set();
|
||||||
window.trustedTypes = {
|
window.trustedTypes = {
|
||||||
isHTML: value => fakeTTObjects.has(value),
|
isHTML: function(value) {
|
||||||
|
if (this !== window.trustedTypes) {
|
||||||
|
throw new Error(this);
|
||||||
|
}
|
||||||
|
return fakeTTObjects.has(value);
|
||||||
|
},
|
||||||
isScript: () => false,
|
isScript: () => false,
|
||||||
isScriptURL: () => false,
|
isScriptURL: () => false,
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue