Skip to content

Commit d00c018

Browse files
authored
fix(isolated-element): allow certain builtin elements (#116)
1 parent c14e5b8 commit d00c018

File tree

3 files changed

+49
-5
lines changed

3 files changed

+49
-5
lines changed

packages/isolated-element/src/index.test.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,28 @@ describe('createIsolatedElement', () => {
99
document.querySelector('body')!.innerHTML = '';
1010
});
1111

12-
it('should validate the custom element name', async () => {
12+
it('should not allow invalid custom element names', async () => {
1313
const invalidName = 'test';
1414

1515
await expect(createIsolatedElement({ name: invalidName })).rejects.toThrow(
16-
`"${invalidName}" is not a valid custom element name`,
16+
`"${invalidName}" cannot have a shadow root attached to it`,
1717
);
1818
});
1919

20+
it('should not allow certain built-in elements', async () => {
21+
const invalidName = 'a';
22+
23+
await expect(createIsolatedElement({ name: invalidName })).rejects.toThrow(
24+
`"${invalidName}" cannot have a shadow root attached to it`,
25+
);
26+
});
27+
28+
it('should allow certain built-in elements', async () => {
29+
const validName = 'div';
30+
31+
await expect(createIsolatedElement({ name: validName })).resolves.toBeDefined();
32+
});
33+
2034
it('should insert an app into the UI', async () => {
2135
const text = 'Example';
2236
const appId = 'app';

packages/isolated-element/src/index.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,31 @@
11
import isPotentialCustomElementName from 'is-potential-custom-element-name';
22
import { CreateIsolatedElementOptions } from './options';
33

4+
/**
5+
* Built-in elements that can have a shadow root attached to them.
6+
* @see https://developer.mozilla.org/en-US/docs/Web/API/Element/attachShadow#elements_you_can_attach_a_shadow_to
7+
*/
8+
const ALLOWED_SHADOW_ELEMENTS = [
9+
'article',
10+
'aside',
11+
'blockquote',
12+
'body',
13+
'div',
14+
'footer',
15+
'h1',
16+
'h2',
17+
'h3',
18+
'h4',
19+
'h5',
20+
'h6',
21+
'header',
22+
'main',
23+
'nav',
24+
'p',
25+
'section',
26+
'span',
27+
];
28+
429
export type { CreateIsolatedElementOptions };
530

631
/**
@@ -33,9 +58,9 @@ export async function createIsolatedElement(options: CreateIsolatedElementOption
3358
}> {
3459
const { name, mode = 'closed', css, isolateEvents = false } = options;
3560

36-
if (!isPotentialCustomElementName(name)) {
61+
if (!ALLOWED_SHADOW_ELEMENTS.includes(name) && !isPotentialCustomElementName(name)) {
3762
throw Error(
38-
`"${name}" is not a valid custom element name. It must be two words and kebab-case, with a few exceptions. See spec for more details: https://html.spec.whatwg.org/multipage/custom-elements.html#valid-custom-element-name`,
63+
`"${name}" cannot have a shadow root attached to it. It must be two words and kebab-case, with a few exceptions. See https://developer.mozilla.org/en-US/docs/Web/API/Element/attachShadow#elements_you_can_attach_a_shadow_to`,
3964
);
4065
}
4166

packages/isolated-element/src/options.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,15 @@
33
*/
44
export interface CreateIsolatedElementOptions {
55
/**
6-
* A unique HTML tag name (two words, kebab case - [see spec](https://html.spec.whatwg.org/multipage/custom-elements.html#valid-custom-element-name)) used when defining the web component used internally. Don't use the same name twice for different UIs.
6+
* An HTML tag name used for the shadow root container.
7+
*
8+
* Note that you can't attach a shadow root to every type of element. There are some that can't have a shadow DOM for security reasons (for example <a>).
9+
*
10+
* @see https://developer.mozilla.org/en-US/docs/Web/API/Element/attachShadow#elements_you_can_attach_a_shadow_to
711
* @example "sticky-note"
812
* @example "anime-skip-player"
913
* @example "github-better-line-count-diff"
14+
* @example "div"
1015
*/
1116
name: string;
1217
/**

0 commit comments

Comments
 (0)