Skip to content

Commit

Permalink
Fix <link> tag not being self-closing (#3506)
Browse files Browse the repository at this point in the history
* fix(#392): ensure link tags are rendered as void elements

* test: ensure html does not include a `</link>` end tag

* chore: add changeset
  • Loading branch information
natemoo-re authored Jun 2, 2022
1 parent 207f58d commit d41540c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/new-knives-raise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fix issue where generated `link` tags would have an invalid closing tag
3 changes: 3 additions & 0 deletions packages/astro/src/runtime/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -715,5 +715,8 @@ function renderElement(
children = defineScriptVars(defineVars) + '\n' + children;
}
}
if ((children == null || children == '') && voidElementNames.test(name)) {
return `<${name}${internalSpreadAttributes(props, shouldEscape)} />`;
}
return `<${name}${internalSpreadAttributes(props, shouldEscape)}>${children}</${name}>`;
}
7 changes: 6 additions & 1 deletion packages/astro/test/0-css.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ describe('CSS', function () {
// test HTML and CSS contents for accuracy
describe('build', () => {
let $;
let html;
let bundledCSS;

before(async () => {
this.timeout(45000); // test needs a little more time in CI
await fixture.build();

// get bundled CSS (will be hashed, hence DOM query)
const html = await fixture.readFile('/index.html');
html = await fixture.readFile('/index.html');
$ = cheerio.load(html);
const bundledCSSHREF = $('link[rel=stylesheet][href^=/assets/]').attr('href');
bundledCSS = (await fixture.readFile(bundledCSSHREF.replace(/^\/?/, '/')))
Expand All @@ -49,6 +50,10 @@ describe('CSS', function () {
expect(bundledCSS).to.include(expected);
});

it('Generated link tags are void elements', async () => {
expect(html).to.not.include("</link>");
});

it('No <style> skips scoping', async () => {
// Astro component without <style> should not include scoped class
expect($('#no-scope').attr('class')).to.equal(undefined);
Expand Down

0 comments on commit d41540c

Please sign in to comment.