Skip to content

Commit 0f6815d

Browse files
authored
Merge pull request #300 from prettier/xml-self-closing-space
Bring back the xmlSelfClosingSpace option
2 parents 06023cd + 6b84c49 commit 0f6815d

File tree

7 files changed

+195
-2
lines changed

7 files changed

+195
-2
lines changed

.github/ISSUE_TEMPLATE/formatting.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ body:
2727
- "ignore"
2828
validations:
2929
required: true
30+
- type: dropdown
31+
attributes:
32+
label: xmlSelfClosingSpace
33+
description: What value do you have the `xmlSelfClosingSpace` option set to? (Defaults to `true`.)
34+
options:
35+
- "true"
36+
- "false"
3037
- type: textarea
3138
attributes:
3239
label: Input XML

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a
66

77
## [Unreleased]
88

9+
### Added
10+
11+
- Bring back the `xmlSelfClosingSpace` option.
12+
913
## [1.0.2] - 2021-07-17
1014

1115
### Changed

src/plugin.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ import printer from "./printer";
66

77
// These are the extra options defined by this plugin.
88
const options: Plugin["options"] = {
9+
xmlSelfClosingSpace: {
10+
type: "boolean",
11+
category: "XML",
12+
default: true,
13+
description: "Adds a space before self-closing tags.",
14+
since: "1.1.0"
15+
},
916
xmlWhitespaceSensitivity: {
1017
type: "choice",
1118
category: "XML",

src/printer.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ const printer: Printer = {
271271

272272
// Determine the value that will go between the <, name, and attributes
273273
// of an element and the /> of an element.
274-
const space: Doc = opts.bracketSameLine ? " " : line;
274+
const space: Doc = opts.xmlSelfClosingSpace ? line : softline;
275275

276276
if (SLASH_CLOSE) {
277277
return group([...parts, space, SLASH_CLOSE[0].image]);
@@ -446,7 +446,8 @@ const printer: Printer = {
446446
);
447447
}
448448

449-
return group([...parts, line, SPECIAL_CLOSE[0].image]);
449+
const space = opts.xmlSelfClosingSpace ? line : softline;
450+
return group([...parts, space, SPECIAL_CLOSE[0].image]);
450451
}
451452
}
452453
}

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ export type Embed = (
134134
// the options we defined in our plugin configuration.
135135
export type Options = Prettier.ParserOptions<any> & {
136136
bracketSameLine: boolean;
137+
xmlSelfClosingSpace: boolean;
137138
xmlWhitespaceSensitivity: "ignore" | "strict";
138139
};
139140

test/__snapshots__/format.test.ts.snap

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,82 @@ use {
7676
"
7777
`;
7878

79+
exports[`bracketSameLine => true, xmlSelfClosingSpace => false 1`] = `
80+
"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?>
81+
<!DOCTYPE module PUBLIC \\"-//Puppy Crawl//DTD Check Configuration 1.3//EN\\"
82+
\\"https://www.puppycrawl.com/dtds/configuration_1_3.dtd\\">
83+
<?xml-model href=\\"project.rnc\\" type=\\"application/relax-ng-compact-syntax\\"?>
84+
<!-- foo -->
85+
<svg
86+
xmlns=\\"http://www.w3.org/2000/svg\\"
87+
xmlns:xlink=\\"http://www.w3.org/1999/xlink\\"
88+
width=\\"200\\"
89+
height=\\"100\\"
90+
viewBox=\\"0 0 200 100\\">
91+
<title>Style inheritance and the use element</title>
92+
<desc _attr=\\"attr\\">
93+
&anp; &#12345;
94+
<![CDATA[
95+
foo
96+
]]>
97+
bar
98+
</desc>
99+
<?pagebreak?>
100+
101+
<style/>
102+
<style/>
103+
<style type=\\"text/css\\">
104+
circle {
105+
stroke-opacity: 0.7;
106+
}
107+
.special circle {
108+
stroke: green;
109+
}
110+
use {
111+
stroke: purple;
112+
fill: orange;
113+
}
114+
</style>
115+
116+
<yaml
117+
myveryveryveryverylongattributename=\\"myveryveryveryverylongattributevalue\\">
118+
- 1
119+
- 2
120+
- 3
121+
</style>
122+
123+
<!-- inner comment -->
124+
125+
<?pagebreak?>
126+
<g class=\\"special\\" style=\\"fill: blue\\">
127+
<circle id=\\"c\\" cy=\\"50\\" cx=\\"50\\" r=\\"40\\" stroke-width=\\"20\\"/>
128+
</g>
129+
<use xlink:href=\\"#c\\" x=\\"100\\"/>
130+
<ignored>
131+
<!-- prettier-ignore-start -->
132+
< ignored />
133+
<!-- prettier-ignore-end -->
134+
</ignored>
135+
<p>
136+
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed at est eget
137+
enim consectetur accumsan. Aliquam pretium sodales ipsum quis dignissim. Sed
138+
id sem vel diam luctus fringilla. Aliquam quis egestas magna. Curabitur
139+
molestie lorem et odio porta, et molestie libero laoreet. Morbi rhoncus
140+
sagittis cursus. Nullam vehicula pretium consequat. Praesent porta ante at
141+
posuere sollicitudin. Nullam commodo tempor arcu, at condimentum neque
142+
elementum ut.
143+
</p>
144+
<span>content</span>
145+
146+
<div>
147+
even more
148+
<content/>
149+
</div>
150+
</svg>
151+
<!-- bar -->
152+
"
153+
`;
154+
79155
exports[`defaults 1`] = `
80156
"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\" ?>
81157
<!DOCTYPE module PUBLIC \\"-//Puppy Crawl//DTD Check Configuration 1.3//EN\\"
@@ -151,6 +227,84 @@ use {
151227
"
152228
`;
153229

230+
exports[`xmlSelfClosingSpace => false 1`] = `
231+
"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?>
232+
<!DOCTYPE module PUBLIC \\"-//Puppy Crawl//DTD Check Configuration 1.3//EN\\"
233+
\\"https://www.puppycrawl.com/dtds/configuration_1_3.dtd\\">
234+
<?xml-model href=\\"project.rnc\\" type=\\"application/relax-ng-compact-syntax\\"?>
235+
<!-- foo -->
236+
<svg
237+
xmlns=\\"http://www.w3.org/2000/svg\\"
238+
xmlns:xlink=\\"http://www.w3.org/1999/xlink\\"
239+
width=\\"200\\"
240+
height=\\"100\\"
241+
viewBox=\\"0 0 200 100\\"
242+
>
243+
<title>Style inheritance and the use element</title>
244+
<desc _attr=\\"attr\\">
245+
&anp; &#12345;
246+
<![CDATA[
247+
foo
248+
]]>
249+
bar
250+
</desc>
251+
<?pagebreak?>
252+
253+
<style/>
254+
<style/>
255+
<style type=\\"text/css\\">
256+
circle {
257+
stroke-opacity: 0.7;
258+
}
259+
.special circle {
260+
stroke: green;
261+
}
262+
use {
263+
stroke: purple;
264+
fill: orange;
265+
}
266+
</style>
267+
268+
<yaml
269+
myveryveryveryverylongattributename=\\"myveryveryveryverylongattributevalue\\"
270+
>
271+
- 1
272+
- 2
273+
- 3
274+
</style>
275+
276+
<!-- inner comment -->
277+
278+
<?pagebreak?>
279+
<g class=\\"special\\" style=\\"fill: blue\\">
280+
<circle id=\\"c\\" cy=\\"50\\" cx=\\"50\\" r=\\"40\\" stroke-width=\\"20\\"/>
281+
</g>
282+
<use xlink:href=\\"#c\\" x=\\"100\\"/>
283+
<ignored>
284+
<!-- prettier-ignore-start -->
285+
< ignored />
286+
<!-- prettier-ignore-end -->
287+
</ignored>
288+
<p>
289+
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed at est eget
290+
enim consectetur accumsan. Aliquam pretium sodales ipsum quis dignissim. Sed
291+
id sem vel diam luctus fringilla. Aliquam quis egestas magna. Curabitur
292+
molestie lorem et odio porta, et molestie libero laoreet. Morbi rhoncus
293+
sagittis cursus. Nullam vehicula pretium consequat. Praesent porta ante at
294+
posuere sollicitudin. Nullam commodo tempor arcu, at condimentum neque
295+
elementum ut.
296+
</p>
297+
<span>content</span>
298+
299+
<div>
300+
even more
301+
<content/>
302+
</div>
303+
</svg>
304+
<!-- bar -->
305+
"
306+
`;
307+
154308
exports[`xmlWhitespaceSensitivity => ignore 1`] = `
155309
"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\" ?>
156310
<!DOCTYPE module PUBLIC \\"-//Puppy Crawl//DTD Check Configuration 1.3//EN\\"

test/format.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,22 @@ test("bracketSameLine => true", () => {
3333

3434
expect(formatted).toMatchSnapshot();
3535
});
36+
37+
test("xmlSelfClosingSpace => false", () => {
38+
const formatted = format(fixture, {
39+
xmlSelfClosingSpace: false,
40+
xmlWhitespaceSensitivity: "ignore"
41+
});
42+
43+
expect(formatted).toMatchSnapshot();
44+
});
45+
46+
test("bracketSameLine => true, xmlSelfClosingSpace => false", () => {
47+
const formatted = format(fixture, {
48+
bracketSameLine: true,
49+
xmlSelfClosingSpace: false,
50+
xmlWhitespaceSensitivity: "ignore"
51+
});
52+
53+
expect(formatted).toMatchSnapshot();
54+
});

0 commit comments

Comments
 (0)