Fix <link> tag not being self-closing (#3506)

* fix(#392): ensure link tags are rendered as void elements

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

* chore: add changeset
This commit is contained in:
Nate Moore 2022-06-02 13:03:37 -05:00 committed by GitHub
parent 207f58d171
commit d41540cc77
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 1 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Fix issue where generated `link` tags would have an invalid closing tag

View file

@ -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}>`;
}

View file

@ -18,6 +18,7 @@ describe('CSS', function () {
// test HTML and CSS contents for accuracy
describe('build', () => {
let $;
let html;
let bundledCSS;
before(async () => {
@ -25,7 +26,7 @@ describe('CSS', function () {
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(/^\/?/, '/')))
@ -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);