@prettier/plugin-xml
Advanced tools
Comparing version 0.5.0 to 0.6.0
@@ -9,2 +9,8 @@ # Changelog | ||
## [0.6.0] - 2020-01-27 | ||
### Added | ||
- The `xmlWhitespaceSensitivity` option, with current valid values of `"strict"` and `"ignore"`. `"strict"` behavior maintains the current behavior, while `"ignore"` allows the plugin more freedom in where to place nodes. | ||
## [0.5.0] - 2020-01-21 | ||
@@ -49,3 +55,4 @@ | ||
[unreleased]: https://github.com/prettier/plugin-xml/compare/v0.5.0...HEAD | ||
[unreleased]: https://github.com/prettier/plugin-xml/compare/v0.6.0...HEAD | ||
[0.6.0]: https://github.com/prettier/plugin-xml/compare/v0.5.0...v0.6.0 | ||
[0.5.0]: https://github.com/prettier/plugin-xml/compare/v0.4.0...v0.5.0 | ||
@@ -52,0 +59,0 @@ [0.4.0]: https://github.com/prettier/plugin-xml/compare/v0.3.0...v0.4.0 |
{ | ||
"name": "@prettier/plugin-xml", | ||
"version": "0.5.0", | ||
"version": "0.6.0", | ||
"description": "prettier plugin for XML", | ||
@@ -30,3 +30,3 @@ "main": "src/plugin.js", | ||
"eslint-plugin-import": "^2.16.0", | ||
"jest": "^24.0.0" | ||
"jest": "^25.1.0" | ||
}, | ||
@@ -33,0 +33,0 @@ "jest": { |
@@ -49,7 +49,8 @@ <h1 align="center">Prettier for XML</h1> | ||
| Name | Default | Description | | ||
| --------------------- | :-----: | ------------------------------------------------------------------------------------------------ | | ||
| `printWidth` | `80` | Same as in Prettier ([see prettier docs](https://prettier.io/docs/en/options.html#print-width)). | | ||
| `tabWidth` | `2` | Same as in Prettier ([see prettier docs](https://prettier.io/docs/en/options.html#tab-width)). | | ||
| `xmlSelfClosingSpace` | `true` | Adds a space before self-closing tags. | | ||
| Name | Default | Description | | ||
| -------------------------- | :--------: | ------------------------------------------------------------------------------------------------ | | ||
| `printWidth` | `80` | Same as in Prettier ([see prettier docs](https://prettier.io/docs/en/options.html#print-width)). | | ||
| `tabWidth` | `2` | Same as in Prettier ([see prettier docs](https://prettier.io/docs/en/options.html#tab-width)). | | ||
| `xmlSelfClosingSpace` | `true` | Adds a space before self-closing tags. | | ||
| `xmlWhitespaceSensitivity` | `"strict"` | How to handle whitespaces. Options are `"strict"` and `"ignore"`. | | ||
@@ -56,0 +57,0 @@ Any of these can be added to your existing [prettier configuration |
@@ -46,2 +46,18 @@ const parse = require("./parse"); | ||
description: "Adds a space before self-closing tags." | ||
}, | ||
xmlWhitespaceSensitivity: { | ||
type: "choice", | ||
category: "Global", | ||
default: "strict", | ||
description: "How to handle whitespaces in XML.", | ||
choices: [ | ||
{ | ||
value: "strict", | ||
description: "Whitespaces are considered sensitive in all elements." | ||
}, | ||
{ | ||
value: "ignore", | ||
description: "Whitespaces are considered insensitive in all elements." | ||
} | ||
] | ||
} | ||
@@ -48,0 +64,0 @@ }, |
134
src/print.js
@@ -12,2 +12,39 @@ const { | ||
const elementOnly = node => { | ||
const { CData, Comment, chardata, element, reference } = node.children; | ||
return ( | ||
!CData && | ||
!Comment && | ||
(!chardata || chardata.every(datum => !datum.children.TEXT)) && | ||
element && | ||
!reference | ||
); | ||
}; | ||
const printCData = node => ({ | ||
offset: node.startOffset, | ||
printed: node.image | ||
}); | ||
const printComment = node => ({ | ||
offset: node.startOffset, | ||
printed: node.image | ||
}); | ||
const printCharData = (path, print) => (node, index) => ({ | ||
offset: node.location.startOffset, | ||
printed: path.call(print, "children", "chardata", index) | ||
}); | ||
const printElement = (path, print) => (node, index) => ({ | ||
offset: node.location.startOffset, | ||
printed: path.call(print, "children", "element", index) | ||
}); | ||
const printReference = node => ({ | ||
offset: node.location.startOffset, | ||
printed: (node.children.CharRef || node.children.EntityRef)[0].image | ||
}); | ||
const nodes = { | ||
@@ -31,54 +68,21 @@ attribute: (path, _opts, _print) => { | ||
const { | ||
CData, | ||
Comment, | ||
chardata, | ||
element, | ||
reference | ||
CData = [], | ||
Comment = [], | ||
chardata = [], | ||
element = [], | ||
reference = [] | ||
} = path.getValue().children; | ||
let parts = []; | ||
if (CData) { | ||
CData.forEach(node => { | ||
parts.push({ offset: node.startOffset, printed: node.image }); | ||
}); | ||
} | ||
if (Comment) { | ||
Comment.forEach(node => { | ||
parts.push({ offset: node.startOffset, printed: node.image }); | ||
}); | ||
} | ||
if (chardata) { | ||
chardata.forEach((node, index) => { | ||
parts.push({ | ||
offset: node.location.startOffset, | ||
printed: path.call(print, "children", "chardata", index) | ||
}); | ||
}); | ||
} | ||
if (element) { | ||
element.forEach((node, index) => { | ||
parts.push({ | ||
offset: node.location.startOffset, | ||
printed: path.call(print, "children", "element", index) | ||
}); | ||
}); | ||
} | ||
if (reference) { | ||
reference.forEach(node => { | ||
parts.push({ | ||
offset: node.location.startOffset, | ||
printed: (node.children.CharRef || node.children.EntityRef)[0].image | ||
}); | ||
}); | ||
} | ||
parts.sort((left, right) => left.offset - right.offset); | ||
parts = parts.map(({ printed }) => printed); | ||
return group(concat(parts)); | ||
return group( | ||
concat( | ||
[] | ||
.concat(CData.map(printCData)) | ||
.concat(Comment.map(printComment)) | ||
.concat(chardata.map(printCharData(path, print))) | ||
.concat(element.map(printElement(path, print))) | ||
.concat(reference.map(printReference)) | ||
.sort((left, right) => left.offset - right.offset) | ||
.map(({ printed }) => printed) | ||
) | ||
); | ||
}, | ||
@@ -174,7 +178,33 @@ docTypeDecl: (path, opts, print) => { | ||
const openTag = group(concat(parts.concat(softline, START_CLOSE[0].image))); | ||
const closeTag = group( | ||
concat([SLASH_OPEN[0].image, END_NAME[0].image, END[0].image]) | ||
); | ||
// If we're ignoring whitespace and we're printing a node that only contains | ||
// other elements, then we can just print out the child elements directly. | ||
if (opts.xmlWhitespaceSensitivity === "ignore" && elementOnly(content[0])) { | ||
return group( | ||
concat([ | ||
openTag, | ||
indent( | ||
concat([ | ||
hardline, | ||
join( | ||
hardline, | ||
path.map(print, "children", "content", 0, "children", "element") | ||
) | ||
]) | ||
), | ||
hardline, | ||
closeTag | ||
]) | ||
); | ||
} | ||
return group( | ||
concat([ | ||
group(concat(parts.concat(softline, START_CLOSE[0].image))), | ||
openTag, | ||
indent(path.call(print, "children", "content", 0)), | ||
group(concat([SLASH_OPEN[0].image, END_NAME[0].image, END[0].image])) | ||
closeTag | ||
]) | ||
@@ -181,0 +211,0 @@ ); |
@@ -5,8 +5,17 @@ const fs = require("fs"); | ||
test("fixture", () => { | ||
const filepath = path.join(__dirname, "./fixture.xml"); | ||
const content = fs.readFileSync(filepath, "utf-8"); | ||
const fixture = fs.readFileSync(path.join(__dirname, "./fixture.xml"), "utf-8"); | ||
const format = (content, opts = {}) => prettier.format(content, { | ||
...opts, | ||
parser: "xml", | ||
plugins: ["."] | ||
}); | ||
const formatted = prettier.format(content, { parser: "xml", plugins: ["."] }); | ||
test("defaults", () => { | ||
const formatted = format(fixture); | ||
expect(formatted).toMatchSnapshot(); | ||
}); | ||
test("xmlWhitespaceSensitivity => ignore", () => { | ||
const formatted = format(fixture, { xmlWhitespaceSensitivity: "ignore" }); | ||
expect(formatted).toMatchSnapshot(); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
20063
349
78