Repository: prettier/plugin-xml
Pull request #50 (2020-01-28 14:17:28)

Handle processing instructions inside elements


Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 4d09846..ad90091 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a
 
 ## [Unreleased]
 
+### Added
+
+- Handle processing instructions inside elements.
+
 ## [0.6.0] - 2020-01-27
 
 ### Added
diff --git a/README.md b/README.md
index b6eeb04..431a413 100644
--- a/README.md
+++ b/README.md
@@ -46,7 +46,7 @@ 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                                                                                      |
+| API Option                 | CLI Option                     |  Default   | Description                                                                                      |
 | -------------------------- | ------------------------------ | :--------: | ------------------------------------------------------------------------------------------------ |
 | `printWidth`               | `--print-width`                |    `80`    | Same as in Prettier ([see prettier docs](https://prettier.io/docs/en/options.html#print-width)). |
 | `tabWidth`                 | `--tab-width`                  |    `2`     | Same as in Prettier ([see prettier docs](https://prettier.io/docs/en/options.html#tab-width)).   |
diff --git a/src/print.js b/src/print.js
index 2d11b9b..0bb294b 100644
--- a/src/print.js
+++ b/src/print.js
@@ -41,6 +41,11 @@ const printElement = (path, print) => (node, index) => ({
   printed: path.call(print, "children", "element", index)
 });
 
+const printProcessingInstruction = node => ({
+  offset: node.startOffset,
+  printed: node.image
+});
+
 const printReference = node => ({
   offset: node.location.startOffset,
   printed: (node.children.CharRef || node.children.EntityRef)[0].image
@@ -68,6 +73,7 @@ const nodes = {
       Comment = [],
       chardata = [],
       element = [],
+      PROCESSING_INSTRUCTION = [],
       reference = []
     } = path.getValue().children;
 
@@ -78,6 +84,7 @@ const nodes = {
           .concat(Comment.map(printComment))
           .concat(chardata.map(printCharData(path, print)))
           .concat(element.map(printElement(path, print)))
+          .concat(PROCESSING_INSTRUCTION.map(printProcessingInstruction))
           .concat(reference.map(printReference))
           .sort((left, right) => left.offset - right.offset)
           .map(({ printed }) => printed)
@@ -181,18 +188,30 @@ const nodes = {
     // 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])) {
+      const { element, PROCESSING_INSTRUCTION = [] } = content[0].children;
+      const children = []
+        .concat(
+          element.map((node, index) => ({
+            offset: node.location.startOffset,
+            printed: path.call(
+              print,
+              "children",
+              "content",
+              0,
+              "children",
+              "element",
+              index
+            )
+          }))
+        )
+        .concat(PROCESSING_INSTRUCTION.map(printProcessingInstruction))
+        .sort((left, right) => left.offset - right.offset)
+        .map(({ printed }) => printed);
+
       return group(
         concat([
           openTag,
-          indent(
-            concat([
-              hardline,
-              join(
-                hardline,
-                path.map(print, "children", "content", 0, "children", "element")
-              )
-            ])
-          ),
+          indent(concat([hardline, join(hardline, children)])),
           hardline,
           closeTag
         ])


Test patch:
diff --git a/test/__snapshots__/format.test.js.snap b/test/__snapshots__/format.test.js.snap
index 985e92b..fc7ea83 100644
--- a/test/__snapshots__/format.test.js.snap
+++ b/test/__snapshots__/format.test.js.snap
@@ -19,6 +19,7 @@ exports[`defaults 1`] = `
     <![CDATA[foo]]>
     bar
   </desc>
+  <?pagebreak?>
   <style />
   <style type=\\"text/css\\">
     circle          { stroke-opacity: 0.7; }
@@ -26,6 +27,7 @@ exports[`defaults 1`] = `
     use             { stroke: purple;
                       fill: orange; }
   </style>
+  <?pagebreak?>
     <g class=\\"special\\" style=\\"fill: blue\\">
         <circle id=\\"c\\" cy=\\"50\\" cx=\\"50\\" r=\\"40\\" stroke-width=\\"20\\" />
     </g>
@@ -54,6 +56,7 @@ exports[`xmlWhitespaceSensitivity => ignore 1`] = `
     <![CDATA[foo]]>
     bar
   </desc>
+  <?pagebreak?>
   <style />
   <style type=\\"text/css\\">
     circle          { stroke-opacity: 0.7; }
@@ -61,6 +64,7 @@ exports[`xmlWhitespaceSensitivity => ignore 1`] = `
     use             { stroke: purple;
                       fill: orange; }
   </style>
+  <?pagebreak?>
   <g class=\\"special\\" style=\\"fill: blue\\">
     <circle id=\\"c\\" cy=\\"50\\" cx=\\"50\\" r=\\"40\\" stroke-width=\\"20\\" />
   </g>
diff --git a/test/fixture.xml b/test/fixture.xml
index 67913f1..0c247ad 100644
--- a/test/fixture.xml
+++ b/test/fixture.xml
@@ -16,6 +16,7 @@
     <![CDATA[foo]]>
     bar
   </desc>
+  <?pagebreak?>
   <style></style>
   <style type="text/css">
     circle          { stroke-opacity: 0.7; }
@@ -23,6 +24,7 @@
     use             { stroke: purple;
                       fill: orange; }
   </style>
+  <?pagebreak?>
     <g class="special" style="fill: blue">
         <circle id="c" cy="50" cx="50" r="40" stroke-width="20" />
     </g>