2017-06-06 00:28:38 +10:00
|
|
|
"use strict";
|
|
|
|
|
|
2018-02-13 18:41:55 +03:00
|
|
|
const htmlTagNames = require("html-tag-names");
|
2017-12-30 21:30:09 +11:00
|
|
|
const clean = require("./clean");
|
2018-02-09 13:17:48 +01:00
|
|
|
const privateUtil = require("../common/util");
|
|
|
|
|
const sharedUtil = require("../common/util-shared");
|
2017-12-26 12:23:50 +11:00
|
|
|
const doc = require("../doc");
|
|
|
|
|
const docBuilders = doc.builders;
|
2017-06-06 00:28:38 +10:00
|
|
|
const concat = docBuilders.concat;
|
|
|
|
|
const join = docBuilders.join;
|
|
|
|
|
const line = docBuilders.line;
|
|
|
|
|
const hardline = docBuilders.hardline;
|
|
|
|
|
const softline = docBuilders.softline;
|
|
|
|
|
const group = docBuilders.group;
|
2017-06-21 15:13:51 -07:00
|
|
|
const fill = docBuilders.fill;
|
2017-06-06 00:28:38 +10:00
|
|
|
const indent = docBuilders.indent;
|
|
|
|
|
|
2017-12-26 12:23:50 +11:00
|
|
|
const removeLines = doc.utils.removeLines;
|
2017-06-19 11:41:10 +02:00
|
|
|
|
2017-06-06 00:28:38 +10:00
|
|
|
function genericPrint(path, options, print) {
|
2018-02-05 16:42:32 +03:00
|
|
|
const node = path.getValue();
|
2017-06-06 00:28:38 +10:00
|
|
|
|
2017-09-03 22:40:44 +02:00
|
|
|
/* istanbul ignore if */
|
2018-02-05 16:42:32 +03:00
|
|
|
if (!node) {
|
2017-06-06 00:28:38 +10:00
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-05 16:42:32 +03:00
|
|
|
if (typeof node === "string") {
|
|
|
|
|
return node;
|
2017-06-06 00:28:38 +10:00
|
|
|
}
|
|
|
|
|
|
2018-02-05 16:42:32 +03:00
|
|
|
switch (node.type) {
|
2018-01-24 19:44:09 +03:00
|
|
|
case "css-comment-yaml":
|
2018-02-05 16:42:32 +03:00
|
|
|
return node.value;
|
2017-06-06 00:28:38 +10:00
|
|
|
case "css-root": {
|
2018-01-12 00:05:57 +03:00
|
|
|
const nodes = printNodeSequence(path, options, print);
|
|
|
|
|
|
|
|
|
|
if (nodes.parts.length) {
|
|
|
|
|
return concat([nodes, hardline]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nodes;
|
2017-06-06 00:28:38 +10:00
|
|
|
}
|
|
|
|
|
case "css-comment": {
|
2018-02-05 16:42:32 +03:00
|
|
|
if (node.raws.content) {
|
|
|
|
|
return node.raws.content;
|
2017-06-06 00:28:38 +10:00
|
|
|
}
|
2018-02-05 16:42:32 +03:00
|
|
|
const text = options.originalText.slice(
|
2018-02-09 13:17:48 +01:00
|
|
|
options.locStart(node),
|
|
|
|
|
options.locEnd(node)
|
2018-02-05 16:42:32 +03:00
|
|
|
);
|
|
|
|
|
const rawText = node.raws.text || node.text;
|
2017-06-06 00:28:38 +10:00
|
|
|
// Workaround a bug where the location is off.
|
|
|
|
|
// https://github.com/postcss/postcss-scss/issues/63
|
|
|
|
|
if (text.indexOf(rawText) === -1) {
|
2018-02-05 16:42:32 +03:00
|
|
|
if (node.raws.inline) {
|
2017-06-06 00:28:38 +10:00
|
|
|
return concat(["// ", rawText]);
|
|
|
|
|
}
|
|
|
|
|
return concat(["/* ", rawText, " */"]);
|
|
|
|
|
}
|
|
|
|
|
return text;
|
|
|
|
|
}
|
|
|
|
|
case "css-rule": {
|
2018-01-30 14:16:07 +03:00
|
|
|
// If a Less file ends up being parsed with the SCSS parser, Less
|
|
|
|
|
// variable declarations will be parsed as atrules with names ending
|
|
|
|
|
// with a colon, so keep the original case then.
|
|
|
|
|
const isDetachedRulesetDeclaration =
|
2018-02-05 16:42:32 +03:00
|
|
|
node.selector &&
|
2018-02-07 13:20:13 +03:00
|
|
|
node.selector.type !== "selector-root-invalid" &&
|
|
|
|
|
((typeof node.selector === "string" &&
|
|
|
|
|
/^@.+:.*$/.test(node.selector)) ||
|
|
|
|
|
(node.selector.value && /^@.+:.*$/.test(node.selector.value)));
|
|
|
|
|
|
2017-06-06 00:28:38 +10:00
|
|
|
return concat([
|
|
|
|
|
path.call(print, "selector"),
|
2018-02-05 16:42:32 +03:00
|
|
|
node.important ? " !important" : "",
|
|
|
|
|
node.nodes
|
2017-06-06 00:28:38 +10:00
|
|
|
? concat([
|
|
|
|
|
" {",
|
2018-02-05 16:42:32 +03:00
|
|
|
node.nodes.length > 0
|
2017-06-06 00:28:38 +10:00
|
|
|
? indent(
|
|
|
|
|
concat([hardline, printNodeSequence(path, options, print)])
|
|
|
|
|
)
|
|
|
|
|
: "",
|
|
|
|
|
hardline,
|
2018-01-30 14:16:07 +03:00
|
|
|
"}",
|
|
|
|
|
isDetachedRulesetDeclaration ? ";" : ""
|
2017-06-06 00:28:38 +10:00
|
|
|
])
|
|
|
|
|
: ";"
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
case "css-decl": {
|
2017-06-07 12:40:36 -07:00
|
|
|
// When the following less construct &:extend(.foo); is parsed with scss,
|
|
|
|
|
// it will put a space after `:` and break it. Ideally we should parse
|
|
|
|
|
// less files with less, but we can hardcode this to work with scss as
|
|
|
|
|
// well.
|
|
|
|
|
const isValueExtend =
|
2018-02-05 16:42:32 +03:00
|
|
|
node.value.type === "value-root" &&
|
|
|
|
|
node.value.group.type === "value-value" &&
|
|
|
|
|
node.value.group.group.type === "value-func" &&
|
|
|
|
|
node.value.group.group.value === "extend";
|
2017-06-19 11:41:10 +02:00
|
|
|
const isComposed =
|
2018-02-05 16:42:32 +03:00
|
|
|
node.value.type === "value-root" &&
|
|
|
|
|
node.value.group.type === "value-value" &&
|
|
|
|
|
node.prop === "composes";
|
2017-06-07 12:40:36 -07:00
|
|
|
|
2017-06-06 00:28:38 +10:00
|
|
|
return concat([
|
2018-02-05 16:42:32 +03:00
|
|
|
node.raws.before.replace(/[\s;]/g, ""),
|
|
|
|
|
maybeToLowerCase(node.prop),
|
2018-02-06 18:09:42 +03:00
|
|
|
node.raws.between.trim() === ":" ? ":" : node.raws.between.trim(),
|
2017-06-07 12:40:36 -07:00
|
|
|
isValueExtend ? "" : " ",
|
2017-06-19 11:41:10 +02:00
|
|
|
isComposed
|
|
|
|
|
? removeLines(path.call(print, "value"))
|
|
|
|
|
: path.call(print, "value"),
|
2018-02-06 18:09:42 +03:00
|
|
|
node.raws.important
|
|
|
|
|
? node.raws.important.replace(/\s*!\s*important/i, " !important")
|
|
|
|
|
: node.important ? " !important" : "",
|
|
|
|
|
node.raws.scssDefault
|
|
|
|
|
? node.raws.scssDefault.replace(/\s*!default/i, " !default")
|
|
|
|
|
: node.scssDefault ? " !default" : "",
|
|
|
|
|
node.raws.scssGlobal
|
|
|
|
|
? node.raws.scssGlobal.replace(/\s*!global/i, " !global")
|
|
|
|
|
: node.scssGlobal ? " !global" : "",
|
2018-02-05 16:42:32 +03:00
|
|
|
node.nodes
|
2017-06-06 00:28:38 +10:00
|
|
|
? concat([
|
|
|
|
|
" {",
|
|
|
|
|
indent(
|
|
|
|
|
concat([softline, printNodeSequence(path, options, print)])
|
|
|
|
|
),
|
|
|
|
|
softline,
|
|
|
|
|
"}"
|
|
|
|
|
])
|
|
|
|
|
: ";"
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
case "css-atrule": {
|
|
|
|
|
const hasParams =
|
2018-02-05 16:42:32 +03:00
|
|
|
node.params &&
|
|
|
|
|
!(node.params.type === "media-query-list" && node.params.value === "");
|
2017-09-03 21:22:28 +02:00
|
|
|
const isDetachedRulesetCall =
|
|
|
|
|
hasParams &&
|
2018-02-05 16:42:32 +03:00
|
|
|
node.params.type === "media-query-list" &&
|
|
|
|
|
/^\(\s*\)$/.test(node.params.value);
|
|
|
|
|
const isControlDirective = isNodeControlDirective(node);
|
2018-02-02 18:06:47 +03:00
|
|
|
const hasParensAround =
|
2018-02-05 16:42:32 +03:00
|
|
|
node.value &&
|
|
|
|
|
node.value.group.group.type === "value-paren_group" &&
|
|
|
|
|
node.value.group.group.open !== null &&
|
|
|
|
|
node.value.group.group.close !== null;
|
|
|
|
|
const isElse = node.name === "else";
|
2018-02-02 18:06:47 +03:00
|
|
|
|
2017-06-06 00:28:38 +10:00
|
|
|
return concat([
|
|
|
|
|
"@",
|
2017-09-15 19:48:16 +02:00
|
|
|
// If a Less file ends up being parsed with the SCSS parser, Less
|
|
|
|
|
// variable declarations will be parsed as atrules with names ending
|
|
|
|
|
// with a colon, so keep the original case then.
|
2018-02-05 16:42:32 +03:00
|
|
|
isDetachedRulesetCall || node.name.endsWith(":")
|
|
|
|
|
? node.name
|
|
|
|
|
: maybeToLowerCase(node.name),
|
2017-09-03 21:22:28 +02:00
|
|
|
hasParams
|
|
|
|
|
? concat([
|
|
|
|
|
isDetachedRulesetCall ? "" : " ",
|
|
|
|
|
path.call(print, "params")
|
|
|
|
|
])
|
|
|
|
|
: "",
|
2018-02-05 16:42:32 +03:00
|
|
|
node.selector
|
|
|
|
|
? indent(concat([" ", path.call(print, "selector")]))
|
|
|
|
|
: "",
|
|
|
|
|
node.value
|
2018-02-02 18:06:47 +03:00
|
|
|
? group(
|
|
|
|
|
concat([
|
|
|
|
|
" ",
|
|
|
|
|
path.call(print, "value"),
|
|
|
|
|
isControlDirective ? (hasParensAround ? " " : line) : ""
|
|
|
|
|
])
|
|
|
|
|
)
|
|
|
|
|
: isElse ? " " : "",
|
2018-02-05 16:42:32 +03:00
|
|
|
node.nodes
|
2017-06-06 00:28:38 +10:00
|
|
|
? concat([
|
2018-02-02 18:06:47 +03:00
|
|
|
isControlDirective ? "" : " ",
|
|
|
|
|
"{",
|
2017-06-06 00:28:38 +10:00
|
|
|
indent(
|
|
|
|
|
concat([
|
2018-02-05 16:42:32 +03:00
|
|
|
node.nodes.length > 0 ? softline : "",
|
2017-06-06 00:28:38 +10:00
|
|
|
printNodeSequence(path, options, print)
|
|
|
|
|
])
|
|
|
|
|
),
|
|
|
|
|
softline,
|
|
|
|
|
"}"
|
|
|
|
|
])
|
|
|
|
|
: ";"
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
case "css-import": {
|
|
|
|
|
return concat([
|
|
|
|
|
"@",
|
2018-02-05 16:42:32 +03:00
|
|
|
maybeToLowerCase(node.name),
|
2017-06-06 00:28:38 +10:00
|
|
|
" ",
|
2018-02-05 16:42:32 +03:00
|
|
|
node.directives ? concat([node.directives, " "]) : "",
|
|
|
|
|
adjustStrings(node.importPath, options),
|
|
|
|
|
node.nodes.length > 0
|
2017-08-21 21:28:22 +02:00
|
|
|
? concat([
|
|
|
|
|
" {",
|
|
|
|
|
indent(
|
|
|
|
|
concat([softline, printNodeSequence(path, options, print)])
|
|
|
|
|
),
|
|
|
|
|
softline,
|
|
|
|
|
"}"
|
|
|
|
|
])
|
|
|
|
|
: ";"
|
2017-06-06 00:28:38 +10:00
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
// postcss-media-query-parser
|
|
|
|
|
case "media-query-list": {
|
|
|
|
|
const parts = [];
|
|
|
|
|
path.each(childPath => {
|
|
|
|
|
const node = childPath.getValue();
|
|
|
|
|
if (node.type === "media-query" && node.value === "") {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
parts.push(childPath.call(print));
|
|
|
|
|
}, "nodes");
|
2018-01-20 02:56:28 +03:00
|
|
|
|
|
|
|
|
return group(indent(join(line, parts)));
|
2017-06-06 00:28:38 +10:00
|
|
|
}
|
|
|
|
|
case "media-query": {
|
2018-02-05 16:42:32 +03:00
|
|
|
const parentNode = path.getParentNode();
|
|
|
|
|
const isLastNode =
|
|
|
|
|
parentNode &&
|
|
|
|
|
parentNode.nodes &&
|
|
|
|
|
parentNode.nodes.indexOf(node) === parentNode.nodes.length - 1;
|
2018-01-20 02:56:28 +03:00
|
|
|
|
|
|
|
|
return concat([
|
|
|
|
|
join(" ", path.map(print, "nodes")),
|
|
|
|
|
isLastNode ? "" : ","
|
|
|
|
|
]);
|
2017-06-06 00:28:38 +10:00
|
|
|
}
|
|
|
|
|
case "media-type": {
|
2018-02-05 16:42:32 +03:00
|
|
|
const atRuleAncestorNode = getAncestorNode(path, "css-atrule");
|
2017-08-17 08:59:56 +02:00
|
|
|
if (
|
2018-02-05 16:42:32 +03:00
|
|
|
atRuleAncestorNode &&
|
|
|
|
|
atRuleAncestorNode.name.toLowerCase() === "charset"
|
2017-08-17 08:59:56 +02:00
|
|
|
) {
|
2018-02-05 16:42:32 +03:00
|
|
|
return node.value;
|
2017-08-17 08:59:56 +02:00
|
|
|
}
|
2018-02-05 16:42:32 +03:00
|
|
|
return adjustNumbers(adjustStrings(node.value, options));
|
2017-06-06 00:28:38 +10:00
|
|
|
}
|
|
|
|
|
case "media-feature-expression": {
|
2018-02-05 16:42:32 +03:00
|
|
|
if (!node.nodes) {
|
|
|
|
|
return node.value;
|
2017-06-06 00:28:38 +10:00
|
|
|
}
|
|
|
|
|
return concat(["(", concat(path.map(print, "nodes")), ")"]);
|
|
|
|
|
}
|
|
|
|
|
case "media-feature": {
|
2017-09-03 21:22:28 +02:00
|
|
|
return maybeToLowerCase(
|
2018-02-05 16:42:32 +03:00
|
|
|
adjustStrings(node.value.replace(/ +/g, " "), options)
|
2017-09-03 21:22:28 +02:00
|
|
|
);
|
2017-06-06 00:28:38 +10:00
|
|
|
}
|
|
|
|
|
case "media-colon": {
|
2018-02-05 16:42:32 +03:00
|
|
|
return concat([node.value, " "]);
|
2017-06-06 00:28:38 +10:00
|
|
|
}
|
|
|
|
|
case "media-value": {
|
2018-02-05 16:42:32 +03:00
|
|
|
return adjustNumbers(adjustStrings(node.value, options));
|
2017-06-06 00:28:38 +10:00
|
|
|
}
|
|
|
|
|
case "media-keyword": {
|
2018-02-05 16:42:32 +03:00
|
|
|
return adjustStrings(node.value, options);
|
2017-06-06 00:28:38 +10:00
|
|
|
}
|
|
|
|
|
case "media-url": {
|
2018-01-23 23:33:19 +03:00
|
|
|
return adjustStrings(
|
2018-02-05 16:42:32 +03:00
|
|
|
node.value.replace(/^url\(\s+/gi, "url(").replace(/\s+\)$/gi, ")"),
|
2018-01-23 23:33:19 +03:00
|
|
|
options
|
|
|
|
|
);
|
2017-06-06 00:28:38 +10:00
|
|
|
}
|
|
|
|
|
case "media-unknown": {
|
2018-02-05 16:42:32 +03:00
|
|
|
return adjustStrings(node.value, options);
|
2017-06-06 00:28:38 +10:00
|
|
|
}
|
|
|
|
|
// postcss-selector-parser
|
2017-09-03 21:22:28 +02:00
|
|
|
case "selector-root-invalid": {
|
|
|
|
|
// This is likely a SCSS nested property: `background: { color: red; }`.
|
2018-02-05 16:42:32 +03:00
|
|
|
return adjustNumbers(
|
|
|
|
|
adjustStrings(maybeToLowerCase(node.value), options)
|
|
|
|
|
);
|
2017-09-03 21:22:28 +02:00
|
|
|
}
|
2017-06-06 00:28:38 +10:00
|
|
|
case "selector-root": {
|
2018-02-05 16:42:32 +03:00
|
|
|
const atRuleAncestorNode = getAncestorNode(path, "css-atrule");
|
|
|
|
|
const insideInExtend =
|
|
|
|
|
atRuleAncestorNode && atRuleAncestorNode.name === "extend";
|
2018-02-02 18:06:47 +03:00
|
|
|
const insideInCustomSelector =
|
2018-02-05 16:42:32 +03:00
|
|
|
atRuleAncestorNode && atRuleAncestorNode.name === "custom-selector";
|
2018-02-02 18:06:47 +03:00
|
|
|
|
|
|
|
|
return group(
|
|
|
|
|
concat([
|
|
|
|
|
insideInCustomSelector
|
2018-02-05 16:42:32 +03:00
|
|
|
? concat([atRuleAncestorNode.customSelector, line])
|
2018-02-02 18:06:47 +03:00
|
|
|
: "",
|
|
|
|
|
join(
|
|
|
|
|
concat([
|
|
|
|
|
",",
|
|
|
|
|
insideInExtend || insideInCustomSelector ? line : hardline
|
|
|
|
|
]),
|
|
|
|
|
path.map(print, "nodes")
|
|
|
|
|
)
|
|
|
|
|
])
|
|
|
|
|
);
|
2017-06-06 00:28:38 +10:00
|
|
|
}
|
|
|
|
|
case "selector-comment": {
|
2018-02-05 16:42:32 +03:00
|
|
|
return node.value;
|
2017-06-06 00:28:38 +10:00
|
|
|
}
|
|
|
|
|
case "selector-string": {
|
2018-02-05 16:42:32 +03:00
|
|
|
return adjustStrings(node.value, options);
|
2017-06-06 00:28:38 +10:00
|
|
|
}
|
|
|
|
|
case "selector-tag": {
|
2018-02-13 18:41:55 +03:00
|
|
|
const lowercasedValue = node.value.toLowerCase();
|
|
|
|
|
const atRuleAncestorNode = getAncestorNode(path, "css-atrule");
|
|
|
|
|
const isKeyframeKeywords =
|
|
|
|
|
atRuleAncestorNode &&
|
|
|
|
|
atRuleAncestorNode.name &&
|
|
|
|
|
atRuleAncestorNode.name.toLowerCase().endsWith("keyframes") &&
|
|
|
|
|
["from", "to"].indexOf(lowercasedValue) !== -1;
|
|
|
|
|
const isHTMLTag = htmlTagNames.indexOf(lowercasedValue) !== -1;
|
|
|
|
|
|
2018-02-12 18:39:03 +03:00
|
|
|
return concat([
|
|
|
|
|
node.namespace
|
|
|
|
|
? concat([node.namespace === true ? "" : node.namespace.trim(), "|"])
|
|
|
|
|
: "",
|
2018-02-13 18:41:55 +03:00
|
|
|
adjustNumbers(
|
|
|
|
|
isHTMLTag || isKeyframeKeywords ? lowercasedValue : node.value
|
|
|
|
|
)
|
2018-02-12 18:39:03 +03:00
|
|
|
]);
|
2017-06-06 00:28:38 +10:00
|
|
|
}
|
|
|
|
|
case "selector-id": {
|
2018-02-05 16:42:32 +03:00
|
|
|
return concat(["#", node.value]);
|
2017-06-06 00:28:38 +10:00
|
|
|
}
|
|
|
|
|
case "selector-class": {
|
2018-02-05 16:42:32 +03:00
|
|
|
return concat([".", adjustNumbers(adjustStrings(node.value, options))]);
|
2017-06-06 00:28:38 +10:00
|
|
|
}
|
|
|
|
|
case "selector-attribute": {
|
|
|
|
|
return concat([
|
|
|
|
|
"[",
|
2018-02-05 16:42:32 +03:00
|
|
|
node.namespace
|
|
|
|
|
? concat([node.namespace === true ? "" : node.namespace.trim(), "|"])
|
2018-01-26 13:00:20 +03:00
|
|
|
: "",
|
2018-02-05 16:42:32 +03:00
|
|
|
node.attribute.trim(),
|
|
|
|
|
node.operator ? node.operator : "",
|
|
|
|
|
node.value
|
|
|
|
|
? quoteAttributeValue(
|
|
|
|
|
adjustStrings(node.value.trim(), options),
|
|
|
|
|
options
|
|
|
|
|
)
|
2017-08-22 18:45:15 +02:00
|
|
|
: "",
|
2018-02-05 16:42:32 +03:00
|
|
|
node.insensitive ? " i" : "",
|
2017-06-06 00:28:38 +10:00
|
|
|
"]"
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
case "selector-combinator": {
|
2018-01-24 14:53:22 +03:00
|
|
|
if (
|
2018-02-05 16:42:32 +03:00
|
|
|
node.value === "+" ||
|
|
|
|
|
node.value === ">" ||
|
|
|
|
|
node.value === "~" ||
|
|
|
|
|
node.value === ">>>"
|
2018-01-24 14:53:22 +03:00
|
|
|
) {
|
2018-02-05 16:42:32 +03:00
|
|
|
const parentNode = path.getParentNode();
|
2017-07-08 14:07:39 +02:00
|
|
|
const leading =
|
2018-02-05 16:42:32 +03:00
|
|
|
parentNode.type === "selector-selector" &&
|
|
|
|
|
parentNode.nodes[0] === node
|
2017-07-08 14:07:39 +02:00
|
|
|
? ""
|
|
|
|
|
: line;
|
2018-02-05 16:42:32 +03:00
|
|
|
const isLastNode =
|
|
|
|
|
parentNode.nodes.length - 1 === parentNode.nodes.indexOf(node);
|
|
|
|
|
return concat([leading, node.value, isLastNode ? "" : " "]);
|
2017-06-06 00:28:38 +10:00
|
|
|
}
|
2018-02-05 16:42:32 +03:00
|
|
|
const leading = node.value.trim().startsWith("(") ? line : "";
|
2017-09-03 21:22:28 +02:00
|
|
|
const value =
|
2018-02-05 16:42:32 +03:00
|
|
|
adjustNumbers(adjustStrings(node.value.trim(), options)) || line;
|
2017-09-03 21:22:28 +02:00
|
|
|
return concat([leading, value]);
|
2017-06-06 00:28:38 +10:00
|
|
|
}
|
|
|
|
|
case "selector-universal": {
|
2018-02-12 18:39:03 +03:00
|
|
|
return concat([
|
|
|
|
|
node.namespace
|
|
|
|
|
? concat([node.namespace === true ? "" : node.namespace.trim(), "|"])
|
|
|
|
|
: "",
|
|
|
|
|
adjustNumbers(node.value)
|
|
|
|
|
]);
|
2017-06-06 00:28:38 +10:00
|
|
|
}
|
|
|
|
|
case "selector-selector": {
|
|
|
|
|
return group(indent(concat(path.map(print, "nodes"))));
|
|
|
|
|
}
|
|
|
|
|
case "selector-pseudo": {
|
|
|
|
|
return concat([
|
2018-02-05 16:42:32 +03:00
|
|
|
maybeToLowerCase(node.value),
|
|
|
|
|
node.nodes && node.nodes.length > 0
|
2017-06-06 00:28:38 +10:00
|
|
|
? concat(["(", join(", ", path.map(print, "nodes")), ")"])
|
|
|
|
|
: ""
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
case "selector-nesting": {
|
2018-02-05 16:42:32 +03:00
|
|
|
return node.value;
|
2017-06-06 00:28:38 +10:00
|
|
|
}
|
|
|
|
|
// postcss-values-parser
|
|
|
|
|
case "value-root": {
|
|
|
|
|
return path.call(print, "group");
|
|
|
|
|
}
|
2018-02-06 18:09:42 +03:00
|
|
|
case "value-comment": {
|
|
|
|
|
return concat(["/*", node.value, "*/"]);
|
|
|
|
|
}
|
2017-06-06 00:28:38 +10:00
|
|
|
case "value-comma_group": {
|
2018-02-05 16:42:32 +03:00
|
|
|
const parentNode = path.getParentNode();
|
|
|
|
|
const declAncestorNode = getAncestorNode(path, "css-decl");
|
|
|
|
|
const declAncestorProp =
|
|
|
|
|
declAncestorNode &&
|
|
|
|
|
declAncestorNode.prop &&
|
|
|
|
|
declAncestorNode.prop.toLowerCase();
|
2017-11-01 15:21:59 -02:00
|
|
|
const isGridValue =
|
2018-02-05 16:42:32 +03:00
|
|
|
declAncestorProp &&
|
|
|
|
|
parentNode.type === "value-value" &&
|
|
|
|
|
(declAncestorProp === "grid" ||
|
|
|
|
|
declAncestorProp.startsWith("grid-template"));
|
|
|
|
|
const atRuleAncestorNode = getAncestorNode(path, "css-atrule");
|
2018-02-02 18:06:47 +03:00
|
|
|
const isControlDirective =
|
2018-02-05 16:42:32 +03:00
|
|
|
atRuleAncestorNode && isNodeControlDirective(atRuleAncestorNode);
|
2017-11-01 15:21:59 -02:00
|
|
|
|
2017-06-06 00:28:38 +10:00
|
|
|
const printed = path.map(print, "groups");
|
|
|
|
|
const parts = [];
|
2018-02-10 20:57:06 +03:00
|
|
|
const isProgid =
|
|
|
|
|
declAncestorNode &&
|
|
|
|
|
declAncestorNode.value.group &&
|
|
|
|
|
declAncestorNode.value.group.group &&
|
|
|
|
|
declAncestorNode.value.group.group.groups &&
|
|
|
|
|
declAncestorNode.value.group.group.groups[0] &&
|
|
|
|
|
declAncestorNode.value.group.group.groups[0].type === "value-word" &&
|
|
|
|
|
declAncestorNode.value.group.group.groups[0].value === "progid";
|
|
|
|
|
|
2017-11-01 15:21:59 -02:00
|
|
|
let didBreak = false;
|
2018-02-05 16:42:32 +03:00
|
|
|
for (let i = 0; i < node.groups.length; ++i) {
|
2017-06-06 00:28:38 +10:00
|
|
|
parts.push(printed[i]);
|
2018-02-02 18:06:47 +03:00
|
|
|
|
2018-02-09 15:28:55 +03:00
|
|
|
// Ignore after latest node (i.e. before semicolon)
|
|
|
|
|
if (!node.groups[i + 1]) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Ignore colon
|
|
|
|
|
if (node.groups[i].value === ":") {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-10 20:57:06 +03:00
|
|
|
if (
|
|
|
|
|
isProgid &&
|
|
|
|
|
node.groups[i].type === "value-word" &&
|
|
|
|
|
node.groups[i].value.endsWith("=")
|
|
|
|
|
) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-09 15:28:55 +03:00
|
|
|
const isMathOperator = isMathOperatorNode(node.groups[i]);
|
|
|
|
|
const isNextMathOperator = isMathOperatorNode(node.groups[i + 1]);
|
|
|
|
|
|
|
|
|
|
// Ignore math operators
|
|
|
|
|
if (
|
|
|
|
|
(isMathOperator &&
|
|
|
|
|
node.groups[i + 1].raws &&
|
|
|
|
|
node.groups[i + 1].raws.before === "") ||
|
|
|
|
|
(isNextMathOperator &&
|
|
|
|
|
node.groups[i + 1].raws &&
|
2018-02-10 20:57:06 +03:00
|
|
|
node.groups[i + 1].raws.before === "") ||
|
|
|
|
|
(isMathOperator &&
|
|
|
|
|
(node.groups[i + 1].type === "value-paren_group" ||
|
|
|
|
|
node.groups[i + 1].type === "value-word" ||
|
|
|
|
|
node.groups[i + 1].type === "value-number" ||
|
|
|
|
|
isMathOperatorNode(node.groups[i + 1])) &&
|
|
|
|
|
(!node.groups[i - 1] ||
|
|
|
|
|
(node.groups[i - 1] && isMathOperatorNode(node.groups[i - 1]))))
|
2018-02-09 15:28:55 +03:00
|
|
|
) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Ignore `@` in Less (i.e. `@@var;`)
|
|
|
|
|
if (
|
|
|
|
|
node.groups[i].type === "value-atword" &&
|
|
|
|
|
node.groups[i].value === ""
|
|
|
|
|
) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Ignore `~` in Less (i.e. `content: ~"^//* some horrible but needed css hack";`)
|
|
|
|
|
if (node.groups[i].value === "~") {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Ignore interpolation in SCSS (i.e. ``#{variable}``)
|
|
|
|
|
if (
|
2018-02-10 20:57:06 +03:00
|
|
|
(node.groups[i].value === "#" &&
|
|
|
|
|
node.groups[i].type === "value-word") ||
|
|
|
|
|
(node.groups[i].value === "{" &&
|
|
|
|
|
node.groups[i].type === "value-word") ||
|
|
|
|
|
(node.groups[i + 1].value === "}" &&
|
|
|
|
|
node.groups[i + 1].type === "value-word") ||
|
2018-02-10 02:05:48 +03:00
|
|
|
(node.groups[i + 1].value === "{" &&
|
2018-02-10 20:57:06 +03:00
|
|
|
node.groups[i + 1].type === "value-word" &&
|
2018-02-10 02:05:48 +03:00
|
|
|
node.groups[i + 1].raws &&
|
|
|
|
|
node.groups[i + 1].raws.before === "") ||
|
|
|
|
|
(node.groups[i].value === "}" &&
|
2018-02-10 20:57:06 +03:00
|
|
|
node.groups[i].type === "value-word" &&
|
2018-02-10 02:05:48 +03:00
|
|
|
node.groups[i + 1].raws &&
|
|
|
|
|
node.groups[i + 1].raws.before === "")
|
2018-02-09 15:28:55 +03:00
|
|
|
) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const isEqualityOperator =
|
|
|
|
|
isControlDirective && isEqualityOperatorNode(node.groups[i]);
|
|
|
|
|
const isRelationalOperator =
|
|
|
|
|
isControlDirective && isRelationalOperatorNode(node.groups[i]);
|
|
|
|
|
const isNextEqualityOperator =
|
|
|
|
|
isControlDirective && isEqualityOperatorNode(node.groups[i + 1]);
|
|
|
|
|
const isNextRelationalOperator =
|
|
|
|
|
isControlDirective && isRelationalOperatorNode(node.groups[i + 1]);
|
|
|
|
|
const isNextIfElseKeyword =
|
|
|
|
|
isControlDirective && isIfElseKeywordNode(node.groups[i + 1]);
|
|
|
|
|
const isEachKeyword =
|
|
|
|
|
isControlDirective && isEachKeywordNode(node.groups[i]);
|
|
|
|
|
const isNextEachKeyword =
|
|
|
|
|
isControlDirective && isEachKeywordNode(node.groups[i + 1]);
|
|
|
|
|
const isForKeyword =
|
|
|
|
|
atRuleAncestorNode &&
|
|
|
|
|
atRuleAncestorNode.name === "for" &&
|
|
|
|
|
isForKeywordNode(node.groups[i]);
|
|
|
|
|
const isNextForKeyword =
|
|
|
|
|
isControlDirective && isForKeywordNode(node.groups[i + 1]);
|
|
|
|
|
const IsNextColon = node.groups[i + 1].value === ":";
|
|
|
|
|
|
|
|
|
|
if (isGridValue) {
|
|
|
|
|
if (
|
|
|
|
|
node.groups[i].source.start.line !==
|
|
|
|
|
node.groups[i + 1].source.start.line
|
2017-06-06 00:28:38 +10:00
|
|
|
) {
|
2018-02-09 15:28:55 +03:00
|
|
|
parts.push(hardline);
|
|
|
|
|
didBreak = true;
|
|
|
|
|
} else {
|
2017-06-06 00:28:38 +10:00
|
|
|
parts.push(" ");
|
|
|
|
|
}
|
2018-02-09 15:28:55 +03:00
|
|
|
} else if (
|
|
|
|
|
isNextMathOperator ||
|
|
|
|
|
isNextEqualityOperator ||
|
|
|
|
|
isNextRelationalOperator ||
|
|
|
|
|
isNextIfElseKeyword ||
|
|
|
|
|
isForKeyword ||
|
|
|
|
|
isEachKeyword
|
|
|
|
|
) {
|
|
|
|
|
parts.push(" ");
|
|
|
|
|
} else if (
|
|
|
|
|
!IsNextColon ||
|
|
|
|
|
isEqualityOperator ||
|
|
|
|
|
isRelationalOperator ||
|
|
|
|
|
isNextForKeyword ||
|
|
|
|
|
isNextEachKeyword
|
|
|
|
|
) {
|
|
|
|
|
parts.push(line);
|
2017-06-06 00:28:38 +10:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-01 15:21:59 -02:00
|
|
|
if (didBreak) {
|
|
|
|
|
parts.unshift(hardline);
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-02 18:06:47 +03:00
|
|
|
if (isControlDirective) {
|
|
|
|
|
return group(indent(concat(parts)));
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-10 20:57:06 +03:00
|
|
|
return group(isProgid ? fill(parts) : indent(fill(parts)));
|
2017-06-06 00:28:38 +10:00
|
|
|
}
|
|
|
|
|
case "value-paren_group": {
|
2018-02-05 16:42:32 +03:00
|
|
|
const parentNode = path.getParentNode();
|
2017-06-06 00:28:38 +10:00
|
|
|
const isURLCall =
|
2018-02-05 16:42:32 +03:00
|
|
|
parentNode &&
|
|
|
|
|
parentNode.type === "value-func" &&
|
|
|
|
|
parentNode.value === "url";
|
2017-06-06 00:28:38 +10:00
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
isURLCall &&
|
2018-02-05 16:42:32 +03:00
|
|
|
(node.groups.length === 1 ||
|
|
|
|
|
(node.groups.length > 0 &&
|
|
|
|
|
node.groups[0].type === "value-comma_group" &&
|
|
|
|
|
node.groups[0].groups.length > 0 &&
|
|
|
|
|
node.groups[0].groups[0].type === "value-word" &&
|
|
|
|
|
node.groups[0].groups[0].value.startsWith("data:")))
|
2017-06-06 00:28:38 +10:00
|
|
|
) {
|
|
|
|
|
return concat([
|
2018-02-05 16:42:32 +03:00
|
|
|
node.open ? path.call(print, "open") : "",
|
2017-06-06 00:28:38 +10:00
|
|
|
join(",", path.map(print, "groups")),
|
2018-02-05 16:42:32 +03:00
|
|
|
node.close ? path.call(print, "close") : ""
|
2017-06-06 00:28:38 +10:00
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-05 16:42:32 +03:00
|
|
|
if (!node.open) {
|
2017-06-21 15:13:51 -07:00
|
|
|
const printed = path.map(print, "groups");
|
|
|
|
|
const res = [];
|
|
|
|
|
|
|
|
|
|
for (let i = 0; i < printed.length; i++) {
|
|
|
|
|
if (i !== 0) {
|
|
|
|
|
res.push(concat([",", line]));
|
|
|
|
|
}
|
|
|
|
|
res.push(printed[i]);
|
|
|
|
|
}
|
|
|
|
|
return group(indent(fill(res)));
|
2017-06-06 00:28:38 +10:00
|
|
|
}
|
|
|
|
|
|
2018-02-05 16:42:32 +03:00
|
|
|
const decl = path.getParentNode(2);
|
2017-10-21 21:35:22 -04:00
|
|
|
const isMap =
|
2018-02-05 16:42:32 +03:00
|
|
|
decl && decl.type === "css-decl" && decl.prop.startsWith("$");
|
2017-10-21 21:35:22 -04:00
|
|
|
|
2017-06-06 00:28:38 +10:00
|
|
|
return group(
|
|
|
|
|
concat([
|
2018-02-05 16:42:32 +03:00
|
|
|
node.open ? path.call(print, "open") : "",
|
2017-06-06 00:28:38 +10:00
|
|
|
indent(
|
|
|
|
|
concat([
|
|
|
|
|
softline,
|
2017-10-21 21:35:22 -04:00
|
|
|
join(
|
|
|
|
|
concat([",", isMap ? hardline : line]),
|
|
|
|
|
path.map(print, "groups")
|
|
|
|
|
)
|
2017-06-06 00:28:38 +10:00
|
|
|
])
|
|
|
|
|
),
|
|
|
|
|
softline,
|
2018-02-05 16:42:32 +03:00
|
|
|
node.close ? path.call(print, "close") : ""
|
2017-06-06 00:28:38 +10:00
|
|
|
])
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
case "value-value": {
|
|
|
|
|
return path.call(print, "group");
|
|
|
|
|
}
|
|
|
|
|
case "value-func": {
|
2018-02-05 16:42:32 +03:00
|
|
|
return concat([node.value, path.call(print, "group")]);
|
2017-06-06 00:28:38 +10:00
|
|
|
}
|
|
|
|
|
case "value-paren": {
|
2018-02-05 16:42:32 +03:00
|
|
|
return node.value;
|
2017-06-06 00:28:38 +10:00
|
|
|
}
|
|
|
|
|
case "value-number": {
|
2018-02-05 16:42:32 +03:00
|
|
|
return concat([printNumber(node.value), maybeToLowerCase(node.unit)]);
|
2017-06-06 00:28:38 +10:00
|
|
|
}
|
|
|
|
|
case "value-operator": {
|
2018-02-05 16:42:32 +03:00
|
|
|
return node.value;
|
2017-06-06 00:28:38 +10:00
|
|
|
}
|
|
|
|
|
case "value-word": {
|
2018-02-05 16:42:32 +03:00
|
|
|
if ((node.isColor && node.isHex) || isWideKeywords(node.value)) {
|
|
|
|
|
return node.value.toLowerCase();
|
2017-06-20 23:07:31 +10:00
|
|
|
}
|
2018-02-05 16:42:32 +03:00
|
|
|
return node.value;
|
2017-06-06 00:28:38 +10:00
|
|
|
}
|
|
|
|
|
case "value-colon": {
|
2018-02-07 13:33:10 +03:00
|
|
|
const parent = path.getParentNode();
|
|
|
|
|
const index =
|
|
|
|
|
parent &&
|
|
|
|
|
parent.groups &&
|
|
|
|
|
parent.groups.length > 0 &&
|
|
|
|
|
parent.groups.indexOf(node);
|
|
|
|
|
const prevProgidProperty =
|
|
|
|
|
index && parent.groups[index - 1].value === "progid";
|
|
|
|
|
const valueFuncAncestorNode = getAncestorNode(path, "value-func");
|
|
|
|
|
const insideInURLFunc =
|
|
|
|
|
valueFuncAncestorNode && valueFuncAncestorNode.value === "url";
|
|
|
|
|
|
|
|
|
|
return concat([
|
|
|
|
|
node.value,
|
|
|
|
|
prevProgidProperty || insideInURLFunc ? "" : line
|
|
|
|
|
]);
|
2017-06-06 00:28:38 +10:00
|
|
|
}
|
|
|
|
|
case "value-comma": {
|
2018-02-05 16:42:32 +03:00
|
|
|
return concat([node.value, " "]);
|
2017-06-06 00:28:38 +10:00
|
|
|
}
|
|
|
|
|
case "value-string": {
|
2018-02-09 13:17:48 +01:00
|
|
|
return privateUtil.printString(
|
2018-02-05 16:42:32 +03:00
|
|
|
node.raws.quote + node.value + node.raws.quote,
|
|
|
|
|
options
|
|
|
|
|
);
|
2017-06-06 00:28:38 +10:00
|
|
|
}
|
|
|
|
|
case "value-atword": {
|
2018-02-05 16:42:32 +03:00
|
|
|
return concat(["@", node.value]);
|
2017-06-06 00:28:38 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
default:
|
2017-09-03 22:40:44 +02:00
|
|
|
/* istanbul ignore next */
|
2018-02-05 16:42:32 +03:00
|
|
|
throw new Error("unknown postcss type: " + JSON.stringify(node.type));
|
2017-06-06 00:28:38 +10:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-07 13:33:10 +03:00
|
|
|
function isForKeywordNode(node) {
|
|
|
|
|
return (
|
|
|
|
|
node.type &&
|
|
|
|
|
node.type === "value-word" &&
|
|
|
|
|
node.value &&
|
|
|
|
|
["from", "through", "end"].indexOf(node.value) !== -1
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function isIfElseKeywordNode(node) {
|
|
|
|
|
return (
|
|
|
|
|
node.type &&
|
|
|
|
|
node.type === "value-word" &&
|
|
|
|
|
node.value &&
|
|
|
|
|
["and", "or", "not"].indexOf(node.value) !== -1
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function isEachKeywordNode(node) {
|
|
|
|
|
return (
|
|
|
|
|
node.type &&
|
|
|
|
|
node.type === "value-word" &&
|
|
|
|
|
node.value &&
|
|
|
|
|
["in"].indexOf(node.value) !== -1
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function isMathOperatorNode(node) {
|
|
|
|
|
return (
|
|
|
|
|
node.type &&
|
|
|
|
|
node.type === "value-operator" &&
|
|
|
|
|
node.value &&
|
|
|
|
|
["+", "-", "/", "*", "%"].indexOf(node.value) !== -1
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function isEqualityOperatorNode(node) {
|
|
|
|
|
return (
|
|
|
|
|
node.type &&
|
|
|
|
|
node.type === "value-word" &&
|
|
|
|
|
node.value &&
|
|
|
|
|
["==", "!="].indexOf(node.value) !== -1
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function isRelationalOperatorNode(node) {
|
|
|
|
|
return (
|
|
|
|
|
node.type &&
|
|
|
|
|
node.type === "value-word" &&
|
|
|
|
|
node.value &&
|
|
|
|
|
["<", ">", "<=", ">="].indexOf(node.value) !== -1
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-05 16:42:32 +03:00
|
|
|
function isNodeControlDirective(node) {
|
|
|
|
|
return (
|
|
|
|
|
node.type &&
|
|
|
|
|
node.type === "css-atrule" &&
|
|
|
|
|
node.name &&
|
|
|
|
|
(node.name === "if" ||
|
|
|
|
|
node.name === "else" ||
|
|
|
|
|
node.name === "for" ||
|
|
|
|
|
node.name === "each" ||
|
|
|
|
|
node.name === "while")
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-02 18:06:47 +03:00
|
|
|
function getAncestorCounter(path, typeOrTypes) {
|
|
|
|
|
const types = [].concat(typeOrTypes);
|
|
|
|
|
|
|
|
|
|
let counter = -1;
|
|
|
|
|
let ancestorNode;
|
|
|
|
|
|
|
|
|
|
while ((ancestorNode = path.getParentNode(++counter))) {
|
|
|
|
|
if (types.indexOf(ancestorNode.type) !== -1) {
|
|
|
|
|
return counter;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getAncestorNode(path, typeOrTypes) {
|
|
|
|
|
const counter = getAncestorCounter(path, typeOrTypes);
|
|
|
|
|
return counter === -1 ? null : path.getParentNode(counter);
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-06 00:28:38 +10:00
|
|
|
function printNodeSequence(path, options, print) {
|
|
|
|
|
const node = path.getValue();
|
|
|
|
|
const parts = [];
|
|
|
|
|
let i = 0;
|
|
|
|
|
path.map(pathChild => {
|
2017-06-11 00:45:59 +10:00
|
|
|
const prevNode = node.nodes[i - 1];
|
|
|
|
|
if (
|
|
|
|
|
prevNode &&
|
|
|
|
|
prevNode.type === "css-comment" &&
|
|
|
|
|
prevNode.text.trim() === "prettier-ignore"
|
|
|
|
|
) {
|
|
|
|
|
const childNode = pathChild.getValue();
|
|
|
|
|
parts.push(
|
|
|
|
|
options.originalText.slice(
|
2018-02-09 13:17:48 +01:00
|
|
|
options.locStart(childNode),
|
|
|
|
|
options.locEnd(childNode)
|
2017-06-11 00:45:59 +10:00
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
parts.push(pathChild.call(print));
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-06 00:28:38 +10:00
|
|
|
if (i !== node.nodes.length - 1) {
|
|
|
|
|
if (
|
2017-06-11 00:40:05 +10:00
|
|
|
(node.nodes[i + 1].type === "css-comment" &&
|
2018-02-09 13:17:48 +01:00
|
|
|
!privateUtil.hasNewline(
|
2017-06-11 00:40:05 +10:00
|
|
|
options.originalText,
|
2018-02-09 13:17:48 +01:00
|
|
|
options.locStart(node.nodes[i + 1]),
|
2017-06-11 00:40:05 +10:00
|
|
|
{ backwards: true }
|
|
|
|
|
)) ||
|
|
|
|
|
(node.nodes[i + 1].type === "css-atrule" &&
|
2017-08-08 05:07:47 +03:00
|
|
|
node.nodes[i + 1].name === "else" &&
|
|
|
|
|
node.nodes[i].type !== "css-comment")
|
2017-06-06 00:28:38 +10:00
|
|
|
) {
|
|
|
|
|
parts.push(" ");
|
|
|
|
|
} else {
|
|
|
|
|
parts.push(hardline);
|
2018-02-09 13:17:48 +01:00
|
|
|
if (
|
|
|
|
|
sharedUtil.isNextLineEmpty(
|
|
|
|
|
options.originalText,
|
|
|
|
|
pathChild.getValue(),
|
|
|
|
|
options
|
|
|
|
|
)
|
|
|
|
|
) {
|
2017-06-06 00:28:38 +10:00
|
|
|
parts.push(hardline);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
i++;
|
|
|
|
|
}, "nodes");
|
|
|
|
|
|
|
|
|
|
return concat(parts);
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-14 08:57:16 +02:00
|
|
|
const STRING_REGEX = /(['"])(?:(?!\1)[^\\]|\\[\s\S])*\1/g;
|
2017-08-15 17:25:57 +02:00
|
|
|
const NUMBER_REGEX = /(?:\d*\.\d+|\d+\.?)(?:[eE][+-]?\d+)?/g;
|
2017-09-03 21:22:28 +02:00
|
|
|
const STANDARD_UNIT_REGEX = /[a-zA-Z]+/g;
|
|
|
|
|
const WORD_PART_REGEX = /[$@]?[a-zA-Z_\u0080-\uFFFF][\w\-\u0080-\uFFFF]*/g;
|
|
|
|
|
const ADJUST_NUMBERS_REGEX = RegExp(
|
|
|
|
|
STRING_REGEX.source +
|
|
|
|
|
`|` +
|
|
|
|
|
`(${WORD_PART_REGEX.source})?` +
|
|
|
|
|
`(${NUMBER_REGEX.source})` +
|
|
|
|
|
`(${STANDARD_UNIT_REGEX.source})?`,
|
2017-08-15 17:25:57 +02:00
|
|
|
"g"
|
|
|
|
|
);
|
2017-08-14 08:57:16 +02:00
|
|
|
|
|
|
|
|
function adjustStrings(value, options) {
|
2018-02-09 13:17:48 +01:00
|
|
|
return value.replace(STRING_REGEX, match =>
|
|
|
|
|
privateUtil.printString(match, options)
|
|
|
|
|
);
|
2017-08-14 08:57:16 +02:00
|
|
|
}
|
|
|
|
|
|
2017-08-22 18:45:15 +02:00
|
|
|
function quoteAttributeValue(value, options) {
|
|
|
|
|
const quote = options.singleQuote ? "'" : '"';
|
|
|
|
|
return value.includes('"') || value.includes("'")
|
|
|
|
|
? value
|
|
|
|
|
: quote + value + quote;
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-15 17:25:57 +02:00
|
|
|
function adjustNumbers(value) {
|
|
|
|
|
return value.replace(
|
2017-09-03 21:22:28 +02:00
|
|
|
ADJUST_NUMBERS_REGEX,
|
|
|
|
|
(match, quote, wordPart, number, unit) =>
|
|
|
|
|
!wordPart && number
|
|
|
|
|
? (wordPart || "") + printNumber(number) + maybeToLowerCase(unit || "")
|
|
|
|
|
: match
|
2017-08-15 17:25:57 +02:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function printNumber(rawNumber) {
|
|
|
|
|
return (
|
2018-02-09 13:17:48 +01:00
|
|
|
privateUtil
|
2017-08-15 17:25:57 +02:00
|
|
|
.printNumber(rawNumber)
|
|
|
|
|
// Remove trailing `.0`.
|
|
|
|
|
.replace(/\.0(?=$|e)/, "")
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-03 21:22:28 +02:00
|
|
|
function maybeToLowerCase(value) {
|
2017-09-14 21:55:11 +02:00
|
|
|
return value.includes("$") ||
|
2017-09-16 20:24:08 +10:00
|
|
|
value.includes("@") ||
|
|
|
|
|
value.includes("#") ||
|
2017-09-21 20:23:22 +02:00
|
|
|
value.startsWith("%") ||
|
2018-01-19 17:39:44 +03:00
|
|
|
value.startsWith("--") ||
|
|
|
|
|
(value.includes("(") && value.includes(")"))
|
2017-09-03 21:22:28 +02:00
|
|
|
? value
|
|
|
|
|
: value.toLowerCase();
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-23 18:10:39 +03:00
|
|
|
function isWideKeywords(value) {
|
|
|
|
|
return (
|
|
|
|
|
["initial", "inherit", "unset", "revert"].indexOf(
|
|
|
|
|
value.replace().toLowerCase()
|
|
|
|
|
) !== -1
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-26 12:23:50 +11:00
|
|
|
module.exports = {
|
|
|
|
|
print: genericPrint,
|
2018-02-09 13:17:48 +01:00
|
|
|
hasPrettierIgnore: privateUtil.hasIgnoreComment,
|
2017-12-30 21:30:09 +11:00
|
|
|
massageAstNode: clean
|
2017-12-26 12:23:50 +11:00
|
|
|
};
|