Skip to content

Commit

Permalink
Add option dedentHtmlTagChildren to dedent children of <html> tag…
Browse files Browse the repository at this point in the history
… for HTML files
  • Loading branch information
thachnn committed Sep 14, 2022
1 parent 00ec91b commit 7c4c31d
Show file tree
Hide file tree
Showing 12 changed files with 132 additions and 228 deletions.
7 changes: 7 additions & 0 deletions src/language-html/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,11 @@ module.exports = {
default: false,
description: "Indent script and style tags in Vue files.",
},
dedentHtmlTagChildren: {
since: "2.0.5",
category: CATEGORY_HTML,
type: "boolean",
default: false,
description: "Dedent children of html tag in HTML files.",
},
};
9 changes: 9 additions & 0 deletions src/language-html/print/element.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,15 @@ function printElement(path, options, print) {
) {
return childrenDoc;
}
if (
node.parent.type === "root" &&
node.type === "element" &&
node.fullName.toLowerCase() === "html" &&
options.parser === "html" &&
options.dedentHtmlTagChildren
) {
return dedentToRoot(childrenDoc);
}
return indent(childrenDoc);
};

Expand Down
53 changes: 53 additions & 0 deletions tests/format/html/indent/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`simple.html - {"dedentHtmlTagChildren":false} format 1`] = `
====================================options=====================================
dedentHtmlTagChildren: false
parsers: ["html"]
printWidth: 80
| printWidth
=====================================input======================================
<!DOCTYPE html><html>
<head><title>Sample page</title></head><body>
<h1>Sample page</h1><p>This page is just a demo.</p></body></html>
=====================================output=====================================
<!DOCTYPE html>
<html>
<head>
<title>Sample page</title>
</head>
<body>
<h1>Sample page</h1>
<p>This page is just a demo.</p>
</body>
</html>
================================================================================
`;

exports[`simple.html - {"dedentHtmlTagChildren":true} format 1`] = `
====================================options=====================================
dedentHtmlTagChildren: true
parsers: ["html"]
printWidth: 80
| printWidth
=====================================input======================================
<!DOCTYPE html><html>
<head><title>Sample page</title></head><body>
<h1>Sample page</h1><p>This page is just a demo.</p></body></html>
=====================================output=====================================
<!DOCTYPE html>
<html>
<head>
<title>Sample page</title>
</head>
<body>
<h1>Sample page</h1>
<p>This page is just a demo.</p>
</body>
</html>
================================================================================
`;
2 changes: 2 additions & 0 deletions tests/format/html/indent/jsfmt.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
run_spec(__dirname, ["html"], { dedentHtmlTagChildren: true });
run_spec(__dirname, ["html"], { dedentHtmlTagChildren: false });
3 changes: 3 additions & 0 deletions tests/format/html/indent/simple.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<!DOCTYPE html><html>
<head><title>Sample page</title></head><body>
<h1>Sample page</h1><p>This page is just a demo.</p></body></html>
6 changes: 6 additions & 0 deletions tests/integration/__tests__/__snapshots__/early-exit.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ Format options:
--bracket-same-line Put > of opening tags on the last line instead of on a new line.
Defaults to false.
--no-bracket-spacing Do not print spaces between brackets.
--dedent-html-tag-children
Dedent children of html tag in HTML files.
Defaults to false.
--embedded-language-formatting <auto|off>
Control how Prettier formats quoted code embedded in the file.
Defaults to auto.
Expand Down Expand Up @@ -239,6 +242,9 @@ Format options:
--bracket-same-line Put > of opening tags on the last line instead of on a new line.
Defaults to false.
--no-bracket-spacing Do not print spaces between brackets.
--dedent-html-tag-children
Dedent children of html tag in HTML files.
Defaults to false.
--embedded-language-formatting <auto|off>
Control how Prettier formats quoted code embedded in the file.
Defaults to auto.
Expand Down
13 changes: 13 additions & 0 deletions tests/integration/__tests__/__snapshots__/help-options.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,19 @@ Default: -1
exports[`show detailed usage with --help cursor-offset (write) 1`] = `[]`;
exports[`show detailed usage with --help dedent-html-tag-children (stderr) 1`] = `""`;
exports[`show detailed usage with --help dedent-html-tag-children (stdout) 1`] = `
"--dedent-html-tag-children
Dedent children of html tag in HTML files.
Default: false
"
`;
exports[`show detailed usage with --help dedent-html-tag-children (write) 1`] = `[]`;
exports[`show detailed usage with --help editorconfig (stderr) 1`] = `""`;
exports[`show detailed usage with --help editorconfig (stdout) 1`] = `
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ exports[`show external options with \`--help\` 1`] = `
- First value
+ Second value
@@ -22,16 +22,18 @@
@@ -25,16 +25,18 @@
Control how Prettier formats quoted code embedded in the file.
Defaults to auto.
--end-of-line <lf|crlf|cr|auto>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ exports[`show external options with \`--help\` 1`] = `
- First value
+ Second value
@@ -22,16 +22,18 @@
@@ -25,16 +25,18 @@
Control how Prettier formats quoted code embedded in the file.
Defaults to auto.
--end-of-line <lf|crlf|cr|auto>
Expand Down
5 changes: 5 additions & 0 deletions tests/integration/__tests__/__snapshots__/schema.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ exports[`schema 1`] = `
This option cannot be used with --range-start and --range-end.",
"type": "integer",
},
"dedentHtmlTagChildren": {
"default": false,
"description": "Dedent children of html tag in HTML files.",
"type": "boolean",
},
"embeddedLanguageFormatting": {
"default": "auto",
"description": "Control how Prettier formats quoted code embedded in the file.",
Expand Down
13 changes: 13 additions & 0 deletions tests/integration/__tests__/__snapshots__/support-info.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ exports[`API getSupportInfo() 1`] = `
},
"type": "int",
},
"dedentHtmlTagChildren": {
"default": false,
"type": "boolean",
},
"embeddedLanguageFormatting": {
"choices": [
"auto",
Expand Down Expand Up @@ -835,6 +839,15 @@ exports[`CLI --support-info (stdout) 1`] = `
"since": "1.4.0",
"type": "int"
},
{
"category": "HTML",
"default": false,
"description": "Dedent children of html tag in HTML files.",
"name": "dedentHtmlTagChildren",
"pluginDefaults": {},
"since": "2.0.5",
"type": "boolean"
},
{
"category": "Global",
"choices": [
Expand Down

0 comments on commit 7c4c31d

Please sign in to comment.