Skip to content
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

Handlebars: Improve HTML void element check #14110

Merged
merged 8 commits into from Jan 22, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions cspell.json
Expand Up @@ -33,6 +33,7 @@
"Bekkelund",
"Bento",
"bfnrt",
"bgsound",
"binaryish",
"bindon",
"bitshift",
Expand Down
1 change: 0 additions & 1 deletion package.json
Expand Up @@ -57,7 +57,6 @@
"html-element-attributes": "3.1.0",
"html-styles": "1.0.0",
"html-tag-names": "2.0.1",
"html-void-elements": "2.0.1",
"ignore": "5.2.0",
"jest-docblock": "28.1.1",
"json5": "2.2.2",
Expand Down
24 changes: 0 additions & 24 deletions scripts/vendors/vendor-meta.json
Expand Up @@ -6,7 +6,6 @@
"execa": "execa.js",
"html-element-attributes": "html-element-attributes.json",
"html-tag-names": "html-tag-names.json",
"html-void-elements": "html-void-elements.json",
"leven": "leven.js",
"mem": "mem.js",
"sdbm": "sdbm.js",
Expand Down Expand Up @@ -396,29 +395,6 @@
}
]
},
{
"name": "html-void-elements",
"maintainers": [],
"version": "2.0.1",
"description": "List of HTML void tag names",
"repository": "wooorm/html-void-elements",
"homepage": null,
"private": false,
"license": "MIT",
"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",
"author": {
"name": "Titus Wormer",
"email": "tituswormer@gmail.com",
"url": "https://wooorm.com"
},
"contributors": [
{
"name": "Titus Wormer",
"email": "tituswormer@gmail.com",
"url": "https://wooorm.com"
}
]
},
{
"name": "leven",
"maintainers": [],
Expand Down
1 change: 0 additions & 1 deletion scripts/vendors/vendors.mjs
Expand Up @@ -5,7 +5,6 @@ const vendors = [
"execa",
"html-element-attributes",
"html-tag-names",
"html-void-elements",
"leven",
"mem",
"sdbm",
Expand Down
7 changes: 7 additions & 0 deletions src/language-handlebars/html-void-elements.evaluate.js
@@ -0,0 +1,7 @@
"use strict";

const {
voidMap,
} = require("@glimmer/syntax/dist/commonjs/es2017/lib/generation/printer.js");

module.exports = Object.keys(voidMap);
9 changes: 7 additions & 2 deletions src/language-handlebars/utils.js
@@ -1,7 +1,7 @@
"use strict";

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

function isLastNodeOfSiblings(path) {
const node = path.getValue();
Expand Down Expand Up @@ -38,10 +38,15 @@ function isGlimmerComponent(node) {
}

const voidTags = new Set(htmlVoidElements);
// https://github.com/glimmerjs/glimmer-vm/blob/ec5648f3895b9ab8d085523be001553746221449/packages/%40glimmer/syntax/lib/generation/printer.ts#L44-L46
function isVoidTag(tag) {
return voidTags.has(tag.toLowerCase()) && !isUppercase(tag[0]);
}

function isVoid(node) {
return (
voidTags.has(node.tag) ||
node.selfClosing === true ||
isVoidTag(node.tag) ||
(isGlimmerComponent(node) &&
node.children.every((node) => isWhitespaceNode(node)))
);
Expand Down
Expand Up @@ -111,3 +111,102 @@ printWidth: 80
<Component />
================================================================================
`;

exports[`snippet: basefont format 1`] = `
====================================options=====================================
parsers: ["glimmer"]
printWidth: 80
| printWidth
=====================================input======================================
<basefont> text </basefont>
=====================================output=====================================
<basefont> text </basefont>
================================================================================
`;

exports[`snippet: bgsound format 1`] = `
====================================options=====================================
parsers: ["glimmer"]
printWidth: 80
| printWidth
=====================================input======================================
<bgsound> text </bgsound>
=====================================output=====================================
<bgsound> text </bgsound>
================================================================================
`;

exports[`snippet: frame format 1`] = `
====================================options=====================================
parsers: ["glimmer"]
printWidth: 80
| printWidth
=====================================input======================================
<frame> text </frame>
=====================================output=====================================
<frame> text </frame>
================================================================================
`;

exports[`snippet: image format 1`] = `
====================================options=====================================
parsers: ["glimmer"]
printWidth: 80
| printWidth
=====================================input======================================
<image> text </image>
=====================================output=====================================
<image> text </image>
================================================================================
`;

exports[`snippet: isindex format 1`] = `
====================================options=====================================
parsers: ["glimmer"]
printWidth: 80
| printWidth
=====================================input======================================
<isindex> text </isindex>
=====================================output=====================================
<isindex> text </isindex>
================================================================================
`;

exports[`snippet: menuitem format 1`] = `
====================================options=====================================
parsers: ["glimmer"]
printWidth: 80
| printWidth
=====================================input======================================
<menuitem> text </menuitem>
=====================================output=====================================
<menuitem> text </menuitem>
================================================================================
`;

exports[`snippet: nextid format 1`] = `
====================================options=====================================
parsers: ["glimmer"]
printWidth: 80
| printWidth
=====================================input======================================
<nextid> text </nextid>
=====================================output=====================================
<nextid> text </nextid>
================================================================================
`;

exports[`void-elements.hbs format 1`] = `
====================================options=====================================
parsers: ["glimmer"]
printWidth: 80
| printWidth
=====================================input======================================
<img>
<input>

=====================================output=====================================
<img />
<input />
================================================================================
`;
23 changes: 22 additions & 1 deletion tests/format/handlebars/element-node/jsfmt.spec.js
@@ -1 +1,22 @@
run_spec(__dirname, ["glimmer"]);
run_spec(
{
dirname: __dirname,
/*
Missed HTML void tags in glimmer parser
https://github.com/glimmerjs/glimmer-vm/blob/ec5648f3895b9ab8d085523be001553746221449/packages/%40glimmer/syntax/lib/parser/tokenizer-event-handlers.ts#LL7C13-L7C13
https://github.com/glimmerjs/glimmer-vm/blob/ec5648f3895b9ab8d085523be001553746221449/packages/%40glimmer/syntax/lib/generation/printer.ts#L8-L9
Please move tags to `tests/format/misc/errors/handlebars/jsfmt.spec.js`
when removing from this list
*/
snippets: [
"basefont",
"bgsound",
"frame",
"image",
"isindex",
"menuitem",
"nextid",
].map((tag) => ({ name: tag, code: `<${tag}> text </${tag}>` })),
},
["glimmer"]
);
2 changes: 2 additions & 0 deletions tests/format/handlebars/element-node/void-elements.hbs
@@ -0,0 +1,2 @@
<img>
<input>