Skip to content

Commit

Permalink
Merge pull request #5 from AegisJSProject/feature/customization
Browse files Browse the repository at this point in the history
Improve customization
  • Loading branch information
shgysk8zer0 authored Mar 31, 2024
2 parents 2a5c687 + b1e9dbb commit 6ecb116
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 13 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [v0.0.4] - 2024-03-30

### Added
- Add functions to easily add/set stylesheets
- Add option to set custom stringify/map function in HTML parsers
- Add web component example in README

## [v0.0.3] - 2024-03-29

### Added
Expand Down
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,28 @@ document.adoptedStyleSheets = [css`
`];
```

## Web Component Example

```js
import { template } from './template.js';
import { base, dark, light } from './theme.js';
import { btnStyles, cardStyles } from './styles.js';

class MyComponent extends HTMLElement {
#shadow;

constructor() {
super();

this.#shadow = this.attachShadow({ mode: 'closed' });
this.#shadow.append(template);
this.#shadow.adoptedStyleSheets = [base, dark, light btnStyles, cardStyes];
}
}

customElements.define('my-component', MyComponent);
```

> [!WARNING]
> The Sanitizer API is still being developed, and could change. Until the API
> is stable, this project will remain pre-v1.0.0
Expand Down
16 changes: 16 additions & 0 deletions css.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,19 @@ export function styleSheetToLink(sheet, { crossOrigin = 'anonymous', referrerPol
link.href = URL.createObjectURL(file);
return link;
}

export function setStyleSheets(node, ...styles) {
if (node instanceof HTMLDocument || node instanceof ShadowRoot) {
node.adoptedStyleSheets = styles;
} else {
throw new TypeError('Node must be a `HTMLDocument` or `ShadowRoot`.');
}
}

export function addStyleSheets(node, ...styles) {
if (node instanceof Document || node instanceof ShadowRoot) {
node.adoptedStyleSheets = [...node.adoptedStyleSheets, ...styles];
} else {
throw new TypeError('Node must be a `HTMLDocument` or `ShadowRoot`.');
}
}
18 changes: 10 additions & 8 deletions html.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { sanitizer } from '@aegisjsproject/sanitizer/config/base.js';
import { stringify } from './utils.js';

export const createHTMLParser = (config = sanitizer) => (strings, ...values) => {
const el = document.createElement('div');
const frag = document.createDocumentFragment();
el.setHTML(String.raw(strings, ...values.map(stringify)).trim(), { sanitizer: config });
frag.append(...el.childNodes);
return frag;
};
export function createHTMLParser(config = sanitizer, { mapper = stringify } = {}) {
return (strings, ...values) => {
const el = document.createElement('div');
const frag = document.createDocumentFragment();
el.setHTML(String.raw(strings, ...values.map(mapper)).trim(), { sanitizer: config });
frag.append(...el.childNodes);
return frag;
};
}

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

export const el = (...args) => html.apply(null, args).firstElementChild;

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@aegisjsproject/parsers",
"version": "0.0.3",
"version": "0.0.4",
"description": "A collection of secure & minimal parsers for HTML, CSS, SVG, MathML, XML, and JSON",
"keywords": [
"aegis",
Expand Down
5 changes: 4 additions & 1 deletion parsers.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
export { html, el, doc, createHTMLParser } from './html.js';
export { css, createCSSParser, styleSheetToLink, createStyleSheet } from './css.js';
export {
css, createCSSParser, styleSheetToLink, createStyleSheet, addStyleSheets,
setStyleSheets,
} from './css.js';
export { svg } from './svg.js';
export { math } from './math.js';
export { xml } from './xml.js';
Expand Down
2 changes: 1 addition & 1 deletion utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export function stringify(thing) {
return thing;

case 'function':
throw new TypeError('Functions are not allowed.');
throw new TypeError('Functions are not supported.');

case 'object':
if (thing === null) {
Expand Down

0 comments on commit 6ecb116

Please sign in to comment.