Skip to content

Commit 310a5b1

Browse files
authored
Handlebars: Improve HTML void element check (#14110)
1 parent 62195dd commit 310a5b1

13 files changed

Lines changed: 523 additions & 62 deletions

File tree

cspell.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"Bekkelund",
3434
"Bento",
3535
"bfnrt",
36+
"bgsound",
3637
"binaryish",
3738
"bindon",
3839
"bitshift",

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@
5757
"html-element-attributes": "3.1.0",
5858
"html-styles": "1.0.0",
5959
"html-tag-names": "2.0.1",
60-
"html-void-elements": "2.0.1",
6160
"ignore": "5.2.0",
6261
"jest-docblock": "28.1.1",
6362
"json5": "2.2.2",

scripts/vendors/vendor-meta.json

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
"execa": "execa.js",
77
"html-element-attributes": "html-element-attributes.json",
88
"html-tag-names": "html-tag-names.json",
9-
"html-void-elements": "html-void-elements.json",
109
"leven": "leven.js",
1110
"mem": "mem.js",
1211
"sdbm": "sdbm.js",
@@ -396,29 +395,6 @@
396395
}
397396
]
398397
},
399-
{
400-
"name": "html-void-elements",
401-
"maintainers": [],
402-
"version": "2.0.1",
403-
"description": "List of HTML void tag names",
404-
"repository": "wooorm/html-void-elements",
405-
"homepage": null,
406-
"private": false,
407-
"license": "MIT",
408-
"licenseText": "(The MIT License)\n\nCopyright (c) 2016 Titus Wormer <tituswormer@gmail.com>\n\nPermission is hereby granted, free of charge, to any person obtaining\na copy of this software and associated documentation files (the\n'Software'), to deal in the Software without restriction, including\nwithout limitation the rights to use, copy, modify, merge, publish,\ndistribute, sublicense, and/or sell copies of the Software, and to\npermit persons to whom the Software is furnished to do so, subject to\nthe following conditions:\n\nThe above copyright notice and this permission notice shall be\nincluded in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,\nEXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\nMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.\nIN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY\nCLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,\nTORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE\nSOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n",
409-
"author": {
410-
"name": "Titus Wormer",
411-
"email": "tituswormer@gmail.com",
412-
"url": "https://wooorm.com"
413-
},
414-
"contributors": [
415-
{
416-
"name": "Titus Wormer",
417-
"email": "tituswormer@gmail.com",
418-
"url": "https://wooorm.com"
419-
}
420-
]
421-
},
422398
{
423399
"name": "leven",
424400
"maintainers": [],

scripts/vendors/vendors.mjs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ const vendors = [
55
"execa",
66
"html-element-attributes",
77
"html-tag-names",
8-
"html-void-elements",
98
"leven",
109
"mem",
1110
"sdbm",
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"use strict";
2+
3+
const {
4+
voidMap,
5+
} = require("@glimmer/syntax/dist/commonjs/es2017/lib/generation/printer.js");
6+
7+
module.exports = Object.keys(voidMap);

src/language-handlebars/utils.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"use strict";
22

3-
const { htmlVoidElements } = require("../../vendors/html-void-elements.json");
43
const getLast = require("../utils/get-last.js");
4+
const htmlVoidElements = require("./html-void-elements.evaluate.js");
55

66
function isLastNodeOfSiblings(path) {
77
const node = path.getValue();
@@ -38,10 +38,15 @@ function isGlimmerComponent(node) {
3838
}
3939

4040
const voidTags = new Set(htmlVoidElements);
41+
// https://github.com/glimmerjs/glimmer-vm/blob/ec5648f3895b9ab8d085523be001553746221449/packages/%40glimmer/syntax/lib/generation/printer.ts#L44-L46
42+
function isVoidTag(tag) {
43+
return voidTags.has(tag.toLowerCase()) && !isUppercase(tag[0]);
44+
}
45+
4146
function isVoid(node) {
4247
return (
43-
voidTags.has(node.tag) ||
4448
node.selfClosing === true ||
49+
isVoidTag(node.tag) ||
4550
(isGlimmerComponent(node) &&
4651
node.children.every((node) => isWhitespaceNode(node)))
4752
);

tests/format/handlebars/element-node/__snapshots__/jsfmt.spec.js.snap

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,102 @@ printWidth: 80
111111
<Component />
112112
================================================================================
113113
`;
114+
115+
exports[`snippet: basefont format 1`] = `
116+
====================================options=====================================
117+
parsers: ["glimmer"]
118+
printWidth: 80
119+
| printWidth
120+
=====================================input======================================
121+
<basefont> text </basefont>
122+
=====================================output=====================================
123+
<basefont> text </basefont>
124+
================================================================================
125+
`;
126+
127+
exports[`snippet: bgsound format 1`] = `
128+
====================================options=====================================
129+
parsers: ["glimmer"]
130+
printWidth: 80
131+
| printWidth
132+
=====================================input======================================
133+
<bgsound> text </bgsound>
134+
=====================================output=====================================
135+
<bgsound> text </bgsound>
136+
================================================================================
137+
`;
138+
139+
exports[`snippet: frame format 1`] = `
140+
====================================options=====================================
141+
parsers: ["glimmer"]
142+
printWidth: 80
143+
| printWidth
144+
=====================================input======================================
145+
<frame> text </frame>
146+
=====================================output=====================================
147+
<frame> text </frame>
148+
================================================================================
149+
`;
150+
151+
exports[`snippet: image format 1`] = `
152+
====================================options=====================================
153+
parsers: ["glimmer"]
154+
printWidth: 80
155+
| printWidth
156+
=====================================input======================================
157+
<image> text </image>
158+
=====================================output=====================================
159+
<image> text </image>
160+
================================================================================
161+
`;
162+
163+
exports[`snippet: isindex format 1`] = `
164+
====================================options=====================================
165+
parsers: ["glimmer"]
166+
printWidth: 80
167+
| printWidth
168+
=====================================input======================================
169+
<isindex> text </isindex>
170+
=====================================output=====================================
171+
<isindex> text </isindex>
172+
================================================================================
173+
`;
174+
175+
exports[`snippet: menuitem format 1`] = `
176+
====================================options=====================================
177+
parsers: ["glimmer"]
178+
printWidth: 80
179+
| printWidth
180+
=====================================input======================================
181+
<menuitem> text </menuitem>
182+
=====================================output=====================================
183+
<menuitem> text </menuitem>
184+
================================================================================
185+
`;
186+
187+
exports[`snippet: nextid format 1`] = `
188+
====================================options=====================================
189+
parsers: ["glimmer"]
190+
printWidth: 80
191+
| printWidth
192+
=====================================input======================================
193+
<nextid> text </nextid>
194+
=====================================output=====================================
195+
<nextid> text </nextid>
196+
================================================================================
197+
`;
198+
199+
exports[`void-elements.hbs format 1`] = `
200+
====================================options=====================================
201+
parsers: ["glimmer"]
202+
printWidth: 80
203+
| printWidth
204+
=====================================input======================================
205+
<img>
206+
<input>
207+
208+
=====================================output=====================================
209+
<img />
210+
<input />
211+
================================================================================
212+
`;
Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,22 @@
1-
run_spec(__dirname, ["glimmer"]);
1+
run_spec(
2+
{
3+
dirname: __dirname,
4+
/*
5+
Missed HTML void tags in glimmer parser
6+
https://github.com/glimmerjs/glimmer-vm/blob/ec5648f3895b9ab8d085523be001553746221449/packages/%40glimmer/syntax/lib/parser/tokenizer-event-handlers.ts#LL7C13-L7C13
7+
https://github.com/glimmerjs/glimmer-vm/blob/ec5648f3895b9ab8d085523be001553746221449/packages/%40glimmer/syntax/lib/generation/printer.ts#L8-L9
8+
Please move tags to `tests/format/misc/errors/handlebars/jsfmt.spec.js`
9+
when removing from this list
10+
*/
11+
snippets: [
12+
"basefont",
13+
"bgsound",
14+
"frame",
15+
"image",
16+
"isindex",
17+
"menuitem",
18+
"nextid",
19+
].map((tag) => ({ name: tag, code: `<${tag}> text </${tag}>` })),
20+
},
21+
["glimmer"]
22+
);
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<img>
2+
<input>

0 commit comments

Comments
 (0)