forked from glimmerjs/glimmer-vm
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow to render TrustedHTML objects similar to SafeString
- Loading branch information
Showing
7 changed files
with
75 additions
and
3 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
49 changes: 49 additions & 0 deletions
49
packages/@glimmer-workspace/integration-tests/test/trusted-html-test.ts
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,49 @@ | ||
import type { TrustedTypePolicy, TrustedTypesWindow } from 'trusted-types/lib'; | ||
|
||
import { jitSuite, RenderTest, test } from '..'; | ||
|
||
let policy: TrustedTypePolicy | undefined; | ||
if (typeof window !== 'undefined') { | ||
let trustedTypes = (window as unknown as TrustedTypesWindow).trustedTypes; | ||
if (trustedTypes?.createPolicy) { | ||
policy = trustedTypes.createPolicy('test', { | ||
createHTML: (s: string) => s, | ||
createScript: (s: string) => s, | ||
createScriptURL: (s: string) => s, | ||
}); | ||
} | ||
} | ||
|
||
export class TrustedHTMLTests extends RenderTest { | ||
static suiteName = 'TrustedHTML'; | ||
|
||
@test | ||
'renders TrustedHTML similar to SafeString'() { | ||
if (!policy) return; | ||
|
||
let html = '<b>test\'""</b>'; | ||
this.registerHelper('trustedHTML', () => { | ||
return policy?.createHTML(html); | ||
}); | ||
|
||
this.render('<div>{{trustedHTML}}</div>'); | ||
this.assertHTML('<div><b>test\'""</b></div'); | ||
this.assertStableRerender(); | ||
} | ||
|
||
@test | ||
'renders TrustedHTML in attribute context as string'() { | ||
if (!policy) return; | ||
|
||
let html = '<b>test\'""</b>'; | ||
this.registerHelper('trustedHTML', () => { | ||
return policy?.createHTML(html); | ||
}); | ||
|
||
this.render('<a title="{{trustedHTML}}">{{trustedHTML}}</a>'); | ||
this.assertHTML('<a title="<b>test\'"&quot;</b>"><b>test\'""</b></a>'); | ||
this.assertStableRerender(); | ||
} | ||
} | ||
|
||
jitSuite(TrustedHTMLTests); |
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
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 |
---|---|---|
|
@@ -7,4 +7,5 @@ export const ContentType = { | |
Fragment: 5, | ||
Node: 6, | ||
Other: 8, | ||
TrustedHTML: 9, | ||
} as const; |