diff --git a/src/language-html/printer-html.js b/src/language-html/printer-html.js index 98c0fde5fc71..014f5f9ea555 100644 --- a/src/language-html/printer-html.js +++ b/src/language-html/printer-html.js @@ -34,7 +34,8 @@ const { normalizeParts, preferHardlineAsLeadingSpaces, replaceDocNewlines, - replaceNewlines + replaceNewlines, + shouldPreserveElementContent } = require("./utils"); const preprocess = require("./preprocess"); const assert = require("assert"); @@ -442,29 +443,58 @@ function printChildren(path, options, print) { ); function printChild(childPath) { - if (!hasPrettierIgnore(childPath)) { - return print(childPath); - } const child = childPath.getValue(); - return concat( - [].concat( - printOpeningTagPrefix(child), - replaceNewlines( - options.originalText.slice( - options.locStart(child) + - (child.prev && needsToBorrowNextOpeningTagStartMarker(child.prev) - ? printOpeningTagStartMarker(child).length - : 0), - options.locEnd(child) - - (child.next && needsToBorrowPrevClosingTagEndMarker(child.next) - ? printClosingTagEndMarker(child).length - : 0) + + if (hasPrettierIgnore(childPath)) { + return concat( + [].concat( + printOpeningTagPrefix(child), + replaceNewlines( + options.originalText.slice( + options.locStart(child) + + (child.prev && + needsToBorrowNextOpeningTagStartMarker(child.prev) + ? printOpeningTagStartMarker(child).length + : 0), + options.locEnd(child) - + (child.next && needsToBorrowPrevClosingTagEndMarker(child.next) + ? printClosingTagEndMarker(child).length + : 0) + ), + literalline ), - literalline - ), - printClosingTagSuffix(child) - ) - ); + printClosingTagSuffix(child) + ) + ); + } + + if (shouldPreserveElementContent(childPath)) { + return concat( + [].concat( + printOpeningTagPrefix(child), + group(printOpeningTag(childPath, options, print)), + replaceNewlines( + options.originalText.slice( + child.startSourceSpan.end.offset - + (child.firstChild && + needsToBorrowParentOpeningTagEndMarker(child.firstChild) + ? printOpeningTagEndMarker(child).length + : 0), + child.endSourceSpan.start.offset + + (child.lastChild && + needsToBorrowParentClosingTagStartMarker(child.lastChild) + ? printClosingTagStartMarker(child).length + : 0) + ), + literalline + ), + printClosingTag(child), + printClosingTagSuffix(child) + ) + ); + } + + return print(childPath); } function printBetweenLine(prevNode, nextNode) { diff --git a/src/language-html/utils.js b/src/language-html/utils.js index 8d75dc78b690..1561a3dd2f0f 100644 --- a/src/language-html/utils.js +++ b/src/language-html/utils.js @@ -34,10 +34,16 @@ function mapObject(object, fn) { return newObject; } -function hasPrettierIgnore(path) { +function shouldPreserveElementContent(path) { const node = path.getValue(); - if (node.type === "attribute" || node.type === "text") { - return false; + + if ( + node.type === "element" && + node.fullName === "template" && + node.attrMap.lang && + node.attrMap.lang !== "html" + ) { + return true; } // TODO: handle non-text children in
@@ -50,6 +56,15 @@ function hasPrettierIgnore(path) { return true; } + return false; +} + +function hasPrettierIgnore(path) { + const node = path.getValue(); + if (node.type === "attribute" || node.type === "text") { + return false; + } + const parentNode = path.getParentNode(); if (!parentNode) { return false; @@ -614,5 +629,6 @@ module.exports = { preferHardlineAsLeadingSpaces, preferHardlineAsTrailingSpaces, replaceDocNewlines, - replaceNewlines + replaceNewlines, + shouldPreserveElementContent }; diff --git a/tests/html_vue/__snapshots__/jsfmt.spec.js.snap b/tests/html_vue/__snapshots__/jsfmt.spec.js.snap index 776250f5383f..e07a41802756 100644 --- a/tests/html_vue/__snapshots__/jsfmt.spec.js.snap +++ b/tests/html_vue/__snapshots__/jsfmt.spec.js.snap @@ -850,11 +850,7 @@ exports[`pre-child.vue - vue-verify 1`] = ` -+-
++ .test + #foo + .bla + + + + .test + #foo + .bla + +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + + .test + #foo + .bla + + + + .test + #foo + .bla + + +`; + +exports[`template-lang.vue - vue-verify 2`] = ` + + .test + #foo + .bla + + + + .test + #foo + .bla + +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + + .test + #foo + .bla + + + + .test + #foo + .bla + + +`; + exports[`test.vue - vue-verify 1`] = ` diff --git a/tests/html_vue/template-lang.vue b/tests/html_vue/template-lang.vue new file mode 100644 index 000000000000..a5825cdb7024 --- /dev/null +++ b/tests/html_vue/template-lang.vue @@ -0,0 +1,15 @@ + + .test + #foo + .bla + + + + .test + #foo + .bla +