-
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
61b93f9
b9deb83
db760aa
5e4681f
cee9293
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,38 @@ | ||
| import { newSpecPage } from '@stencil/core/testing'; | ||
| import { mockPodOS } from '../../test/mockPodOS'; | ||
| import { PosHtmlTool } from './pos-html-tool'; | ||
| import { PosApp } from '../pos-app/pos-app'; | ||
| import { PosLabel } from '../pos-label/pos-label'; | ||
| import { PosResource } from '../pos-resource/pos-resource'; | ||
| import { Thing } from '@pod-os/core'; | ||
| import { when } from 'jest-when'; | ||
|
|
||
| describe('pos-html-tool', () => { | ||
| it('respects the resource event and renders inserted pos-label', async () => { | ||
| const os = mockPodOS(); | ||
| when(os.store.get) | ||
| .calledWith('https://resource.test') | ||
| .mockReturnValue({ | ||
| label: () => 'Test Resource', | ||
| } as unknown as Thing); | ||
| const page = await newSpecPage({ | ||
| components: [PosApp, PosResource, PosLabel, PosHtmlTool], | ||
| html: `<pos-app> | ||
| <pos-resource uri="https://resource.test" lazy="true"> | ||
| <pos-html-tool/> | ||
| </pos-resource> | ||
| </pos-app>`, | ||
| }); | ||
| const el = page.root?.querySelector('pos-html-tool') as unknown as PosHtmlTool; | ||
| el.fragment = '<pos-label></pos-label>'; | ||
| await page.waitForChanges(); | ||
| const label = page.root?.querySelector('pos-label'); | ||
| expect(label).toEqualHtml(` | ||
| <pos-label> | ||
| <mock:shadow-root> | ||
| Test Resource | ||
| </mock:shadow-root> | ||
| </pos-label> | ||
| `); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| /** | ||
| * @jest-environment @happy-dom/jest-environment | ||
| * | ||
| * => dompurify needs a real DOM to work | ||
| */ | ||
|
|
||
| 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></pos-label>'; | ||
| await page.waitForChanges(); | ||
| expect(page.root?.innerHTML).toEqualHtml('<pos-label></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: false, | ||
| }) | ||
| 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,19 @@ | ||
| /** | ||
| * @jest-environment @happy-dom/jest-environment | ||
| * | ||
| * => dompurify needs a real DOM to work | ||
| */ | ||
|
|
||
| import { sanitizeHtmlTool } from './sanitizeHtmlTool'; | ||
|
|
||
| describe('sanitizeHtmlTool', () => { | ||
| it('keeps whitelisted elements', () => { | ||
| const sanitized = sanitizeHtmlTool('<pos-label></pos-label>'); | ||
| expect(sanitized).toEqual('<pos-label></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 'dompurify'; | ||
|
|
||
| export function sanitizeHtmlTool(htmlToolFragment: string) { | ||
| return DOMPurify.sanitize(htmlToolFragment, { ADD_TAGS: ['pos-label'] }); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| /** | ||
| * @jest-environment @happy-dom/jest-environment | ||
| * | ||
| * => dompurify needs a real DOM to work | ||
| */ | ||
| import { sanitizeHtmlTool } from '../pos-html-tool/sanitizeHtmlTool'; | ||
|
|
||
| describe('pos-label', () => { | ||
| it('is whitelisted by sanitizeHtmlTool', () => { | ||
| const sanitized = sanitizeHtmlTool('<pos-label/>'); | ||
| expect(sanitized).toEqual('<pos-label></pos-label>'); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| import { Thing } from '@pod-os/core'; | ||
| import { Component, Event, EventEmitter, h, Listen, State } from '@stencil/core'; | ||
| import { ResourceAware, subscribeResource } from '../events/ResourceAware'; | ||
| import { selectToolsForTypes, ToolConfig } from './selectToolsForTypes'; | ||
| import { HTMLToolConfig, selectToolsForTypes, ToolConfig } from './selectToolsForTypes'; | ||
|
|
||
| /** | ||
| * This component is responsible for rendering tools that are useful to interact with the current resource. | ||
|
|
@@ -43,7 +43,21 @@ export class PosTypeRouter implements ResourceAware { | |
|
|
||
| receiveResource = (resource: Thing) => { | ||
| const types = resource.types(); | ||
| this.availableTools = selectToolsForTypes(types); | ||
| const registeredTools = [ | ||
| { | ||
| element: 'pos-html-tool', | ||
| label: 'Example tool', | ||
| icon: 'list-ul', | ||
| types: [ | ||
| { | ||
| uri: 'https://schema.org/Recipe', | ||
| priority: 20, | ||
| }, | ||
| ], | ||
| fragment: '<pos-label/>', | ||
| }, | ||
| ]; | ||
|
Collaborator
Author
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. This will need to be loaded from type index with defaults for missing values. New PodOS core methods will be needed. |
||
| this.availableTools = selectToolsForTypes(types, registeredTools); | ||
| this.currentTool = this.availableTools.find(it => it.element === this.initialTool) ?? this.availableTools[0]; | ||
| }; | ||
|
|
||
|
|
@@ -59,7 +73,14 @@ export class PosTypeRouter implements ResourceAware { | |
| <pos-tool-select selected={this.currentTool} tools={this.availableTools}></pos-tool-select> | ||
| <div class={{ tools: true, transition: this.transitioning }} onAnimationEnd={() => this.removeOldTool()}> | ||
| {OldTool && <OldTool class="tool hidden"></OldTool>} | ||
| <SelectedTool class="tool visible"></SelectedTool> | ||
| {SelectedTool == 'pos-html-tool' ? ( | ||
| <pos-html-tool | ||
| fragment={(this.currentTool as HTMLToolConfig).fragment} | ||
|
Collaborator
Author
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. This is not quite the right solution to support more than one HTML tool (if several types match). It doesn't allow them to be addressed separately through URL params.
Collaborator
Author
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. I'm considering instead allowing tools to be registered with a unique name. This would be user friendly but require an additional predicate to be present in the registration. |
||
| class="tool visible" | ||
| ></pos-html-tool> | ||
| ) : ( | ||
| <SelectedTool class="tool visible"></SelectedTool> | ||
| )} | ||
| </div> | ||
| </section> | ||
| ); | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.