Skip to content

fix: follow spec for customElement option #13247

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/stale-rats-check.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: follow spec for `customElement` option
10 changes: 9 additions & 1 deletion packages/svelte/messages/compile-errors/template.md
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,15 @@ HTML restricts where certain elements can appear. In case of a violation the bro

## svelte_options_invalid_tagname

> Tag name must be two or more words joined by the "-" character
> Tag name must be lowercase and hyphenated

See https://html.spec.whatwg.org/multipage/custom-elements.html#valid-custom-element-name for more information on valid tag names

## svelte_options_reserved_tagname

> Tag name is reserved

See https://html.spec.whatwg.org/multipage/custom-elements.html#valid-custom-element-name for more information on valid tag names

## svelte_options_unknown_attribute

Expand Down
13 changes: 11 additions & 2 deletions packages/svelte/src/compiler/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -1352,12 +1352,21 @@ export function svelte_options_invalid_customelement_shadow(node) {
}

/**
* Tag name must be two or more words joined by the "-" character
* Tag name must be lowercase and hyphenated
* @param {null | number | NodeLike} node
* @returns {never}
*/
export function svelte_options_invalid_tagname(node) {
e(node, "svelte_options_invalid_tagname", "Tag name must be two or more words joined by the \"-\" character");
e(node, "svelte_options_invalid_tagname", "Tag name must be lowercase and hyphenated");
}

/**
* Tag name is reserved
* @param {null | number | NodeLike} node
* @returns {never}
*/
export function svelte_options_reserved_tagname(node) {
e(node, "svelte_options_reserved_tagname", "Tag name is reserved");
}

/**
Expand Down
25 changes: 21 additions & 4 deletions packages/svelte/src/compiler/phases/1-parse/read/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import { NAMESPACE_MATHML, NAMESPACE_SVG } from '../../../../constants.js';
import * as e from '../../../errors.js';

const regex_valid_tag_name = /^[a-zA-Z][a-zA-Z0-9]*-[a-zA-Z0-9-]+$/;

/**
* @param {AST.SvelteOptionsRaw} node
* @returns {AST.Root['options']}
Expand Down Expand Up @@ -229,6 +227,21 @@ function get_boolean_value(attribute) {
return value;
}

// https://html.spec.whatwg.org/multipage/custom-elements.html#valid-custom-element-name
const tag_name_char =
'[a-z0-9_.\xB7\xC0-\xD6\xD8-\xF6\xF8-\u037D\u037F-\u1FFF\u200C-\u200D\u203F-\u2040\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD\u{10000}-\u{EFFFF}-]';
const regex_valid_tag_name = new RegExp(`^[a-z]${tag_name_char}*-${tag_name_char}*$`, 'u');
const reserved_tag_names = [
'annotation-xml',
'color-profile',
'font-face',
'font-face-src',
'font-face-uri',
'font-face-format',
'font-face-name',
'missing-glyph'
];

/**
* @param {any} attribute
* @param {string | null} tag
Expand All @@ -238,7 +251,11 @@ function validate_tag(attribute, tag) {
if (typeof tag !== 'string') {
e.svelte_options_invalid_tagname(attribute);
}
if (tag && !regex_valid_tag_name.test(tag)) {
e.svelte_options_invalid_tagname(attribute);
if (tag) {
if (!regex_valid_tag_name.test(tag)) {
e.svelte_options_invalid_tagname(attribute);
} else if (reserved_tag_names.includes(tag)) {
e.svelte_options_reserved_tagname(attribute);
}
}
}
7 changes: 7 additions & 0 deletions packages/svelte/tests/validator/samples/tag-emoji/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { test } from '../../test';

export default test({
compileOptions: {
customElement: true
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<svelte:options customElement="emotion-😍" />
7 changes: 7 additions & 0 deletions packages/svelte/tests/validator/samples/tag-hyphen/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { test } from '../../test';

export default test({
compileOptions: {
customElement: true
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<svelte:options customElement="custom-" />
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[
{
"code": "svelte_options_invalid_tagname",
"message": "Tag name must be two or more words joined by the \"-\" character",
"message": "Tag name must be lowercase and hyphenated",
"start": {
"line": 1,
"column": 16
Expand Down
14 changes: 14 additions & 0 deletions packages/svelte/tests/validator/samples/tag-reserved/errors.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"code": "svelte_options_reserved_tagname",
"message": "Tag name is reserved",
"start": {
"line": 1,
"column": 16
},
"end": {
"line": 1,
"column": 41
}
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<svelte:options customElement="font-face" />