Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ This can be a partial path (element tag name) or full path starting from the doc
- `throwOnFailure`: Throw an error when XML fails to parse and get formatted otherwise the original XML is returned.
- type: `boolean`
- default: `true`
- `forceSelfClosingEmptyTag`: True to force empty tags to be self-closing.
- type: `boolean`
- default: `false`

### Usage:

Expand Down
1 change: 1 addition & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export type FormatOptions = {
collapseContent?: boolean;
lineSeparator?: string;
whiteSpaceAtEndOfSelfclosingTag?: boolean;
forceSelfClosingEmptyTag?: boolean;
}

declare function format(xml: string, options?: FormatOptions): string;
Expand Down
7 changes: 6 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ export type XMLFormatterOptions = {
* True to throw an error when parsing XML document with invalid content like mismatched closing tags.
*/
strictMode?: boolean;

/**
* True to force empty tags to be self-closing.
*/
forceSelfClosingEmptyTag?: boolean;
};

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

if (node.children === null) {
if (node.children === null || (state.options.forceSelfClosingEmptyTag && node.children.length === 0)) {
const selfClosingNodeClosingTag = state.options.whiteSpaceAtEndOfSelfclosingTag ? ' />' : '/>'
// self-closing node
appendContent(state, selfClosingNodeClosingTag);
Expand Down
1 change: 1 addition & 0 deletions test/data15/xml1-input.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<root><!-- test --><frag><p xml:space="preserve">This is <b>some</b> <b>some2</b> content</p></frag><empty></empty></root>
7 changes: 7 additions & 0 deletions test/data15/xml1-output.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<root>
<!-- test -->
<frag>
<p xml:space="preserve">This is <b>some</b> <b>some2</b> content</p>
</frag>
<empty/>
</root>
4 changes: 4 additions & 0 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,8 @@ describe('XML formatter', function () {
});
});

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

});