Skip to content

Commit

Permalink
Fix for collapseContent bug (#65)
Browse files Browse the repository at this point in the history
Fix for space being trimmed when using `collapseContent`
  • Loading branch information
RobertLevett authored Oct 23, 2023
1 parent eb037fe commit b7de974
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ function processElementNode(node: XmlParserElementNode, state: XMLFormatterState
if (child.content.includes('\n')) {
containsTextNodesWithLineBreaks = true;
child.content = child.content.trim();
} else if (index === 0 || index === nodeChildren.length - 1) {
} else if ((index === 0 || index === nodeChildren.length - 1) && !preserveSpace) {
if (child.content.trim().length === 0) {
// If the text node is at the start or end and is empty, it should be ignored when formatting
child.content = '';
Expand Down
1 change: 1 addition & 0 deletions test/data12/xml2-input.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?xml version="1.0" encoding="utf-8"?><root><content xml:space="preserve"><b><u>text1</u></b><b> </b><b><u>text2</u></b></content></root>
4 changes: 4 additions & 0 deletions test/data12/xml2-output.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<content xml:space="preserve"><b><u>text1</u></b><b> </b><b><u>text2</u></b></content>
</root>
4 changes: 4 additions & 0 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,5 +134,9 @@ describe('XML formatter', function () {
context('should collapse empty tags when forceSelfClosingEmptyTag=true', function () {
assertFormat('test/data15/xml*-input.xml', { forceSelfClosingEmptyTag: true });
});

context('should not remove space with style before differently stylised word when prettifying xml', function () {
assertFormat('test/data16/xml*-input.xml', { collapseContent: true });
});

});

0 comments on commit b7de974

Please sign in to comment.