Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions docs/elements/components/pos-html-tool/readme.md
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/)*
1 change: 1 addition & 0 deletions elements/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"@tiptap/pm": "^3.10.1",
"@tiptap/starter-kit": "^3.10.1",
"@uvdsl/solid-oidc-client-browser": "^0.1.3",
"dompurify": "^3.3.0",
"idb": "^8.0.3",
"neverthrow": "^8.2.0",
"pollen-css": "^5.0.2",
Expand Down
21 changes: 21 additions & 0 deletions elements/src/components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ export namespace Components {
}
interface PosGettingStarted {
}
interface PosHtmlTool {
/**
* HTML fragment to sanitize and render
*/
"fragment": string;
}
/**
* Tries fetch an image with the solid authentication, and can visualize http errors like 403 or 404 if this fails.
* Falls back to classic `<img src="...">` on network errors like CORS.
Expand Down Expand Up @@ -625,6 +631,12 @@ declare global {
prototype: HTMLPosGettingStartedElement;
new (): HTMLPosGettingStartedElement;
};
interface HTMLPosHtmlToolElement extends Components.PosHtmlTool, HTMLStencilElement {
}
var HTMLPosHtmlToolElement: {
prototype: HTMLPosHtmlToolElement;
new (): HTMLPosHtmlToolElement;
};
interface HTMLPosImageElementEventMap {
"pod-os:init": any;
"pod-os:resource-loaded": string;
Expand Down Expand Up @@ -1113,6 +1125,7 @@ declare global {
"pos-error-toast": HTMLPosErrorToastElement;
"pos-example-resources": HTMLPosExampleResourcesElement;
"pos-getting-started": HTMLPosGettingStartedElement;
"pos-html-tool": HTMLPosHtmlToolElement;
"pos-image": HTMLPosImageElement;
"pos-internal-router": HTMLPosInternalRouterElement;
"pos-label": HTMLPosLabelElement;
Expand Down Expand Up @@ -1238,6 +1251,12 @@ declare namespace LocalJSX {
interface PosGettingStarted {
"onPod-os:login"?: (event: PosGettingStartedCustomEvent<void>) => void;
}
interface PosHtmlTool {
/**
* HTML fragment to sanitize and render
*/
"fragment"?: string;
}
/**
* Tries fetch an image with the solid authentication, and can visualize http errors like 403 or 404 if this fails.
* Falls back to classic `<img src="...">` on network errors like CORS.
Expand Down Expand Up @@ -1476,6 +1495,7 @@ declare namespace LocalJSX {
"pos-error-toast": PosErrorToast;
"pos-example-resources": PosExampleResources;
"pos-getting-started": PosGettingStarted;
"pos-html-tool": PosHtmlTool;
"pos-image": PosImage;
"pos-internal-router": PosInternalRouter;
"pos-label": PosLabel;
Expand Down Expand Up @@ -1533,6 +1553,7 @@ declare module "@stencil/core" {
"pos-error-toast": LocalJSX.PosErrorToast & JSXBase.HTMLAttributes<HTMLPosErrorToastElement>;
"pos-example-resources": LocalJSX.PosExampleResources & JSXBase.HTMLAttributes<HTMLPosExampleResourcesElement>;
"pos-getting-started": LocalJSX.PosGettingStarted & JSXBase.HTMLAttributes<HTMLPosGettingStartedElement>;
"pos-html-tool": LocalJSX.PosHtmlTool & JSXBase.HTMLAttributes<HTMLPosHtmlToolElement>;
/**
* Tries fetch an image with the solid authentication, and can visualize http errors like 403 or 404 if this fails.
* Falls back to classic `<img src="...">` on network errors like CORS.
Expand Down
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>
`);
});
});
20 changes: 20 additions & 0 deletions elements/src/components/pos-html-tool/pos-html-tool.spec.tsx
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>');
});
});
17 changes: 17 additions & 0 deletions elements/src/components/pos-html-tool/pos-html-tool.tsx
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>;
}
}
19 changes: 19 additions & 0 deletions elements/src/components/pos-html-tool/sanitizeHtmlTool.spec.tsx
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('');
});
});
5 changes: 5 additions & 0 deletions elements/src/components/pos-html-tool/sanitizeHtmlTool.tsx
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>');
});
});
23 changes: 23 additions & 0 deletions elements/src/components/pos-type-router/pos-type-router.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,29 @@ describe('pos-type-router', () => {
`);
});

it('renders HTML tool if available', async () => {
const page = await newSpecPage({
components: [PosTypeRouter],
html: `<pos-type-router />`,
supportsShadowDom: false,
});
await page.rootInstance.receiveResource({
types: () => [{ uri: 'https://schema.org/Recipe', label: 'Recipe' }],
});
await page.waitForChanges();

expect(page.root).toEqualHtml(`
<pos-type-router>
<section>
<pos-tool-select></pos-tool-select>
<div class="tools">
<pos-html-tool class="tool visible" fragment="<pos-label/>"></pos-html-tool>
</div>
</section>
</pos-type-router>
`);
});

it('renders the selected tool and updates query param', async () => {
const page = await newSpecPage({
components: [PosTypeRouter],
Expand Down
27 changes: 24 additions & 3 deletions elements/src/components/pos-type-router/pos-type-router.tsx
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.
Expand Down Expand Up @@ -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/>',
},
];
Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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];
};

Expand All @@ -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}
Copy link
Collaborator Author

@jg10-mastodon-social jg10-mastodon-social Nov 16, 2025

Choose a reason for hiding this comment

The 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.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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.
Another alternative would be a hash of their content. This would more opaque.

class="tool visible"
></pos-html-tool>
) : (
<SelectedTool class="tool visible"></SelectedTool>
)}
</div>
</section>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,23 @@ describe('select tools for types', () => {
AvailableTools.Generic,
]);
});

it('favours HTML tool over generic if one is available', () => {
const registeredTools = [
{
element: 'pos-html-tool',
label: 'Example tool',
icon: 'list-ul',
types: [
{
uri: 'https://schema.org/Recipe',
priority: 20,
},
],
},
];
const types = [{ uri: 'https://schema.org/Recipe', label: 'Recipe' }];
const tools = selectToolsForTypes(types, registeredTools);
expect(tools).toEqual([registeredTools[0], AvailableTools.Generic]);
});
});
11 changes: 9 additions & 2 deletions elements/src/components/pos-type-router/selectToolsForTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ export interface ToolConfig {
types: TypePriority[];
}

/**
* Describes a tool that can be used implemented as a HTML fragment
*/
export interface HTMLToolConfig extends ToolConfig {
fragment: string;
}

/**
* Describes how well a given RDF type can be handled
*/
Expand Down Expand Up @@ -52,10 +59,10 @@ interface ToolPriority {
priority: number;
}

export function selectToolsForTypes(types: RdfType[]) {
export function selectToolsForTypes(types: RdfType[], registeredTools: ToolConfig[] = []) {
const typeUris = new Set(types.map(type => type.uri));

return Object.values(AvailableTools)
return [...Object.values(AvailableTools), ...registeredTools]
.map(maxPriorityFor(typeUris))
.filter(onlyRelevant)
.toSorted(byPriority)
Expand Down
13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.