Socket
Socket
Sign inDemoInstall

@prettier/plugin-xml

Package Overview
Dependencies
Maintainers
13
Versions
35
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@prettier/plugin-xml - npm Package Compare versions

Comparing version 3.0.0 to 3.1.0

17

CHANGELOG.md

@@ -9,2 +9,15 @@ # Changelog

## [3.1.0] - 2023-07-07
### Added
- the `xmlSortAttributesByKey: true | false` option has been added. See the README.
- The `xmlQuoteAttributes: "preserve" | "single" | "double"` option has been added. See the README.
## [3.0.0] - 2023-07-06
### Changed
- See alpha release notes.
## [3.0.0-alpha.0] - 2023-06-02

@@ -207,3 +220,5 @@

[unreleased]: https://github.com/prettier/plugin-xml/compare/v3.0.0-alpha.0...HEAD
[unreleased]: https://github.com/prettier/plugin-xml/compare/v3.1.0...HEAD
[3.1.0]: https://github.com/prettier/plugin-xml/compare/v3.0.0...v3.1.0
[3.0.0]: https://github.com/prettier/plugin-xml/compare/v3.0.0-alpha.0...v3.0.0
[3.0.0-alpha.0]: https://github.com/prettier/plugin-xml/compare/v2.2.0...v3.0.0-alpha.0

@@ -210,0 +225,0 @@ [2.2.0]: https://github.com/prettier/plugin-xml/compare/v2.1.0...v2.2.0

14

package.json
{
"name": "@prettier/plugin-xml",
"version": "3.0.0",
"version": "3.1.0",
"description": "prettier plugin for XML",

@@ -9,3 +9,3 @@ "type": "module",

"lint": "eslint --cache .",
"prepare": "node bin/languages.js",
"prepare": "node bin/languages.js && husky install",
"print": "prettier --plugin=.",

@@ -31,4 +31,6 @@ "test": "node --experimental-vm-modules node_modules/jest/bin/jest.js"

"eslint-config-prettier": "^8.0.0",
"husky": "^8.0.0",
"jest": "^29.2.1",
"linguist-languages": "^7.21.0"
"linguist-languages": "^7.21.0",
"lint-staged": "^13.2.3"
},

@@ -65,3 +67,9 @@ "eslintConfig": {

"trailingComma": "none"
},
"lint-staged": {
"*.js": [
"eslint --cache --fix",
"prettier --write"
]
}
}

@@ -49,10 +49,12 @@ <h1 align="center">Prettier for XML</h1>

| API Option | CLI Option | Default | Description |
| -------------------------- | ------------------------------ | :--------: | ------------------------------------------------------------------------------------------------------------------------ |
| `bracketSameLine` | `--bracket-same-line` | `true` | Same as in Prettier ([see prettier docs](https://prettier.io/docs/en/options.html#bracket-same-line)) |
| `printWidth` | `--print-width` | `80` | Same as in Prettier ([see prettier docs](https://prettier.io/docs/en/options.html#print-width)). |
| `singleAttributePerLine` | `--single-attribute-per-line` | `false` | Same as in Prettier ([see prettier docs](https://prettier.io/docs/en/options.html#single-attribute-per-line)) |
| `tabWidth` | `--tab-width` | `2` | Same as in Prettier ([see prettier docs](https://prettier.io/docs/en/options.html#tab-width)). |
| `xmlSelfClosingSpace` | `--xml-self-closing-space` | `true` | Adds a space before self-closing tags. |
| `xmlWhitespaceSensitivity` | `--xml-whitespace-sensitivity` | `"strict"` | Options are `"strict"`, `"preserve"`, and `"ignore"`. You may want `"ignore"` or `"preserve"`, [see below](#whitespace). |
| API Option | CLI Option | Default | Description |
| -------------------------- | ------------------------------ | :----------: | ------------------------------------------------------------------------------------------------------------------------ |
| `bracketSameLine` | `--bracket-same-line` | `true` | Same as in Prettier ([see prettier docs](https://prettier.io/docs/en/options.html#bracket-same-line)) |
| `printWidth` | `--print-width` | `80` | Same as in Prettier ([see prettier docs](https://prettier.io/docs/en/options.html#print-width)). |
| `singleAttributePerLine` | `--single-attribute-per-line` | `false` | Same as in Prettier ([see prettier docs](https://prettier.io/docs/en/options.html#single-attribute-per-line)) |
| `tabWidth` | `--tab-width` | `2` | Same as in Prettier ([see prettier docs](https://prettier.io/docs/en/options.html#tab-width)). |
| `xmlQuoteAttributes` | `--xml-quote-attributes` | `"preserve"` | Options are `"preserve"`, `"single"`, and `"double"` |
| `xmlSelfClosingSpace` | `--xml-self-closing-space` | `true` | Adds a space before self-closing tags. |
| `xmlSortAttributesByKey` | `--xml-sort-attributes-by-key` | `false` | Orders XML attributes by key alphabetically while prioritizing xmlns attributes. |
| `xmlWhitespaceSensitivity` | `--xml-whitespace-sensitivity` | `"strict"` | Options are `"strict"`, `"preserve"`, and `"ignore"`. You may want `"ignore"` or `"preserve"`, [see below](#whitespace). |

@@ -59,0 +61,0 @@ Any of these can be added to your existing [prettier configuration

@@ -42,2 +42,32 @@ import languages from "./languages.js";

since: "0.6.0"
},
xmlSortAttributesByKey: {
type: "boolean",
category: "XML",
default: false,
description:
"Orders XML attributes by key alphabetically while prioritizing xmlns attributes."
},
xmlQuoteAttributes: {
type: "choice",
category: "XML",
default: "preserve",
description: "How to handle whitespaces in XML.",
choices: [
{
value: "preserve",
description:
"Quotes in attribute values will be preserved as written."
},
{
value: "single",
description:
"Quotes in attribute values will be converted to consistent single quotes and other quotes in the string will be escaped."
},
{
value: "double",
description:
"Quotes in attribute values will be converted to consistent double quotes and other quotes in the string will be escaped."
}
]
}

@@ -44,0 +74,0 @@ },

@@ -49,3 +49,15 @@ import * as doc from "prettier/doc";

return [Name[0].image, EQUALS[0].image, STRING[0].image];
let attributeValue;
if (opts.xmlQuoteAttributes === "double") {
const content = STRING[0].image.slice(1, -1).replaceAll('"', "&quot;");
attributeValue = `"${content}"`;
} else if (opts.xmlQuoteAttributes === "single") {
const content = STRING[0].image.slice(1, -1).replaceAll("'", "&apos;");
attributeValue = `'${content}'`;
} else {
// preserve
attributeValue = STRING[0].image;
}
return [Name[0].image, EQUALS[0].image, attributeValue];
}

@@ -361,5 +373,52 @@

if (attribute) {
const attributes = path.map(
(attributePath) => ({
node: attributePath.getValue(),
printed: print(attributePath)
}),
"children",
"attribute"
);
if (opts.xmlSortAttributesByKey) {
attributes.sort((left, right) => {
const leftAttr = left.node.children.Name[0].image;
const rightAttr = right.node.children.Name[0].image;
// Check if the attributes are xmlns.
if (leftAttr === "xmlns") return -1;
if (rightAttr === "xmlns") return 1;
// Check if they are both in namespaces.
if (leftAttr.includes(":") && rightAttr.includes(":")) {
const [leftNS, leftKey] = leftAttr.split(":");
const [rightNS, rightKey] = rightAttr.split(":");
// If namespaces are equal, compare keys
if (leftNS === rightNS) return leftKey.localeCompare(rightKey);
// Handle the 1 but not both being an xmlns
if (leftNS === "xmlns") return -1;
if (rightNS === "xlmns") return 1;
return leftNS.localeCompare(rightNS);
}
// Check if the attributes have namespaces.
if (leftAttr.includes(":")) return -1;
if (rightAttr.includes(":")) return 1;
return leftAttr.localeCompare(rightAttr);
});
}
const separator = opts.singleAttributePerLine ? hardline : line;
parts.push(
indent([line, join(separator, path.map(print, "children", "attribute"))])
indent([
line,
join(
separator,
attributes.map(({ printed }) => printed)
)
])
);

@@ -366,0 +425,0 @@ }

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc