Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: prettier/plugin-xml
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v3.0.0
Choose a base ref
...
head repository: prettier/plugin-xml
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v3.1.0
Choose a head ref

Commits on Jun 28, 2023

  1. Added quote standardization and ability to use single or double quote…

    …s exclusively
    Ben Weis committed Jun 28, 2023
    Copy the full SHA
    7eb19fe View commit details

Commits on Jun 29, 2023

  1. Added feature to order XML attributes by key alphabetically while pr…

    …ioritizing xmlns attributes
    Ben Weis committed Jun 29, 2023
    Copy the full SHA
    2e058f0 View commit details
  2. Moved to xmlQuoteAttribute and fixed bug with properly escaping quote…

    …s within attribute values
    Ben Weis committed Jun 29, 2023
    Copy the full SHA
    b559870 View commit details

Commits on Jul 6, 2023

  1. Update CHANGELOG

    kddnewton committed Jul 6, 2023

    Verified

    This commit was signed with the committer’s verified signature.
    kddnewton Kevin Newton
    Copy the full SHA
    2349ad6 View commit details
  2. Fixed Lint Issue

    Ben Weis committed Jul 6, 2023
    Copy the full SHA
    8c21d09 View commit details
  3. Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    6f35cea View commit details

Commits on Jul 7, 2023

  1. chore(deps-dev): bump jest from 29.6.0 to 29.6.1

    Bumps [jest](https://github.com/facebook/jest/tree/HEAD/packages/jest) from 29.6.0 to 29.6.1.
    - [Release notes](https://github.com/facebook/jest/releases)
    - [Changelog](https://github.com/jestjs/jest/blob/main/CHANGELOG.md)
    - [Commits](https://github.com/facebook/jest/commits/v29.6.1/packages/jest)
    
    ---
    updated-dependencies:
    - dependency-name: jest
      dependency-type: direct:development
      update-type: version-update:semver-patch
    ...
    
    Signed-off-by: dependabot[bot] <support@github.com>
    dependabot[bot] authored Jul 7, 2023

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    8fed5eb View commit details
  2. Merge pull request #691 from prettier/dependabot/npm_and_yarn/jest-29…

    ….6.1
    
    chore(deps-dev): bump jest from 29.6.0 to 29.6.1
    github-actions[bot] authored Jul 7, 2023

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    e2356fe View commit details
  3. Ran prettier

    Ben Weis committed Jul 7, 2023
    Copy the full SHA
    9a547ba View commit details
  4. Separated the sort and print steps

    Ben Weis committed Jul 7, 2023
    Copy the full SHA
    5ab14a2 View commit details
  5. Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    5b5edf0 View commit details
  6. Merge pull request #684 from bweis/main

    Added quote standardization and ability to use single or double quote...
    kddnewton authored Jul 7, 2023

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    87fb858 View commit details
  7. Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    f15c65a View commit details
  8. fix tests after merge

    Ben Weis committed Jul 7, 2023
    Copy the full SHA
    a9f7cb4 View commit details
  9. added lint-staged

    Ben Weis committed Jul 7, 2023
    Copy the full SHA
    e324dcd View commit details
  10. Merge pull request #685 from bweis/sorting

     Added feature to order XML attributes by key alphabetically while prioritizing xmlns attributes
    kddnewton authored Jul 7, 2023

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    645fdbc View commit details
  11. Fix husky

    Ben Weis committed Jul 7, 2023
    Copy the full SHA
    ea09fe8 View commit details
  12. Merge pull request #692 from bweis/husky

    Add lint_staged to auto run eslint and prettier upon commit
    kddnewton authored Jul 7, 2023

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    cef22c9 View commit details
  13. Verified

    This commit was signed with the committer’s verified signature.
    kddnewton Kevin Newton
    Copy the full SHA
    e8f73cb View commit details
  14. Merge pull request #693 from prettier/simplify

    Inline sort attributes by key
    kddnewton authored Jul 7, 2023

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    210a685 View commit details
  15. Fix up README

    kddnewton committed Jul 7, 2023

    Verified

    This commit was signed with the committer’s verified signature.
    kddnewton Kevin Newton
    Copy the full SHA
    b433d4e View commit details
  16. Bump to version 3.1.0

    kddnewton committed Jul 7, 2023

    Verified

    This commit was signed with the committer’s verified signature.
    kddnewton Kevin Newton
    Copy the full SHA
    995d820 View commit details
Showing with 1,668 additions and 566 deletions.
  1. +1 −0 .husky/.gitignore
  2. +4 −0 .husky/pre-commit
  3. +16 −1 CHANGELOG.md
  4. +10 −8 README.md
  5. +11 −3 package.json
  6. +30 −0 src/plugin.js
  7. +61 −2 src/printer.js
  8. +622 −0 test/__snapshots__/format.test.js.snap
  9. +35 −0 test/fixture.xml
  10. +30 −0 test/format.test.js
  11. +848 −552 yarn.lock
1 change: 1 addition & 0 deletions .husky/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
_
4 changes: 4 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npx lint-staged
17 changes: 16 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -6,6 +6,19 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a

## [Unreleased]

## [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

### Added
@@ -203,7 +216,9 @@ and it will maintain your formatting.

- Initial release 🎉

[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
[2.2.0]: https://github.com/prettier/plugin-xml/compare/v2.1.0...v2.2.0
[2.1.0]: https://github.com/prettier/plugin-xml/compare/v2.0.1...v2.1.0
18 changes: 10 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -46,14 +46,16 @@ The `prettier` executable is now installed and ready for use:

Below are the options (from [`src/plugin.js`](src/plugin.js)) that `@prettier/plugin-xml` currently supports:

| 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). |

Any of these can be added to your existing [prettier configuration
file](https://prettier.io/docs/en/configuration.html). For example:
14 changes: 11 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"name": "@prettier/plugin-xml",
"version": "3.0.0",
"version": "3.1.0",
"description": "prettier plugin for XML",
"type": "module",
"main": "src/plugin.js",
"scripts": {
"lint": "eslint --cache .",
"prepare": "node bin/languages.js",
"prepare": "node bin/languages.js && husky install",
"print": "prettier --plugin=.",
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js"
},
@@ -27,8 +27,10 @@
"devDependencies": {
"eslint": "^8.5.0",
"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"
},
"eslintConfig": {
"extends": [
@@ -60,5 +62,11 @@
"./src/plugin.js"
],
"trailingComma": "none"
},
"lint-staged": {
"*.js": [
"eslint --cache --fix",
"prettier --write"
]
}
}
30 changes: 30 additions & 0 deletions src/plugin.js
Original file line number Diff line number Diff line change
@@ -39,6 +39,36 @@ const plugin = {
}
],
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."
}
]
}
},
defaultOptions: {
63 changes: 61 additions & 2 deletions src/printer.js
Original file line number Diff line number Diff line change
@@ -46,7 +46,19 @@ function printIToken(path) {
function printAttribute(path, opts, print) {
const { Name, EQUALS, STRING } = path.getValue().children;

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];
}

function printCharData(path, opts, print) {
@@ -357,9 +369,56 @@ function printElement(path, opts, print) {
const parts = [OPEN[0].image, Name[0].image];

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)
)
])
);
}

Loading