Skip to content

Commit 607d175

Browse files
shakyShaneShane Osbourne
andauthored
use trusted types where possible (#1011)
* use trusted types where possible * linting * add support for trusted types * remove from module scope --------- Co-authored-by: Shane Osbourne <[email protected]>
1 parent 4ffda90 commit 607d175

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

src/dom-utils.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,16 @@ function decodeHtml (html) {
8181
txt.innerHTML = html
8282
return txt.value
8383
}
84+
85+
/**
86+
* Use a policy if trustedTypes is available
87+
* @return {{createHTML: (s: string) => any}}
88+
*/
89+
export function createPolicy () {
90+
if (globalThis.trustedTypes) {
91+
return globalThis.trustedTypes?.createPolicy?.('ddg-default', { createHTML: (s) => s })
92+
}
93+
return {
94+
createHTML: (s) => s
95+
}
96+
}

src/features/duckplayer/components/ddg-video-overlay.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ import dax from '../assets/dax.svg'
33
import { overlayCopyVariants } from '../text.js'
44
import { appendImageAsBackground } from '../util.js'
55
import { VideoOverlay } from '../video-overlay.js'
6-
import { html, trustedUnsafe } from '../../../dom-utils.js'
6+
import { createPolicy, html, trustedUnsafe } from '../../../dom-utils.js'
77

88
/**
99
* The custom element that we use to present our UI elements
1010
* over the YouTube player
1111
*/
1212
export class DDGVideoOverlay extends HTMLElement {
13+
policy = createPolicy()
14+
1315
static CUSTOM_TAG_NAME = 'ddg-video-overlay'
1416
/**
1517
* @param {object} options
@@ -60,7 +62,7 @@ export class DDGVideoOverlay extends HTMLElement {
6062
const overlayElement = document.createElement('div')
6163
overlayElement.classList.add('ddg-video-player-overlay')
6264
const svgIcon = trustedUnsafe(dax)
63-
overlayElement.innerHTML = html`
65+
const safeString = html`
6466
<div class="ddg-vpo-bg"></div>
6567
<div class="ddg-vpo-content">
6668
<div class="ddg-eyeball">${svgIcon}</div>
@@ -79,6 +81,9 @@ export class DDGVideoOverlay extends HTMLElement {
7981
</div>
8082
</div>
8183
`.toString()
84+
85+
overlayElement.innerHTML = this.policy.createHTML(safeString)
86+
8287
/**
8388
* Set the link
8489
* @type {string}

src/features/duckplayer/icon-overlay.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ import css from './assets/styles.css'
22
import { SideEffects, VideoParams } from './util.js'
33
import dax from './assets/dax.svg'
44
import { i18n } from './text.js'
5-
import { html, trustedUnsafe } from '../../dom-utils.js'
5+
import { createPolicy, html, trustedUnsafe } from '../../dom-utils.js'
66

77
export class IconOverlay {
88
sideEffects = new SideEffects()
9+
policy = createPolicy()
910

1011
/** @type {HTMLElement | null} */
1112
element = null
@@ -36,7 +37,7 @@ export class IconOverlay {
3637
overlayElement.setAttribute('class', 'ddg-overlay' + (extraClass ? ' ' + extraClass : ''))
3738
overlayElement.setAttribute('data-size', size)
3839
const svgIcon = trustedUnsafe(dax)
39-
overlayElement.innerHTML = html`
40+
const safeString = html`
4041
<a class="ddg-play-privately" href="#">
4142
<div class="ddg-dax">
4243
${svgIcon}
@@ -48,6 +49,8 @@ export class IconOverlay {
4849
</div>
4950
</a>`.toString()
5051

52+
overlayElement.innerHTML = this.policy.createHTML(safeString)
53+
5154
overlayElement.querySelector('a.ddg-play-privately')?.setAttribute('href', href)
5255
return overlayElement
5356
}

0 commit comments

Comments
 (0)