From 2f6d12aaa840197ffe7e42d52166ee93badb4c6d Mon Sep 17 00:00:00 2001 From: Christopher Chedeau Date: Fri, 26 May 2017 13:42:32 -0700 Subject: [PATCH] Inline url('string') (#1751) We don't want them to break even if they go beyond 80 columns --- src/parser.js | 30 ++++++++++++++++++- src/printer.js | 18 ++++++++++- .../__snapshots__/jsfmt.spec.js.snap | 12 ++++++++ tests/css_inline_url/inline_url.js | 3 ++ tests/css_inline_url/jsfmt.spec.js | 1 + 5 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 tests/css_inline_url/__snapshots__/jsfmt.spec.js.snap create mode 100644 tests/css_inline_url/inline_url.js create mode 100644 tests/css_inline_url/jsfmt.spec.js diff --git a/src/parser.js b/src/parser.js index bee78939..d6d07a3c 100644 --- a/src/parser.js +++ b/src/parser.js @@ -220,6 +220,34 @@ function parseValueNodes(nodes) { return rootParenGroup; } +function flattenGroups(node) { + if ( + node.type === "paren_group" && + !node.open && + !node.close && + node.groups.length === 1 + ) { + return flattenGroups(node.groups[0]); + } + + if ( + node.type === "comma_group" && + node.groups.length === 1 + ) { + return flattenGroups(node.groups[0]); + } + + if (node.type === "paren_group" || node.type === "comma_group") { + return Object.assign( + {}, + node, + {groups: node.groups.map(flattenGroups)} + ); + } + + return node; +} + function addTypePrefix(node, prefix) { if (node && typeof node === "object") { delete node.parent; @@ -254,7 +282,7 @@ function parseNestedValue(node) { for (const key in node) { parseNestedValue(node[key]); if (key === "nodes") { - node.group = parseValueNodes(node[key]); + node.group = flattenGroups(parseValueNodes(node[key]));; delete node[key]; } } diff --git a/src/printer.js b/src/printer.js index 038535f6..205d38b9 100644 --- a/src/printer.js +++ b/src/printer.js @@ -2635,7 +2635,23 @@ function genericPrintNoParens(path, options, print, args) { } case "value-paren_group": { if (!n.open) { - return join(concat([",", line]), path.map(print, "groups")); + return group(join(concat([",", line]), path.map(print, "groups"))); + } + + const parent = path.getParentNode(); + if ( + parent && + parent.type === "value-func" && + parent.value === "url" && + n.groups.length === 1 && + n.groups[0].type === "value-string" + ) { + return concat([ + n.open ? path.call(print, "open") : "", + join(", ", path.map(print, "groups")), + n.close ? path.call(print, "close") : "" + ]) + } return group( diff --git a/tests/css_inline_url/__snapshots__/jsfmt.spec.js.snap b/tests/css_inline_url/__snapshots__/jsfmt.spec.js.snap new file mode 100644 index 00000000..f2bf8e83 --- /dev/null +++ b/tests/css_inline_url/__snapshots__/jsfmt.spec.js.snap @@ -0,0 +1,12 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`inline_url.js 1`] = ` +.breadItem { + background-image: url('/images/product/simple_product_manager/breadcrumb/chevron_right.png'); +} +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +.breadItem { + background-image: url('/images/product/simple_product_manager/breadcrumb/chevron_right.png'); +} + +`; diff --git a/tests/css_inline_url/inline_url.js b/tests/css_inline_url/inline_url.js new file mode 100644 index 00000000..357600dd --- /dev/null +++ b/tests/css_inline_url/inline_url.js @@ -0,0 +1,3 @@ +.breadItem { + background-image: url('/images/product/simple_product_manager/breadcrumb/chevron_right.png'); +} diff --git a/tests/css_inline_url/jsfmt.spec.js b/tests/css_inline_url/jsfmt.spec.js new file mode 100644 index 00000000..a7d303b6 --- /dev/null +++ b/tests/css_inline_url/jsfmt.spec.js @@ -0,0 +1 @@ +run_spec(__dirname, { parser: "postcss" });