Skip to content

Commit 4ad9160

Browse files
authored
Angular: allow self-closing tags on custom elements (#14170)
Fixes #14168
1 parent 06ea1ac commit 4ad9160

File tree

5 files changed

+79
-11
lines changed

5 files changed

+79
-11
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#### Allow self-closing tags on custom elements (#14170 by @fisker)
2+
3+
See [Angular v15.1.0 release note](https://github.com/angular/angular/releases/tag/15.1.0) for details.
4+
5+
<!-- prettier-ignore -->
6+
```html
7+
// Input
8+
<app-test/>
9+
10+
// Prettier stable
11+
SyntaxError: Only void and foreign elements can be self closed "app-test" (1:1)
12+
> 1 | <app-test/>
13+
| ^^^^^^^^^
14+
2 |
15+
16+
// Prettier main
17+
<app-test />
18+
```

src/language-html/parser-html.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const { locStart, locEnd } = require("./loc.js");
2424
* @typedef {import('angular-html-parser/lib/compiler/src/ml_parser/parser').ParseTreeResult} ParserTreeResult
2525
* @typedef {Omit<import('angular-html-parser').ParseOptions, 'canSelfClose'> & {
2626
* name?: 'html' | 'angular' | 'vue' | 'lwc';
27-
* recognizeSelfClosing?: boolean;
27+
* canSelfClose?: boolean;
2828
* normalizeTagName?: boolean;
2929
* normalizeAttributeName?: boolean;
3030
* }} ParserOptions
@@ -42,7 +42,7 @@ const { locStart, locEnd } = require("./loc.js");
4242
function ngHtmlParser(
4343
input,
4444
{
45-
recognizeSelfClosing,
45+
canSelfClose,
4646
normalizeTagName,
4747
normalizeAttributeName,
4848
allowHtmComponentClosingTags,
@@ -64,7 +64,7 @@ function ngHtmlParser(
6464
} = require("angular-html-parser/lib/compiler/src/ml_parser/html_tags");
6565

6666
let { rootNodes, errors } = parser.parse(input, {
67-
canSelfClose: recognizeSelfClosing,
67+
canSelfClose,
6868
allowHtmComponentClosingTags,
6969
isTagNameCaseSensitive,
7070
getTagContentType,
@@ -97,7 +97,7 @@ function ngHtmlParser(
9797
let secondParseResult;
9898
const doSecondParse = () =>
9999
parser.parse(input, {
100-
canSelfClose: recognizeSelfClosing,
100+
canSelfClose,
101101
allowHtmComponentClosingTags,
102102
isTagNameCaseSensitive,
103103
});
@@ -135,13 +135,13 @@ function ngHtmlParser(
135135
}
136136
} else {
137137
// If not Vue SFC, treat as html
138-
recognizeSelfClosing = true;
138+
canSelfClose = true;
139139
normalizeTagName = true;
140140
normalizeAttributeName = true;
141141
allowHtmComponentClosingTags = true;
142142
isTagNameCaseSensitive = false;
143143
const htmlParseResult = parser.parse(input, {
144-
canSelfClose: recognizeSelfClosing,
144+
canSelfClose,
145145
allowHtmComponentClosingTags,
146146
isTagNameCaseSensitive,
147147
});
@@ -373,7 +373,7 @@ function _parse(text, options, parserOptions, shouldParseFrontMatter = true) {
373373
*/
374374
function createParser({
375375
name,
376-
recognizeSelfClosing = false,
376+
canSelfClose = false,
377377
normalizeTagName = false,
378378
normalizeAttributeName = false,
379379
allowHtmComponentClosingTags = false,
@@ -386,7 +386,7 @@ function createParser({
386386
text,
387387
{ parser: name, ...options },
388388
{
389-
recognizeSelfClosing,
389+
canSelfClose,
390390
normalizeTagName,
391391
normalizeAttributeName,
392392
allowHtmComponentClosingTags,
@@ -405,15 +405,15 @@ module.exports = {
405405
parsers: {
406406
html: createParser({
407407
name: "html",
408-
recognizeSelfClosing: true,
408+
canSelfClose: true,
409409
normalizeTagName: true,
410410
normalizeAttributeName: true,
411411
allowHtmComponentClosingTags: true,
412412
}),
413-
angular: createParser({ name: "angular" }),
413+
angular: createParser({ name: "angular", canSelfClose: true }),
414414
vue: createParser({
415415
name: "vue",
416-
recognizeSelfClosing: true,
416+
canSelfClose: true,
417417
isTagNameCaseSensitive: true,
418418
getTagContentType: (tagName, prefix, hasParent, attrs) => {
419419
if (
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`self-closing.component.html format 1`] = `
4+
====================================options=====================================
5+
parsers: ["angular"]
6+
printWidth: 80
7+
| printWidth
8+
=====================================input======================================
9+
<app-test/>
10+
<app-test />
11+
<app-test
12+
/>
13+
<img>
14+
<img/>
15+
<img />
16+
<img
17+
/>
18+
<div/>
19+
<div />
20+
<div
21+
/>
22+
23+
=====================================output=====================================
24+
<app-test />
25+
<app-test />
26+
<app-test />
27+
<img />
28+
<img />
29+
<img />
30+
<img />
31+
<div />
32+
<div />
33+
<div />
34+
35+
================================================================================
36+
`;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
run_spec(__dirname, ["angular"]);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<app-test/>
2+
<app-test />
3+
<app-test
4+
/>
5+
<img>
6+
<img/>
7+
<img />
8+
<img
9+
/>
10+
<div/>
11+
<div />
12+
<div
13+
/>

0 commit comments

Comments
 (0)