Skip to content

fix(@angular/build): emit error for invalid self-closing element in index HTML #27529

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 1 commit into from
Apr 25, 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
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,25 @@ export interface FileInfo {
name?: string;
extension: string;
}

/** A list of valid self closing HTML elements */
const VALID_SELF_CLOSING_TAGS = new Set([
'area',
'base',
'br',
'col',
'embed',
'hr',
'img',
'input',
'link',
'meta',
'param',
'source',
'track',
'wbr',
]);

/*
* Helper function used by the IndexHtmlWebpackPlugin.
* Can also be directly used by builder, e. g. in order to generate an index.html
Expand Down Expand Up @@ -190,7 +209,7 @@ export async function augmentIndexHtml(
const foundPreconnects = new Set<string>();

rewriter
.on('startTag', (tag) => {
.on('startTag', (tag, rawTagHtml) => {
switch (tag.tagName) {
case 'html':
// Adjust document locale if specified
Expand Down Expand Up @@ -225,6 +244,12 @@ export async function augmentIndexHtml(
}
}
break;
default:
if (tag.selfClosing && !VALID_SELF_CLOSING_TAGS.has(tag.tagName)) {
errors.push(`Invalid self-closing element in index HTML file: '${rawTagHtml}'.`);

return;
}
}

rewriter.emitStartTag(tag);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ describe('augment-index-html', () => {

it('should add image preconnects if it encounters preconnect elements for other resources', async () => {
const imageDomains = ['https://www.example2.com', 'https://www.example3.com'];
const { content, warnings } = await augmentIndexHtml({
const { content } = await augmentIndexHtml({
...indexGeneratorOptions,
html: '<html><head><link rel="preconnect" href="https://www.example1.com"></head><body></body></html>',
imageDomains,
Expand All @@ -500,4 +500,38 @@ describe('augment-index-html', () => {
</html>
`);
});

describe('self-closing tags', () => {
it('should return an error when used on a not supported element', async () => {
const { errors } = await augmentIndexHtml({
...indexGeneratorOptions,
html: `
<html>
<body>
<app-root />
</body>
</html>'
`,
});

expect(errors.length).toEqual(1);
expect(errors).toEqual([`Invalid self-closing element in index HTML file: '<app-root />'.`]);
});

it('should not return an error when used on a supported element', async () => {
const { errors } = await augmentIndexHtml({
...indexGeneratorOptions,
html: `
<html>
<body>
<br />
<app-root><app-root>
</body>
</html>'
`,
});

expect(errors.length).toEqual(0);
});
});
});