Skip to content
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

Render void elements as self-closing tags #2064

Merged
merged 5 commits into from
Dec 1, 2021
Merged

Render void elements as self-closing tags #2064

merged 5 commits into from
Dec 1, 2021

Conversation

jonathantneal
Copy link
Contributor

@jonathantneal jonathantneal commented Dec 1, 2021

Changes

Testing

no tests added

Docs

bug fix only

@changeset-bot
Copy link

changeset-bot bot commented Dec 1, 2021

🦋 Changeset detected

Latest commit: 437d6c7

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
astro Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@netlify
Copy link

netlify bot commented Dec 1, 2021

✔️ Deploy Preview for astro-docs-2 ready!

🔨 Explore the source changes: 437d6c7

🔍 Inspect the deploy log: https://app.netlify.com/sites/astro-docs-2/deploys/61a7dc45d6807700079ddf79

😎 Browse the preview: https://deploy-preview-2064--astro-docs-2.netlify.app

@netlify
Copy link

netlify bot commented Dec 1, 2021

✔️ Deploy Preview for astro-www ready!

🔨 Explore the source changes: 437d6c7

🔍 Inspect the deploy log: https://app.netlify.com/sites/astro-www/deploys/61a7dc459dfbec0008593276

😎 Browse the preview: https://deploy-preview-2064--astro-www.netlify.app

@github-actions github-actions bot added the pkg: astro Related to the core `astro` package (scope) label Dec 1, 2021
@matthewp
Copy link
Contributor

matthewp commented Dec 1, 2021

Can this just be > and not />?

@@ -223,7 +225,7 @@ If you're still stuck, please open an issue on GitHub or join us at https://astr
// This is a custom element without a renderer. Because of that, render it
// as a string and the user is responsible for adding a script tag for the component definition.
if (!html && typeof Component === 'string') {
html = await renderAstroComponent(await render`<${Component}${spreadAttributes(props)}>${children}</${Component}>`);
html = await renderAstroComponent(await render`<${Component}${spreadAttributes(props)}${voidElementNames.test(Component) ? `/>` : `>${children}</${Component}>`}`);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this just be > and not />?

Yes, it could. My preference is to be explicit.

@drwpow
Copy link
Member

drwpow commented Dec 1, 2021

Could we add a few tests for this, maybe in astro-basic? It would be good to ensure > vs /> doesn’t affect it. It’d also be good to test for things like SVG <path /> (not that our compiler should transform them; just that they don’t break and render as-is)

@jonathantneal
Copy link
Contributor Author

Some tests of the capability have been added, @drwpow.

expect($('body > :nth-child(1)').prop('outerHTML')).to.equal('<input>');

// <Input type="password" />
expect($('body > :nth-child(2)').prop('outerHTML')).to.equal('<input type="password">');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome tests!

Copy link
Member

@drwpow drwpow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great tests, thank you! And good fix

@@ -223,7 +225,9 @@ If you're still stuck, please open an issue on GitHub or join us at https://astr
// This is a custom element without a renderer. Because of that, render it
// as a string and the user is responsible for adding a script tag for the component definition.
if (!html && typeof Component === 'string') {
html = await renderAstroComponent(await render`<${Component}${spreadAttributes(props)}>${children}</${Component}>`);
html = await renderAstroComponent(
await render`<${Component}${spreadAttributes(props)}${(children == null || children == '') && voidElementNames.test(Component) ? `/>` : `>${children}</${Component}>`}`
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Eh, ==? Yea, it’s that or children === undefined || children === null || String(children) === ''. This is knowingly loose. And the linter doesn’t mind. 😅

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is good enough. I can’t think of a problem with == here

@jonathantneal jonathantneal merged commit 5bda895 into withastro:main Dec 1, 2021
@jonathantneal jonathantneal deleted the jn.fix-self-closing-reference-components branch December 1, 2021 20:43
@github-actions github-actions bot mentioned this pull request Dec 1, 2021
vineryap pushed a commit to vineryap/astro that referenced this pull request Dec 22, 2021
* Render void elements as self-closing tags

* changeset

* nit: only check void element names if there is no child content

* nit: only check void element names if there is no child content

* add tests
SiriousHunter pushed a commit to SiriousHunter/astro that referenced this pull request Feb 3, 2023
* Render void elements as self-closing tags

* changeset

* nit: only check void element names if there is no child content

* nit: only check void element names if there is no child content

* add tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pkg: astro Related to the core `astro` package (scope)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

🐛 BUG: Components cannot self-close
4 participants