Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Jan 9, 2025. It is now read-only.
/jtmlPublic archive

Commit29fb463

Browse files
rzhade3fletchto99KyFaStJackMc
committed
Add concept of TrustedTypes policy
Co-authored-by: Matt Langlois <fletchto99@github.com>Co-authored-by: Kylie Stradley <kyfast@github.com>Co-authored-by: Jack McCracken <jackmc@github.com>
1 parent2208fa1 commit29fb463

File tree

4 files changed

+28
-2
lines changed

4 files changed

+28
-2
lines changed

‎src/index.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ export {html} from './html.js'
55
export{isDirective,directive}from'./directive.js'
66
export{until}from'./until.js'
77
export{unsafeHTML}from'./unsafe-html.js'
8+
export{TrustedTypesPolicy}from'./trusted-types.js'

‎src/template-result.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import{TemplateInstance,NodeTemplatePart}from'@github/template-parts'
22
importtype{TemplateTypeInit}from'@github/template-parts'
3+
import{TrustedTypesPolicy}from'./trusted-types.js'
34

45
consttemplates=newWeakMap<TemplateStringsArray,HTMLTemplateElement>()
56
constrenderedTemplates=newWeakMap<Node|NodeTemplatePart,HTMLTemplateElement>()
67
constrenderedTemplateInstances=newWeakMap<Node|NodeTemplatePart,TemplateInstance>()
8+
79
exportclassTemplateResult{
810
constructor(
911
publicreadonlystrings:TemplateStringsArray,
@@ -17,7 +19,11 @@ export class TemplateResult {
1719
}else{
1820
consttemplate=document.createElement('template')
1921
constend=this.strings.length-1
20-
template.innerHTML=this.strings.reduce((str,cur,i)=>str+cur+(i<end ?`{{${i} }}` :''),'')
22+
consthtml=this.strings.reduce((str,cur,i)=>str+cur+(i<end ?`{{${i} }}` :''),'')
23+
consttrustedHtml=TrustedTypesPolicy.cspTrustedTypesPolicy
24+
?(TrustedTypesPolicy.cspTrustedTypesPolicy.createHTML(html)asstring)
25+
:html
26+
template.innerHTML=trustedHtml
2127
templates.set(this.strings,template)
2228
returntemplate
2329
}

‎src/trusted-types.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
interfaceCSPTrustedHTMLToStringable{
2+
toString:()=>string
3+
}
4+
5+
interfaceCSPTrustedTypesPolicy{
6+
createHTML:(s:string)=>CSPTrustedHTMLToStringable
7+
}
8+
9+
exportclassTrustedTypesPolicy{
10+
staticcspTrustedTypesPolicy:CSPTrustedTypesPolicy|null=null
11+
12+
staticsetTrustedTypesPolicy(policy:CSPTrustedTypesPolicy|null):void{
13+
TrustedTypesPolicy.cspTrustedTypesPolicy=policy
14+
}
15+
}

‎src/unsafe-html.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
import{directive}from'./directive.js'
22
import{NodeTemplatePart}from'@github/template-parts'
33
importtype{TemplatePart}from'@github/template-parts'
4+
import{TrustedTypesPolicy}from'./trusted-types.js'
45

56
exportconstunsafeHTML=directive((value:string)=>(part:TemplatePart)=>{
67
if(!(partinstanceofNodeTemplatePart))return
78
consttemplate=document.createElement('template')
8-
template.innerHTML=value
9+
consttrustedValue=TrustedTypesPolicy.cspTrustedTypesPolicy
10+
?(TrustedTypesPolicy.cspTrustedTypesPolicy.createHTML(value)asstring)
11+
:value
12+
template.innerHTML=trustedValue
913
constfragment=document.importNode(template.content,true)
1014
part.replace(...fragment.childNodes)
1115
})

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp