Skip to content

Commit 98d5d06

Browse files
authored
Merge pull request #136 from AegisJSProject/feature/html-parsers
Add new HTML parser options
2 parents 266ac6b + e7ba89b commit 98d5d06

File tree

6 files changed

+65
-7
lines changed

6 files changed

+65
-7
lines changed

CHANGELOG.md

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

1010
## [v0.2.23] - 2025-04-08
1111

12+
## [v0.2.24] - 2025-04-17
13+
14+
### Added
15+
- Add parser for web component/shadow DOM
16+
17+
### Changed
18+
- `createTrustedHTMLTemplate` now throws if not given a `TrustedTypePolicy`
19+
1220
### Added
1321
- Add `createTrustedHTMLTemplate()` to create trusted HTML generating tagged templates
1422

core.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ export {
1414

1515
export {
1616
text, createStyleSheet, createCSSParser, css, lightCSS, darkCSS ,
17-
styleSheetToFile, styleSheetToLink, createHTMLParser, html, doc,
17+
styleSheetToFile, styleSheetToLink, createHTMLParser, html, doc, trustedHTML,
1818
htmlUnsafe, docUnsafe, htmlToFile, createTrustedHTMLTemplate, xml, svg, json, math, url,
19+
createShadowParser, shadow, el,
1920
} from './parsers.js';
2021

2122
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.23",
3+
"version": "0.2.24",
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: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ export {
33
createStyleSheet, createCSSParser, css, lightCSS,
44
darkCSS, styleSheetToFile, styleSheetToLink,
55
} from './parsers/css.js';
6-
export { createHTMLParser, html, doc, htmlUnsafe, docUnsafe, htmlToFile, createTrustedHTMLTemplate } from './parsers/html.js';
6+
export {
7+
createHTMLParser, html, doc, htmlUnsafe, docUnsafe, htmlToFile,
8+
createTrustedHTMLTemplate, trustedHTML, createShadowParser, shadow, el,
9+
} from './parsers/html.js';
710
export { xml } from './parsers/xml.js';
811
export { svg } from './parsers/svg.js';
912
export { json } from './parsers/json.js';

parsers/html.js

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,57 @@ const sanitizer = Object.freeze({
1616

1717
export const html = createHTMLParser(sanitizer, { mapper: stringify });
1818

19+
export const el = (...args) => html.apply(null, args)?.firstElementChild;
20+
21+
export function createShadowParser({
22+
tagName = 'div',
23+
mode = 'open',
24+
clonable = true,
25+
serializable = true,
26+
delegatesFocus = false,
27+
slotAssignment = 'named',
28+
sanitizer: sanitizerConfig = sanitizer,
29+
exportParts,
30+
} = {}) {
31+
const parser = createHTMLParser(sanitizerConfig, { mapper: stringify });
32+
33+
if (Array.isArray(exportParts)) {
34+
exportParts = exportParts.join(', ');
35+
} else if (typeof exportParts === 'object') {
36+
exportParts = Object.entries(exportParts).map(([k, v]) => `${k}: ${v}`).join(', ');
37+
}
38+
39+
return (...args) => {
40+
const host = document.createElement(tagName);
41+
const shadowRoot = host.attachShadow({ mode, clonable, serializable, delegatesFocus, slotAssignment });
42+
43+
shadowRoot.append(parser.apply(parser, args));
44+
45+
if (typeof exportParts === 'string') {
46+
host.setAttribute('exportparts', exportParts);
47+
}
48+
49+
return host;
50+
};
51+
}
52+
53+
export const shadow = createShadowParser();
54+
1955
export function createTrustedHTMLTemplate(policy) {
2056
if (isTrustPolicy(policy)) {
21-
return (...args) => policy.createHTML(String.raw.apply(null, args));
57+
return (strings, ...values) => policy.createHTML(String.raw(strings, ...values.map(stringify)));
58+
} else {
59+
throw new TypeError('Not a Trusted Types Policy.');
60+
}
61+
}
62+
63+
export function trustedHTML(strings, ...values) {
64+
if (isTrustPolicy(trustedTypes?.defaultPolicy)) {
65+
return trustedTypes.defaultPolicy.createHTML(String.raw(strings, ...values.map(stringify)));
66+
} else if (! ('trustedTypes' in globalThis)) {
67+
throw new Error('Trusted Types is not supported.');
2268
} else {
23-
return String.raw;
69+
throw new TypeError('No default Trusted Types Policy is available.');
2470
}
2571
}
2672

0 commit comments

Comments
 (0)