Skip to content

Commit

Permalink
Avoid using Trusted Types in value contexts
Browse files Browse the repository at this point in the history
In particular, replace instanceof checks with trustedTypes.is* checks and
typeof expressions with the types themselves.

This fixes a breakage caused by
DefinitelyTyped/DefinitelyTyped#64360, which removed
the ability to use Trusted Types in value contexts.

PiperOrigin-RevId: 510113784
  • Loading branch information
bjarkler authored and copybara-github committed Feb 16, 2023
1 parent 70074d7 commit cd56470
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/dom/elements/link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/

import {unwrapUrlOrSanitize, Url} from '../../builders/url_sanitizer';
import {TrustedResourceUrl, unwrapResourceUrl} from '../../internals/resource_url_impl';
import {isResourceUrl, TrustedResourceUrl, unwrapResourceUrl} from '../../internals/resource_url_impl';

const SAFE_URL_REL_VALUES = [
'alternate',
Expand Down Expand Up @@ -63,7 +63,7 @@ export function setHrefAndRel(

export function setHrefAndRel(
link: HTMLLinkElement, url: TrustedResourceUrl|Url, rel: string) {
if (url instanceof TrustedResourceUrl) {
if (isResourceUrl(url)) {
link.href = unwrapResourceUrl(url).toString();
} else {
if ((SAFE_URL_REL_VALUES as readonly string[]).indexOf(rel) === -1) {
Expand Down
4 changes: 2 additions & 2 deletions src/internals/html_impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export type SafeHtml = TrustedHTML;
* Also exports the constructor so that instanceof checks work.
*/
export const SafeHtml =
(GlobalTrustedHTML ?? HtmlImpl) as unknown as typeof TrustedHTML;
(GlobalTrustedHTML ?? HtmlImpl) as unknown as TrustedHTML;

/**
* Builds a new `SafeHtml` from the given string, without enforcing safety
Expand All @@ -70,7 +70,7 @@ export const EMPTY_HTML: SafeHtml =
* Checks if the given value is a `SafeHtml` instance.
*/
export function isHtml(value: unknown): value is SafeHtml {
return value instanceof SafeHtml;
return getTrustedTypes()?.isHTML(value) || value instanceof HtmlImpl;
}

/**
Expand Down
7 changes: 4 additions & 3 deletions src/internals/resource_url_impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ export type TrustedResourceUrl = TrustedScriptURL;
/**
* Also exports the constructor so that instanceof checks work.
*/
export const TrustedResourceUrl = (GlobalTrustedScriptURL ?? ResourceUrlImpl) as
unknown as typeof TrustedScriptURL;
export const TrustedResourceUrl =
(GlobalTrustedScriptURL ?? ResourceUrlImpl) as unknown as TrustedScriptURL;

/**
* Builds a new `TrustedResourceUrl` from the given string, without
Expand All @@ -61,7 +61,8 @@ export function createResourceUrl(url: string): TrustedResourceUrl {
* Checks if the given value is a `TrustedResourceUrl` instance.
*/
export function isResourceUrl(value: unknown): value is TrustedResourceUrl {
return value instanceof TrustedResourceUrl;
return getTrustedTypes()?.isScriptURL(value) ||
value instanceof ResourceUrlImpl;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/internals/script_impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export type SafeScript = TrustedScript;
* Also exports the constructor so that instanceof checks work.
*/
export const SafeScript =
(GlobalTrustedScript ?? ScriptImpl) as unknown as typeof TrustedScript;
(GlobalTrustedScript ?? ScriptImpl) as unknown as TrustedScript;

/**
* Builds a new `SafeScript` from the given string, without enforcing
Expand All @@ -72,7 +72,7 @@ export const EMPTY_SCRIPT: SafeScript =
* Checks if the given value is a `SafeScript` instance.
*/
export function isScript(value: unknown): value is SafeScript {
return value instanceof SafeScript;
return getTrustedTypes()?.isScript(value) || value instanceof ScriptImpl;
}

/**
Expand Down

0 comments on commit cd56470

Please sign in to comment.