* Returns the Trusted Types policy, or null if Trusted Types are not * enabled/supported. The first call to this function will create the policy.
()
| 40 | * enabled/supported. The first call to this function will create the policy. |
| 41 | */ |
| 42 | function getPolicy(): TrustedTypePolicy | null { |
| 43 | if (policy === undefined) { |
| 44 | policy = null; |
| 45 | if (typeof window !== 'undefined') { |
| 46 | const ttWindow = window as unknown as {trustedTypes?: TrustedTypePolicyFactory}; |
| 47 | if (ttWindow.trustedTypes !== undefined) { |
| 48 | try { |
| 49 | policy = ttWindow.trustedTypes.createPolicy('angular#components', { |
| 50 | createHTML: (s: string) => s, |
| 51 | }); |
| 52 | } catch (error) { |
| 53 | // createPolicy can throw if the name is already registered, or if |
| 54 | // window.trustedTypes was DOM-clobbered with an HTML element before |
| 55 | // Angular bootstrapped. trustedHTMLFromString falls back to plain |
| 56 | // strings — sanitization in _setInnerHtml still runs. |
| 57 | console.error(error); |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | return policy; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Unsafely promote a string to a TrustedHTML, falling back to strings when |
no test coverage detected