-
Notifications
You must be signed in to change notification settings - Fork 3
HTML Tool PoC spike for fragment defined in type index #151
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
|
|
||
| <!-- Auto Generated Below --> | ||
|
|
||
|
|
||
| ## Properties | ||
|
|
||
| | Property | Attribute | Description | Type | Default | | ||
| | ---------- | ---------- | ------------------------------------ | -------- | ----------- | | ||
| | `fragment` | `fragment` | HTML fragment to sanitize and render | `string` | `undefined` | | ||
|
|
||
|
|
||
| ---------------------------------------------- | ||
|
|
||
| *Built with [StencilJS](https://stenciljs.com/)* |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| import { newSpecPage } from '@stencil/core/testing'; | ||
| import { PosHtmlTool } from './pos-html-tool'; | ||
|
|
||
| describe('pos-html-tool', () => { | ||
| it('inserts sanitized HTML into the page', async () => { | ||
| const page = await newSpecPage({ | ||
| components: [PosHtmlTool], | ||
| html: `<pos-html-tool/>`, | ||
| }); | ||
| page.rootInstance.fragment = '<pos-label/>'; | ||
| await page.waitForChanges(); | ||
| expect(page.root?.innerHTML).toEqualHtml('<pos-label/>'); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import { Component, h, Host, Prop } from '@stencil/core'; | ||
| import { sanitizeHtmlTool } from './sanitizeHtmlTool'; | ||
|
|
||
| @Component({ | ||
| tag: 'pos-html-tool', | ||
| shadow: true, | ||
| }) | ||
| export class PosHtmlTool { | ||
| /** | ||
| * HTML fragment to sanitize and render | ||
| */ | ||
| @Prop() fragment: string; | ||
|
|
||
| render() { | ||
| return <Host innerHTML={sanitizeHtmlTool(this.fragment)}></Host>; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| import { sanitizeHtmlTool } from './sanitizeHtmlTool'; | ||
|
|
||
| describe('sanitizeHtmlTool', () => { | ||
| it('keeps whitelisted elements', () => { | ||
| const sanitized = sanitizeHtmlTool('<pos-label/>'); | ||
| expect(sanitized).toEqual('<pos-label/>'); | ||
| }); | ||
|
|
||
| it('removes unknown HTML elements from fragment', () => { | ||
| const sanitized = sanitizeHtmlTool('<unknown-element>'); | ||
| expect(sanitized).toEqual(''); | ||
| }); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| import DOMPurify from 'isomorphic-dompurify'; | ||
|
|
||
| export function sanitizeHtmlTool(htmlToolFragment: string) { | ||
| return DOMPurify.sanitize(htmlToolFragment, { ADD_TAGS: ['pos-label'] }); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ import { PosApp } from '../pos-app/pos-app'; | |
| import { PosResource } from '../pos-resource/pos-resource'; | ||
| import { PosLabel } from './pos-label'; | ||
| import { when } from 'jest-when'; | ||
| import { sanitizeHtmlTool } from '../pos-html-tool/sanitizeHtmlTool'; | ||
|
|
||
| describe('pos-label', () => { | ||
| it('renders label for successfully loaded resource', async () => { | ||
|
|
@@ -101,4 +102,9 @@ describe('pos-label', () => { | |
| </pos-label> | ||
| `); | ||
| }); | ||
|
|
||
| it('is whitelisted by sanitizeHtmlTool', () => { | ||
| const sanitized = sanitizeHtmlTool('<pos-label/>'); | ||
| expect(sanitized).toEqual('<pos-label/>'); | ||
| }); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As noted in #126 (comment), my intention here is that each white-listed component would manage its own set of tests to ensure that the HTML sanitization whitelists the right attributes etc. So when other changes are made, then these HTML sanitization tests are also updated. |
||
| }); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test fails because
DOMPurify.isSupportedis undefined, so DOMPurify doesn't actually run in the tests.Using
isomorphic-dompurifyis meant to fix this and doesn't, and I haven't successfully directly used jsdom either. There seems to be some kind of interaction with the stenciljs testing framework??