Skip to content

Commit

Permalink
Angular: allow self-closing tags on custom elements (#14170)
Browse files Browse the repository at this point in the history
Fixes #14168
  • Loading branch information
fisker committed Jan 14, 2023
1 parent 06ea1ac commit 4ad9160
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 11 deletions.
18 changes: 18 additions & 0 deletions changelog_unreleased/angular/14170.md
@@ -0,0 +1,18 @@
#### Allow self-closing tags on custom elements (#14170 by @fisker)

See [Angular v15.1.0 release note](https://github.com/angular/angular/releases/tag/15.1.0) for details.

<!-- prettier-ignore -->
```html
// Input
<app-test/>

// Prettier stable
SyntaxError: Only void and foreign elements can be self closed "app-test" (1:1)
> 1 | <app-test/>
| ^^^^^^^^^
2 |

// Prettier main
<app-test />
```
22 changes: 11 additions & 11 deletions src/language-html/parser-html.js
Expand Up @@ -24,7 +24,7 @@ const { locStart, locEnd } = require("./loc.js");
* @typedef {import('angular-html-parser/lib/compiler/src/ml_parser/parser').ParseTreeResult} ParserTreeResult
* @typedef {Omit<import('angular-html-parser').ParseOptions, 'canSelfClose'> & {
* name?: 'html' | 'angular' | 'vue' | 'lwc';
* recognizeSelfClosing?: boolean;
* canSelfClose?: boolean;
* normalizeTagName?: boolean;
* normalizeAttributeName?: boolean;
* }} ParserOptions
Expand All @@ -42,7 +42,7 @@ const { locStart, locEnd } = require("./loc.js");
function ngHtmlParser(
input,
{
recognizeSelfClosing,
canSelfClose,
normalizeTagName,
normalizeAttributeName,
allowHtmComponentClosingTags,
Expand All @@ -64,7 +64,7 @@ function ngHtmlParser(
} = require("angular-html-parser/lib/compiler/src/ml_parser/html_tags");

let { rootNodes, errors } = parser.parse(input, {
canSelfClose: recognizeSelfClosing,
canSelfClose,
allowHtmComponentClosingTags,
isTagNameCaseSensitive,
getTagContentType,
Expand Down Expand Up @@ -97,7 +97,7 @@ function ngHtmlParser(
let secondParseResult;
const doSecondParse = () =>
parser.parse(input, {
canSelfClose: recognizeSelfClosing,
canSelfClose,
allowHtmComponentClosingTags,
isTagNameCaseSensitive,
});
Expand Down Expand Up @@ -135,13 +135,13 @@ function ngHtmlParser(
}
} else {
// If not Vue SFC, treat as html
recognizeSelfClosing = true;
canSelfClose = true;
normalizeTagName = true;
normalizeAttributeName = true;
allowHtmComponentClosingTags = true;
isTagNameCaseSensitive = false;
const htmlParseResult = parser.parse(input, {
canSelfClose: recognizeSelfClosing,
canSelfClose,
allowHtmComponentClosingTags,
isTagNameCaseSensitive,
});
Expand Down Expand Up @@ -373,7 +373,7 @@ function _parse(text, options, parserOptions, shouldParseFrontMatter = true) {
*/
function createParser({
name,
recognizeSelfClosing = false,
canSelfClose = false,
normalizeTagName = false,
normalizeAttributeName = false,
allowHtmComponentClosingTags = false,
Expand All @@ -386,7 +386,7 @@ function createParser({
text,
{ parser: name, ...options },
{
recognizeSelfClosing,
canSelfClose,
normalizeTagName,
normalizeAttributeName,
allowHtmComponentClosingTags,
Expand All @@ -405,15 +405,15 @@ module.exports = {
parsers: {
html: createParser({
name: "html",
recognizeSelfClosing: true,
canSelfClose: true,
normalizeTagName: true,
normalizeAttributeName: true,
allowHtmComponentClosingTags: true,
}),
angular: createParser({ name: "angular" }),
angular: createParser({ name: "angular", canSelfClose: true }),
vue: createParser({
name: "vue",
recognizeSelfClosing: true,
canSelfClose: true,
isTagNameCaseSensitive: true,
getTagContentType: (tagName, prefix, hasParent, attrs) => {
if (
Expand Down
36 changes: 36 additions & 0 deletions tests/format/angular/self-closing/__snapshots__/jsfmt.spec.js.snap
@@ -0,0 +1,36 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`self-closing.component.html format 1`] = `
====================================options=====================================
parsers: ["angular"]
printWidth: 80
| printWidth
=====================================input======================================
<app-test/>
<app-test />
<app-test
/>
<img>
<img/>
<img />
<img
/>
<div/>
<div />
<div
/>
=====================================output=====================================
<app-test />
<app-test />
<app-test />
<img />
<img />
<img />
<img />
<div />
<div />
<div />
================================================================================
`;
1 change: 1 addition & 0 deletions tests/format/angular/self-closing/jsfmt.spec.js
@@ -0,0 +1 @@
run_spec(__dirname, ["angular"]);
13 changes: 13 additions & 0 deletions tests/format/angular/self-closing/self-closing.component.html
@@ -0,0 +1,13 @@
<app-test/>
<app-test />
<app-test
/>
<img>
<img/>
<img />
<img
/>
<div/>
<div />
<div
/>

0 comments on commit 4ad9160

Please sign in to comment.