Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 24 additions & 31 deletions src/printer.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,15 @@ function printContentFragments(path, print) {
...path.map(printIToken, "Comment"),
...path.map(
({ node }) => ({
type: "chardata",
offset: node.location.startOffset,
printed: print()
}),
"chardata"
),
...path.map(
({ node }) => ({
type: "element",
offset: node.location.startOffset,
printed: print()
}),
Expand All @@ -126,6 +128,7 @@ function printContentFragments(path, print) {
...path.map(printIToken, "PROCESSING_INSTRUCTION"),
...path.map(
({ node }) => ({
type: "reference",
offset: node.location.startOffset,
printed: print()
}),
Expand Down Expand Up @@ -200,27 +203,31 @@ function printDocument(path, opts, print) {

if (docTypeDecl) {
fragments.push({
type: "doctype",
offset: docTypeDecl.location.startOffset,
printed: print("docTypeDecl")
});
}

if (prolog) {
fragments.push({
type: "prolog",
offset: prolog.location.startOffset,
printed: print("prolog")
});
}

path.each(({ node }) => {
fragments.push({
type: "misc",
offset: node.location.startOffset,
printed: print()
});
}, "misc");

if (element) {
fragments.push({
type: "element",
offset: element.location.startOffset,
printed: print("element")
});
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -296,6 +304,7 @@ function printCharDataIgnore(path) {

const location = chardata.location;
response.push({
type: "chardata",
offset: location.startOffset,
startLine: location.startLine,
endLine: location.endLine,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 <foo>bar</foo>).
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]);
Expand Down
Loading