-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from rzhade3/main
Add Trusted Types
- Loading branch information
Showing
8 changed files
with
112 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
import {directive} from './directive.js' | ||
import {NodeTemplatePart} from '@github/template-parts' | ||
import type {TemplatePart} from '@github/template-parts' | ||
import {TemplateResult} from './template-result.js' | ||
|
||
export const unsafeHTML = directive((value: string) => (part: TemplatePart) => { | ||
if (!(part instanceof NodeTemplatePart)) return | ||
const template = document.createElement('template') | ||
template.innerHTML = value | ||
const trustedValue = (TemplateResult.cspTrustedTypesPolicy?.createHTML(value) as string | undefined) ?? value | ||
template.innerHTML = trustedValue | ||
const fragment = document.importNode(template.content, true) | ||
part.replace(...fragment.childNodes) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import {expect} from 'chai' | ||
import {TemplateResult} from '../lib/index.js' | ||
|
||
describe('trusted types', () => { | ||
after(() => { | ||
TemplateResult.setCSPTrustedTypesPolicy(null) | ||
}) | ||
|
||
it('can set a CSP Trusted Types policy', () => { | ||
const dummyPolicy = { | ||
createHTML: (htmlText: string) => { | ||
return htmlText | ||
} | ||
} | ||
expect(TemplateResult.cspTrustedTypesPolicy).to.equal(null) | ||
TemplateResult.setCSPTrustedTypesPolicy(dummyPolicy) | ||
expect(TemplateResult.cspTrustedTypesPolicy).to.equal(dummyPolicy) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters