This repository was archived by the owner on Jan 9, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Add Trusted Types #26
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
29fb463
Add concept of TrustedTypes policy
rzhade3 5269718
Testing that one can set a global TT policy
rzhade3 c88a7a6
Update to use variable instead of class
rzhade3 e47453e
Add tests
rzhade3 0e40021
Update docs
rzhade3 1e02b01
Move setCspTrustedPolicy into TemplateResult class
rzhade3 186f094
More concise JS syntax
rzhade3 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.