Skip to content

Commit 442addd

Browse files
add option forceSelfClosingEmptyTag (#63)
Co-authored-by: Constantin Noll <[email protected]>
1 parent 64abc00 commit 442addd

File tree

6 files changed

+22
-1
lines changed

6 files changed

+22
-1
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ This can be a partial path (element tag name) or full path starting from the doc
5959
- `throwOnFailure`: Throw an error when XML fails to parse and get formatted otherwise the original XML is returned.
6060
- type: `boolean`
6161
- default: `true`
62+
- `forceSelfClosingEmptyTag`: True to force empty tags to be self-closing.
63+
- type: `boolean`
64+
- default: `false`
6265

6366
### Usage:
6467

src/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export type FormatOptions = {
55
collapseContent?: boolean;
66
lineSeparator?: string;
77
whiteSpaceAtEndOfSelfclosingTag?: boolean;
8+
forceSelfClosingEmptyTag?: boolean;
89
}
910

1011
declare function format(xml: string, options?: FormatOptions): string;

src/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ export type XMLFormatterOptions = {
5656
* True to throw an error when parsing XML document with invalid content like mismatched closing tags.
5757
*/
5858
strictMode?: boolean;
59+
60+
/**
61+
* True to force empty tags to be self-closing.
62+
*/
63+
forceSelfClosingEmptyTag?: boolean;
5964
};
6065

6166
export type XMLFormatterMinifyOptions = Omit<XMLFormatterOptions, 'lineSeparator'|'indentation'>;
@@ -132,7 +137,7 @@ function processElementNode(node: XmlParserElementNode, state: XMLFormatterState
132137
appendContent(state, '<' + node.name);
133138
processAttributes(state, node.attributes);
134139

135-
if (node.children === null) {
140+
if (node.children === null || (state.options.forceSelfClosingEmptyTag && node.children.length === 0)) {
136141
const selfClosingNodeClosingTag = state.options.whiteSpaceAtEndOfSelfclosingTag ? ' />' : '/>'
137142
// self-closing node
138143
appendContent(state, selfClosingNodeClosingTag);

test/data15/xml1-input.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<root><!-- test --><frag><p xml:space="preserve">This is <b>some</b> <b>some2</b> content</p></frag><empty></empty></root>

test/data15/xml1-output.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<root>
2+
<!-- test -->
3+
<frag>
4+
<p xml:space="preserve">This is <b>some</b> <b>some2</b> content</p>
5+
</frag>
6+
<empty/>
7+
</root>

test/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,4 +131,8 @@ describe('XML formatter', function () {
131131
});
132132
});
133133

134+
context('should collapse empty tags when forceSelfClosingEmptyTag=true', function () {
135+
assertFormat('test/data15/xml*-input.xml', { forceSelfClosingEmptyTag: true });
136+
});
137+
134138
});

0 commit comments

Comments
 (0)