Skip to content

Commit 672e836

Browse files
rzhade3fletchto99lgarron
committed
Make setCSPTrustedTypesPolicy static function
Co-authored-by: Matt Langlois <[email protected]> Co-authored-by: Lucas Garron <[email protected]>
1 parent 244e73c commit 672e836

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

src/index.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,13 @@ interface CSPTrustedHTMLToStringable {
2020
toString: () => string
2121
}
2222
let cspTrustedTypesPolicyPromise: Promise<CSPTrustedTypesPolicy> | null = null
23-
// Passing `null` clears the policy.
24-
export function setCSPTrustedTypesPolicy(policy: CSPTrustedTypesPolicy | Promise<CSPTrustedTypesPolicy> | null): void {
25-
cspTrustedTypesPolicyPromise = policy === null ? policy : Promise.resolve(policy)
26-
}
2723

2824
export default class IncludeFragmentElement extends HTMLElement {
25+
// Passing `null` clears the policy.
26+
static setCSPTrustedTypesPolicy(policy: CSPTrustedTypesPolicy | Promise<CSPTrustedTypesPolicy> | null): void {
27+
cspTrustedTypesPolicyPromise = policy === null ? policy : Promise.resolve(policy)
28+
}
29+
2930
static get observedAttributes(): string[] {
3031
return ['src', 'loading']
3132
}
@@ -62,7 +63,8 @@ export default class IncludeFragmentElement extends HTMLElement {
6263
this.setAttribute('accept', val)
6364
}
6465

65-
// TODO: Should this return a TrustedHTML if available, or always a string?
66+
// We will return string or error for API backwards compatibility. We can consider
67+
// returning TrustedHTML in the future.
6668
get data(): Promise<string | Error> {
6769
return this.#getStringOrErrorData()
6870
}

test/test.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {assert} from '@open-wc/testing'
2-
import {setCSPTrustedTypesPolicy} from '../src/index.ts'
2+
import {default as IncludeFragmentElement} from '../src/index.ts'
33

44
let count
55
const responses = {
@@ -648,12 +648,12 @@ suite('include-fragment-element', function () {
648648

649649
suite('CSP trusted types', () => {
650650
teardown(() => {
651-
setCSPTrustedTypesPolicy(null)
651+
IncludeFragmentElement.setCSPTrustedTypesPolicy(null)
652652
})
653653

654654
test('can set a pass-through mock CSP trusted types policy', async function () {
655655
let policyCalled = false
656-
setCSPTrustedTypesPolicy({
656+
IncludeFragmentElement.setCSPTrustedTypesPolicy({
657657
createHTML: htmlText => {
658658
policyCalled = true
659659
return htmlText
@@ -670,7 +670,7 @@ suite('include-fragment-element', function () {
670670

671671
test('can set and clear a mutating mock CSP trusted types policy', async function () {
672672
let policyCalled = false
673-
setCSPTrustedTypesPolicy({
673+
IncludeFragmentElement.setCSPTrustedTypesPolicy({
674674
createHTML: () => {
675675
policyCalled = true
676676
return '<b>replacement</b>'
@@ -683,7 +683,7 @@ suite('include-fragment-element', function () {
683683
assert.equal('<b>replacement</b>', data)
684684
assert.ok(policyCalled)
685685

686-
setCSPTrustedTypesPolicy(null)
686+
IncludeFragmentElement.setCSPTrustedTypesPolicy(null)
687687
const el2 = document.createElement('include-fragment')
688688
el2.src = '/hello'
689689
const data2 = await el2.data
@@ -699,7 +699,7 @@ suite('include-fragment-element', function () {
699699
return htmlText
700700
}
701701
})
702-
setCSPTrustedTypesPolicy(policy)
702+
IncludeFragmentElement.setCSPTrustedTypesPolicy(policy)
703703

704704
const el = document.createElement('include-fragment')
705705
el.src = '/hello'
@@ -709,7 +709,7 @@ suite('include-fragment-element', function () {
709709
})
710710

711711
test('can reject data using a mock CSP trusted types policy', async function () {
712-
setCSPTrustedTypesPolicy({
712+
IncludeFragmentElement.setCSPTrustedTypesPolicy({
713713
createHTML: () => {
714714
throw new Error('Rejected data!')
715715
}
@@ -726,7 +726,7 @@ suite('include-fragment-element', function () {
726726
})
727727

728728
test('can access headers using a mock CSP trusted types policy', async function () {
729-
setCSPTrustedTypesPolicy({
729+
IncludeFragmentElement.setCSPTrustedTypesPolicy({
730730
createHTML: (htmlText, response) => {
731731
if (response.headers.get('X-Server-Sanitized') !== 'sanitized=true') {
732732
// Note: this will reject the contents, but the error may be caught before it shows in the JS console.

0 commit comments

Comments
 (0)