Skip to content

Commit 6ce4664

Browse files
committed
Add createTrustedHTMLTemplate()
1 parent d32c197 commit 6ce4664

File tree

8 files changed

+27
-11
lines changed

8 files changed

+27
-11
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
## [v0.2.23] - 2025-04-08
11+
12+
### Added
13+
- Add `createTrustedHTMLTemplate()` to create trusted HTML generating tagged templates
14+
1015
## [v0.2.22] - 2024-12-25
1116

1217
### Changed

core.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export {
1515
export {
1616
text, createStyleSheet, createCSSParser, css, lightCSS, darkCSS ,
1717
styleSheetToFile, styleSheetToLink, createHTMLParser, html, doc,
18-
htmlUnsafe, docUnsafe, htmlToFile, xml, svg, json, math, url,
18+
htmlUnsafe, docUnsafe, htmlToFile, createTrustedHTMLTemplate, xml, svg, json, math, url,
1919
} from './parsers.js';
2020

2121
export {

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@aegisjsproject/core",
3-
"version": "0.2.22",
3+
"version": "0.2.23",
44
"description": "A fast, secure, modern, light-weight, and simple JS library for creating web components and more!",
55
"keywords": [
66
"aegis",

parsers.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ export {
33
createStyleSheet, createCSSParser, css, lightCSS,
44
darkCSS, styleSheetToFile, styleSheetToLink,
55
} from './parsers/css.js';
6-
export { createHTMLParser, html, doc, htmlUnsafe, docUnsafe, htmlToFile } from './parsers/html.js';
6+
export { createHTMLParser, html, doc, htmlUnsafe, docUnsafe, htmlToFile, createTrustedHTMLTemplate } from './parsers/html.js';
77
export { xml } from './parsers/xml.js';
88
export { svg } from './parsers/svg.js';
99
export { json } from './parsers/json.js';

parsers/html.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { stringify } from '../stringify.js';
22
import { sanitizer as sanitizerConfig } from '@aegisjsproject/sanitizer/config/base.js';
33
import { getRegisteredComponentTags } from '../componentRegistry.js';
44
import { createHTMLParser, doc } from '@aegisjsproject/parsers/html.js';
5+
import { isTrustPolicy } from '../trust.js';
56

67
const sanitizer = Object.freeze({
78
...sanitizerConfig,
@@ -15,6 +16,14 @@ const sanitizer = Object.freeze({
1516

1617
export const html = createHTMLParser(sanitizer, { mapper: stringify });
1718

19+
export function createTrustedHTMLTemplate(policy) {
20+
if (isTrustPolicy(policy)) {
21+
return (...args) => policy.createHTML(String.raw.apply(null, args));
22+
} else {
23+
return String.raw;
24+
}
25+
}
26+
1827
export function htmlUnsafe(strings, ...values) {
1928
const html = String.raw(strings, ...values.map(stringify));
2029
const frag = document.createDocumentFragment();

test/index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {
22
html, css, replaceStyles, getUniqueSelector, createComponent, closeRegistration,
3-
data, attr, policy,
3+
data, attr, policy, createTrustedHTMLTemplate,
44
} from '@aegisjsproject/core';
55

66
import { FUNCS, observeEvents, on, registerEventAttribute } from '@aegisjsproject/callback-registry';
@@ -12,6 +12,8 @@ import { manageState, stateStyle, stateKey, observeDOMState } from '@aegisjsproj
1212
import * as bootstrap from '@aegisjsproject/styles/palette/bootstrap.js';
1313
import './dad-joke.js';
1414

15+
const trustedHTML = createTrustedHTMLTemplate(policy);
16+
1517
const controller = new AbortController();
1618
const imgController = new AbortController();
1719
const [bg, setBg] = manageState('bg', '#232323');
@@ -106,7 +108,9 @@ try {
106108
}, { once: true, signal: imgController.signal })} />
107109
</div>
108110
<div id="help" popover="auto">Should be shown on button scroll</div>
109-
<footer id="footer">
111+
`);
112+
113+
document.body.insertAdjacentHTML('beforeend', trustedHTML`<footer id="footer">
110114
<div>
111115
<template shadowrootmode="closed">
112116
<p part="content"><slot name="content">No Content</p>

trust.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,10 +176,8 @@ export function createPolicy(name, {
176176

177177
export function createSanitizerPolicy(name = 'aegis#html') {
178178
return createPolicy(name, {
179-
createHTML(input, sanitizer) {
180-
const el = document.createElement('div');
181-
el.setHTML(input, { sanitizer });
182-
return el.innerHTML;
179+
createHTML(input, config) {
180+
return Document.parseHTML(input, config).body.innerHTML;
183181
}
184182
});
185183
}

0 commit comments

Comments
 (0)