From 803349aeff27d0e6ba178e80d219182058fada0c Mon Sep 17 00:00:00 2001 From: Szymon Gebler Date: Sat, 1 Nov 2025 14:49:21 +0100 Subject: [PATCH 1/2] Only treat space, `\t`, `\n`, and `\r` as whitespace The `\s` character class in RegExp includes several other characters such as `\v`, `\f`, or various space variants. However, in XML only 4 characters are considered whitespace: ` ` (space), `\t`, `\n`, `\r`. This commit adjusts the pattern so that only these four are matched. It also adds whitespace characters to the formatting fixture to verify that they are formatted properly. The first two paragraphs contain tab, space, and newline characters. The carriage return character was omitted, since it's quite rare and most text editors convert it to `\n` when saving a file, so it'd likely be replaced once the fixture is edited again. The third paragraph contains all other whitespace characters that the `\s` character class includes. --- src/printer.js | 2 +- test/__snapshots__/format.test.js.snap | 121 ++++++++++++++++++++++++- test/fixture.xml | 7 ++ 3 files changed, 128 insertions(+), 2 deletions(-) diff --git a/src/printer.js b/src/printer.js index aca9ef4c..163b41f8 100644 --- a/src/printer.js +++ b/src/printer.js @@ -284,7 +284,7 @@ function printCharDataIgnore(path) { return; } - const content = chardata.TEXT.replaceAll(/^[\t\n\r\s]+|[\t\n\r\s]+$/g, ""); + const content = chardata.TEXT.replaceAll(/^[\t\n\r ]+|[\t\n\r ]+$/g, ""); const printed = group( content.split(/(\n)/g).map((value) => { if (value === "\n") { diff --git a/test/__snapshots__/format.test.js.snap b/test/__snapshots__/format.test.js.snap index 130388bb..1549485e 100644 --- a/test/__snapshots__/format.test.js.snap +++ b/test/__snapshots__/format.test.js.snap @@ -1,4 +1,4 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP +// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing exports[`bracketSameLine => true 1`] = ` " @@ -126,6 +126,20 @@ use { slide + + + + + + + + + + + + +              

    + " @@ -257,6 +271,20 @@ use { slide + + + + + + + + + + + + +              

    + " @@ -384,6 +412,13 @@ use { slide + + + + + + +              

    " @@ -548,6 +583,20 @@ use { slide + + + + + + + + + + + + +              

    + " @@ -675,6 +724,13 @@ use { slide + + + + + + +              

    " @@ -802,6 +858,13 @@ use { slide + + + + + + +              

    " @@ -929,6 +992,13 @@ use { slide + + + + + + +              

    " @@ -1065,6 +1135,20 @@ use { slide + + + + + + + + + + + + +              

    + " @@ -1192,6 +1276,13 @@ use { slide + + + + + + +              

    " @@ -1328,6 +1419,20 @@ use { slide + + + + + + + + + + + + +              

    + " @@ -1457,6 +1562,20 @@ use { slide + + + + + + + + + + + + +              

    + " diff --git a/test/fixture.xml b/test/fixture.xml index d535eb8b..0d53f6c3 100644 --- a/test/fixture.xml +++ b/test/fixture.xml @@ -106,5 +106,12 @@ slide + + + + + + +              

    From 3598f1f160913d13d9c2135da866f09dc9937e2f Mon Sep 17 00:00:00 2001 From: Szymon Gebler Date: Sat, 1 Nov 2025 15:06:44 +0100 Subject: [PATCH 2/2] Remove `\v` and `\f` from the fixture These are not valid XML characters so it doesn't make sense to test them. --- test/__snapshots__/format.test.js.snap | 22 +++++++++++----------- test/fixture.xml | 2 +- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/test/__snapshots__/format.test.js.snap b/test/__snapshots__/format.test.js.snap index 1549485e..468afee4 100644 --- a/test/__snapshots__/format.test.js.snap +++ b/test/__snapshots__/format.test.js.snap @@ -138,7 +138,7 @@ use { -              

    +              

    @@ -283,7 +283,7 @@ use { -              

    +              

    @@ -418,7 +418,7 @@ use { -              

    +              

    " @@ -595,7 +595,7 @@ use { -              

    +              

    @@ -730,7 +730,7 @@ use { -              

    +              

    " @@ -864,7 +864,7 @@ use { -              

    +              

    " @@ -998,7 +998,7 @@ use { -              

    +              

    " @@ -1147,7 +1147,7 @@ use { -              

    +              

    @@ -1282,7 +1282,7 @@ use { -              

    +              

    " @@ -1431,7 +1431,7 @@ use { -              

    +              

    @@ -1574,7 +1574,7 @@ use { -              

    +              

    diff --git a/test/fixture.xml b/test/fixture.xml index 0d53f6c3..2735da9b 100644 --- a/test/fixture.xml +++ b/test/fixture.xml @@ -112,6 +112,6 @@ -              

    +