diff --git a/.changeset/tiny-parents-refuse.md b/.changeset/tiny-parents-refuse.md new file mode 100644 index 0000000..4fa1284 --- /dev/null +++ b/.changeset/tiny-parents-refuse.md @@ -0,0 +1,5 @@ +--- +'prettier-plugin-astro': minor +--- + +Fixed custom-elements being allowed to self close despite the HTML spec saying otherwise diff --git a/src/printer/index.ts b/src/printer/index.ts index cbf0f7d..dd3b2eb 100644 --- a/src/printer/index.ts +++ b/src/printer/index.ts @@ -102,9 +102,19 @@ export function print(path: AstPath, opts: ParserOptions, print: printFn): Doc { } else { isEmpty = node.children.every((child) => isEmptyTextNode(child)); } + + /** + * An element is allowed to self close only if: + * It is empty AND + * It's a component OR + * It's in the HTML spec as a void element OR + * It has a `set:*` directive + */ const isSelfClosingTag = isEmpty && - (node.type !== 'element' || selfClosingTags.includes(node.name) || hasSetDirectives(node)); + (node.type === 'component' || + selfClosingTags.includes(node.name) || + hasSetDirectives(node)); const isSingleLinePerAttribute = opts.singleAttributePerLine && node.attributes.length > 1; const attributeLine = isSingleLinePerAttribute ? breakParent : ''; diff --git a/test/fixtures/basic/html-custom-elements/output.astro b/test/fixtures/basic/html-custom-elements/output.astro index 97d2dc0..3c60556 100644 --- a/test/fixtures/basic/html-custom-elements/output.astro +++ b/test/fixtures/basic/html-custom-elements/output.astro @@ -1 +1 @@ - +