Skip to content

Commit

Permalink
Add defineComponent & registerAllowedCustomElement to allow tags …
Browse files Browse the repository at this point in the history
…in Aegis Sanitizer Config
  • Loading branch information
shgysk8zer0 committed Apr 5, 2024
1 parent ed6d9ff commit c8db41d
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 12 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [v1.0.2] - 2024-04-05

### Added
- Add `defineComponent` & `registerAllowedCustomElement` to allow tags in Aegis Sanitizer Config

## [v1.0.1] - 2024-04-03

### Fixed
Expand Down
29 changes: 20 additions & 9 deletions custom-elements.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,36 @@
/**
* @copyright 2023 Chris Zuber <admin@kernvalley.us>
* @copyright 2023-2024 Chris Zuber <admin@kernvalley.us>
*/
import { HTML } from '@shgysk8zer0/consts/mimes.js';
import { registerComponent as reg } from '@aegisjsproject/core/componentRegistry.js';

export const supported = globalThis.customElements instanceof Object;

export function isDefined(...tags) {
return supported && tags.every(tag => typeof customElements.get(tag) !== 'undefined');
}

export function registerCustomElement(tag, cls, ...rest) {
if (! supported) {
console.error(new Error('`customElements` not supported'));
export function defineComponent(tag, cls, allow = false) {
return allow ? registerAllowedCustomElement(tag, cls) : registerCustomElement(tag, cls);
}

export function registerAllowedCustomElement(tag, cls, ...rest) {
try {
reg(tag, cls, ...rest);
return true;
} catch(err) {
console.error(err);
return false;
} else if (isDefined(tag)) {
console.warn(new Error(`<${tag}> is already defined`));
// Returns true/false if element being registered matches given class
return customElements.get(tag) === cls;
} else {
}
}

export function registerCustomElement(tag, cls, ...rest) {
try {
customElements.define(tag, cls, ...rest);
return true;
} catch(err) {
console.error(err);
return false;
}
}

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": "@shgysk8zer0/kazoo",
"version": "1.0.1",
"version": "1.0.2",
"private": false,
"type": "module",
"description": "A JavaScript monorepo for all the things!",
Expand Down

0 comments on commit c8db41d

Please sign in to comment.