Skip to content

Commit

Permalink
Fix for styled space being removed
Browse files Browse the repository at this point in the history
Fix for styled space being trimmed when the following word is styled differently, even when within a preserved parent.
  • Loading branch information
RobertLevett committed Oct 23, 2023
1 parent 655f9f7 commit 00a149f
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 00a149f

Please sign in to comment.