Skip to content

Commit

Permalink
Merge pull request #705 from prettier/whitespace-preserve
Browse files Browse the repository at this point in the history
xml:space="preserve" option
  • Loading branch information
kddnewton authored Aug 8, 2023
2 parents 9d104db + c4f0687 commit d1e848a
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a

## [Unreleased]

### Added

- Respect `xml:space="preserve"` as an override to the `xmlWhitespaceSensitivity` option.

## [3.1.1] - 2023-07-14

### Changed
Expand Down
42 changes: 35 additions & 7 deletions src/printer.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,41 @@ function hasIgnoreRanges(comments) {
return false;
}

function isWhitespaceIgnorable(node) {
const { CData, Comment, reference } = node.children;
function isWhitespaceIgnorable(opts, attributes, content) {
// If the whitespace sensitivity setting is "strict", then we can't ignore the
// whitespace.
if (opts.xmlWhitespaceSensitivity === "strict") {
return false;
}

// If there is an xml:space attribute set to "preserve", then we can't ignore
// the whitespace.
if (
attributes &&
attributes.some(
(attribute) =>
attribute &&
attribute.children.Name[0].image === "xml:space" &&
attribute.children.STRING[0].image.slice(1, -1) === "preserve"
)
) {
return false;
}

return !CData && !reference && !hasIgnoreRanges(Comment);
// If there are character data or reference nodes in the content, then we
// can't ignore the whitespace.
if (content.children.CData || content.children.reference) {
return false;
}

// If there are comments in the content and the comments are ignore ranges,
// then we can't ignore the whitespace.
if (hasIgnoreRanges(content.children.Comment)) {
return false;
}

// Otherwise we can.
return true;
}

function printIToken(path) {
Expand Down Expand Up @@ -451,10 +482,7 @@ function printElement(path, opts, print) {
END[0].image
]);

if (
opts.xmlWhitespaceSensitivity !== "strict" &&
isWhitespaceIgnorable(content[0])
) {
if (isWhitespaceIgnorable(opts, attribute, content[0])) {
const fragments = path.call(
(childrenPath) => printElementFragments(childrenPath, opts, print),
"children",
Expand Down
40 changes: 40 additions & 0 deletions test/__snapshots__/format.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ use {
<content />
</div>
<div xml:space="preserve">
<content />
</div>
<div
verylongattribute="verylongvalue"
anotherverylongattribute="anotherverylongvalue" />
Expand Down Expand Up @@ -203,6 +207,10 @@ use {
<content/>
</div>
<div xml:space="preserve">
<content/>
</div>
<div
verylongattribute="verylongvalue"
anotherverylongattribute="anotherverylongvalue"/>
Expand Down Expand Up @@ -319,6 +327,10 @@ use {
<content />
</div>
<div xml:space="preserve">
<content />
</div>
<div
verylongattribute="verylongvalue"
anotherverylongattribute="anotherverylongvalue"
Expand Down Expand Up @@ -459,6 +471,10 @@ use {
<content />
</div>
<div xml:space="preserve">
<content />
</div>
<div
verylongattribute="verylongvalue"
anotherverylongattribute="anotherverylongvalue"
Expand Down Expand Up @@ -588,6 +604,10 @@ use {
<content />
</div>
<div xml:space="preserve">
<content />
</div>
<div
verylongattribute="verylongvalue"
anotherverylongattribute="anotherverylongvalue"
Expand Down Expand Up @@ -707,6 +727,10 @@ use {
<content />
</div>
<div xml:space="preserve">
<content />
</div>
<div
verylongattribute="verylongvalue"
anotherverylongattribute="anotherverylongvalue"
Expand Down Expand Up @@ -826,6 +850,10 @@ use {
<content />
</div>
<div xml:space='preserve'>
<content />
</div>
<div
verylongattribute='verylongvalue'
anotherverylongattribute='anotherverylongvalue'
Expand Down Expand Up @@ -954,6 +982,10 @@ use {
<content/>
</div>
<div xml:space="preserve">
<content/>
</div>
<div
verylongattribute="verylongvalue"
anotherverylongattribute="anotherverylongvalue"
Expand Down Expand Up @@ -1082,6 +1114,10 @@ use {
<content />
</div>
<div xml:space="preserve">
<content />
</div>
<div
verylongattribute="verylongvalue"
anotherverylongattribute="anotherverylongvalue"
Expand Down Expand Up @@ -1203,6 +1239,10 @@ use {
<content />
</div>
<div xml:space="preserve">
<content />
</div>
<div
verylongattribute="verylongvalue"
anotherverylongattribute="anotherverylongvalue"
Expand Down
4 changes: 4 additions & 0 deletions test/fixture.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@
<content />
</div>

<div xml:space="preserve">
<content />
</div>

<div verylongattribute="verylongvalue" anotherverylongattribute="anotherverylongvalue" />

<div attr1='singleQuotes' att2="doubleQuotes" />
Expand Down

0 comments on commit d1e848a

Please sign in to comment.