Skip to content

Commit 70dbc7a

Browse files
committedApr 25, 2024·
fix(@angular/build): emit error for invalid self-closing element in index HTML
When an invalid self-closing HTML element is used in the index.html file, the build proceeds without raising an error, potentially leading to runtime issues. Now, when an invalid self-closing element is encountered in the index.html file, the build process issues an error, providing developers with immediate feedback to correct the issue. Closes: #27528
1 parent 4087728 commit 70dbc7a

File tree

2 files changed

+61
-2
lines changed

2 files changed

+61
-2
lines changed
 

‎packages/angular/build/src/utils/index-file/augment-index-html.ts

+26-1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,25 @@ export interface FileInfo {
4848
name?: string;
4949
extension: string;
5050
}
51+
52+
/** A list of valid self closing HTML elements */
53+
const VALID_SELF_CLOSING_TAGS = new Set([
54+
'area',
55+
'base',
56+
'br',
57+
'col',
58+
'embed',
59+
'hr',
60+
'img',
61+
'input',
62+
'link',
63+
'meta',
64+
'param',
65+
'source',
66+
'track',
67+
'wbr',
68+
]);
69+
5170
/*
5271
* Helper function used by the IndexHtmlWebpackPlugin.
5372
* Can also be directly used by builder, e. g. in order to generate an index.html
@@ -190,7 +209,7 @@ export async function augmentIndexHtml(
190209
const foundPreconnects = new Set<string>();
191210

192211
rewriter
193-
.on('startTag', (tag) => {
212+
.on('startTag', (tag, rawTagHtml) => {
194213
switch (tag.tagName) {
195214
case 'html':
196215
// Adjust document locale if specified
@@ -225,6 +244,12 @@ export async function augmentIndexHtml(
225244
}
226245
}
227246
break;
247+
default:
248+
if (tag.selfClosing && !VALID_SELF_CLOSING_TAGS.has(tag.tagName)) {
249+
errors.push(`Invalid self-closing element in index HTML file: '${rawTagHtml}'.`);
250+
251+
return;
252+
}
228253
}
229254

230255
rewriter.emitStartTag(tag);

‎packages/angular/build/src/utils/index-file/augment-index-html_spec.ts

+35-1
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ describe('augment-index-html', () => {
481481

482482
it('should add image preconnects if it encounters preconnect elements for other resources', async () => {
483483
const imageDomains = ['https://www.example2.com', 'https://www.example3.com'];
484-
const { content, warnings } = await augmentIndexHtml({
484+
const { content } = await augmentIndexHtml({
485485
...indexGeneratorOptions,
486486
html: '<html><head><link rel="preconnect" href="https://www.example1.com"></head><body></body></html>',
487487
imageDomains,
@@ -500,4 +500,38 @@ describe('augment-index-html', () => {
500500
</html>
501501
`);
502502
});
503+
504+
describe('self-closing tags', () => {
505+
it('should return an error when used on a not supported element', async () => {
506+
const { errors } = await augmentIndexHtml({
507+
...indexGeneratorOptions,
508+
html: `
509+
<html>
510+
<body>
511+
<app-root />
512+
</body>
513+
</html>'
514+
`,
515+
});
516+
517+
expect(errors.length).toEqual(1);
518+
expect(errors).toEqual([`Invalid self-closing element in index HTML file: '<app-root />'.`]);
519+
});
520+
521+
it('should not return an error when used on a supported element', async () => {
522+
const { errors } = await augmentIndexHtml({
523+
...indexGeneratorOptions,
524+
html: `
525+
<html>
526+
<body>
527+
<br />
528+
<app-root><app-root>
529+
</body>
530+
</html>'
531+
`,
532+
});
533+
534+
expect(errors.length).toEqual(0);
535+
});
536+
});
503537
});

0 commit comments

Comments
 (0)
Please sign in to comment.