diff --git a/src/printer.js b/src/printer.js index 753ba61..d74f60e 100644 --- a/src/printer.js +++ b/src/printer.js @@ -111,6 +111,7 @@ function printContentFragments(path, print) { ...path.map(printIToken, "Comment"), ...path.map( ({ node }) => ({ + type: "chardata", offset: node.location.startOffset, printed: print() }), @@ -118,6 +119,7 @@ function printContentFragments(path, print) { ), ...path.map( ({ node }) => ({ + type: "element", offset: node.location.startOffset, printed: print() }), @@ -126,6 +128,7 @@ function printContentFragments(path, print) { ...path.map(printIToken, "PROCESSING_INSTRUCTION"), ...path.map( ({ node }) => ({ + type: "reference", offset: node.location.startOffset, printed: print() }), @@ -200,6 +203,7 @@ function printDocument(path, opts, print) { if (docTypeDecl) { fragments.push({ + type: "doctype", offset: docTypeDecl.location.startOffset, printed: print("docTypeDecl") }); @@ -207,6 +211,7 @@ function printDocument(path, opts, print) { if (prolog) { fragments.push({ + type: "prolog", offset: prolog.location.startOffset, printed: print("prolog") }); @@ -214,6 +219,7 @@ function printDocument(path, opts, print) { path.each(({ node }) => { fragments.push({ + type: "misc", offset: node.location.startOffset, printed: print() }); @@ -221,6 +227,7 @@ function printDocument(path, opts, print) { if (element) { fragments.push({ + type: "element", offset: element.location.startOffset, printed: print("element") }); @@ -258,6 +265,7 @@ function printCharDataPreserve(path, print) { prevFragment.printed = group([prevFragment.printed, content]); } else { response.push({ + type: "chardata", offset: location.startOffset, startLine: location.startLine, endLine: location.endLine, @@ -296,6 +304,7 @@ function printCharDataIgnore(path) { const location = chardata.location; response.push({ + type: "chardata", offset: location.startOffset, startLine: location.startLine, endLine: location.endLine, @@ -326,6 +335,7 @@ function printElementFragments(path, opts, print) { response = response.concat( path.map( ({ node: { location } }) => ({ + type: "element", offset: location.startOffset, startLine: location.startLine, endLine: location.endLine, @@ -473,49 +483,32 @@ function printElement(path, opts, print) { return group([...parts, space, "/>"]); } - // If the only content of this tag is chardata, then use a softline so - // that we won't necessarily break (to allow bar). - if ( - fragments.length === 1 && - content.chardata.filter((chardata) => chardata.TEXT).length === 1 - ) { - return group([ - openTag, - indent([softline, fragments[0].printed]), - softline, - closeTag - ]); - } - - let delimiter = hardline; + const docs = []; + let lastNode; - // If the only content is both chardata and references, then use a softline - // so that we won't necessarily break. - if ( - fragments.length === - content.chardata.filter((chardata) => chardata.TEXT).length + - content.reference.length - ) { - delimiter = " "; + // if we have a single element as child, force it to a separate line + if (fragments.length === 1 && content.element.length === 1) { + docs.push(hardline); + } else { + docs.push(softline); } - const docs = [hardline]; - let lastLine = fragments[0].startLine; - + // insert hardlines before/after each element, so they go on separate lines from + // the surrounding text, but leave the mixed content between the elements alone fragments.forEach((node, index) => { if (index !== 0) { - if (node.startLine - lastLine >= 2) { + if (node.startLine - lastNode.endLine >= 2) { docs.push(hardline, hardline); - } else { - docs.push(delimiter); + } else if (node.type === "element" || lastNode.type === "element") { + docs.push(hardline); } } docs.push(node.printed); - lastLine = node.endLine; + lastNode = node; }); - return group([openTag, indent(docs), hardline, closeTag]); + return group([openTag, indent(docs), softline, closeTag]); } return group([openTag, indent(print("content")), closeTag]); diff --git a/test/__snapshots__/format.test.js.snap b/test/__snapshots__/format.test.js.snap index 5a89e2a..73f9cdb 100644 --- a/test/__snapshots__/format.test.js.snap +++ b/test/__snapshots__/format.test.js.snap @@ -126,6 +126,79 @@ use { slide + + +

piñata

+

piñata

+ +

+ piñata +
+

+

+ piñata +
+

+ +

+
+ piñata +

+

+
+ piñata +

+ +

+
+
+ piñata +
+
+

+

+
+
+ piñata +
+
+

+ +

Let's go

+

Let's go

+ +

+ Let's go +
+

+

+ Let's go +
+

+ +

+
+ Let's go +

+

+
+ Let's go +

+ +

+
+
+ Let's go +
+
+

+

+
+
+ Let's go +
+
+

" @@ -257,6 +330,79 @@ use { slide + + +

piñata

+

piñata

+ +

+ piñata +
+

+

+ piñata +
+

+ +

+
+ piñata +

+

+
+ piñata +

+ +

+
+
+ piñata +
+
+

+

+
+
+ piñata +
+
+

+ +

Let's go

+

Let's go

+ +

+ Let's go +
+

+

+ Let's go +
+

+ +

+
+ Let's go +

+

+
+ Let's go +

+ +

+
+
+ Let's go +
+
+

+

+
+
+ Let's go +
+
+

" @@ -384,6 +530,31 @@ use { slide + + +

piñata

+

piñata

+ +

piñata

+

piñata

+ +


piñata

+


piñata

+ +



piñata

+



piñata

+ +

Let's go

+

Let's go

+ +

Let's go

+

Let's go

+ +


Let's go

+


Let's go

+ +



Let's go

+



Let's go

" @@ -548,6 +719,79 @@ use { slide + + +

piñata

+

piñata

+ +

+ piñata +
+

+

+ piñata +
+

+ +

+
+ piñata +

+

+
+ piñata +

+ +

+
+
+ piñata +
+
+

+

+
+
+ piñata +
+
+

+ +

Let's go

+

Let's go

+ +

+ Let's go +
+

+

+ Let's go +
+

+ +

+
+ Let's go +

+

+
+ Let's go +

+ +

+
+
+ Let's go +
+
+

+

+
+
+ Let's go +
+
+

" @@ -675,6 +919,31 @@ use { slide + + +

piñata

+

piñata

+ +

piñata

+

piñata

+ +


piñata

+


piñata

+ +



piñata

+



piñata

+ +

Let's go

+

Let's go

+ +

Let's go

+

Let's go

+ +


Let's go

+


Let's go

+ +



Let's go

+



Let's go

" @@ -802,6 +1071,31 @@ use { slide + + +

piñata

+

piñata

+ +

piñata

+

piñata

+ +


piñata

+


piñata

+ +



piñata

+



piñata

+ +

Let's go

+

Let's go

+ +

Let's go

+

Let's go

+ +


Let's go

+


Let's go

+ +



Let's go

+



Let's go

" @@ -929,6 +1223,31 @@ use { slide + + +

piñata

+

piñata

+ +

piñata

+

piñata

+ +


piñata

+


piñata

+ +



piñata

+



piñata

+ +

Let's go

+

Let's go

+ +

Let's go

+

Let's go

+ +


Let's go

+


Let's go

+ +



Let's go

+



Let's go

" @@ -1065,6 +1384,79 @@ use { slide + + +

piñata

+

piñata

+ +

+ piñata +
+

+

+ piñata +
+

+ +

+
+ piñata +

+

+
+ piñata +

+ +

+
+
+ piñata +
+
+

+

+
+
+ piñata +
+
+

+ +

Let's go

+

Let's go

+ +

+ Let's go +
+

+

+ Let's go +
+

+ +

+
+ Let's go +

+

+
+ Let's go +

+ +

+
+
+ Let's go +
+
+

+

+
+
+ Let's go +
+
+

" @@ -1192,6 +1584,31 @@ use { slide + + +

piñata

+

piñata

+ +

piñata

+

piñata

+ +


piñata

+


piñata

+ +



piñata

+



piñata

+ +

Let's go

+

Let's go

+ +

Let's go

+

Let's go

+ +


Let's go

+


Let's go

+ +



Let's go

+



Let's go

" @@ -1328,6 +1745,79 @@ use { slide + + +

piñata

+

piñata

+ +

+ piñata +
+

+

+ piñata +
+

+ +

+
+ piñata +

+

+
+ piñata +

+ +

+
+
+ piñata +
+
+

+

+
+
+ piñata +
+
+

+ +

Let's go

+

Let's go

+ +

+ Let's go +
+

+

+ Let's go +
+

+ +

+
+ Let's go +

+

+
+ Let's go +

+ +

+
+
+ Let's go +
+
+

+

+
+
+ Let's go +
+
+

" @@ -1457,6 +1947,31 @@ use { slide + + +

piñata

+

piñata

+ +

piñata

+

piñata

+ +


piñata

+


piñata

+ +



piñata

+



piñata

+ +

Let's go

+

Let's go

+ +

Let's go

+

Let's go

+ +


Let's go

+


Let's go

+ +



Let's go

+



Let's go

" diff --git a/test/fixture.xml b/test/fixture.xml index d535eb8..e3e6e4e 100644 --- a/test/fixture.xml +++ b/test/fixture.xml @@ -106,5 +106,30 @@ slide + + +

piñata

+

piñata

+ +

piñata

+

piñata

+ +


piñata

+


piñata

+ +



piñata

+



piñata

+ +

Let's go

+

Let's go

+ +

Let's go

+

Let's go

+ +


Let's go

+


Let's go

+ +



Let's go

+



Let's go