Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(postcss-merge-longhand): Incorrect merging of border declarations #552

Merged
merged 1 commit into from Jul 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions packages/postcss-merge-longhand/src/__tests__/borders.js
Expand Up @@ -352,3 +352,16 @@ trbl.forEach(direction => {
`h1{border-${direction}:var(--variable)}`,
);
});

test(
'Should correctly merge border declarations (#551) (1)',
processCSS,
'h1{border:1px solid black;border-top-width:2px;border-right-width:2px;border-bottom-width:2px}',
'h1{border:solid black;border-width:2px 2px 2px 1px}',
);

test(
'Should correctly merge border declarations (#551) (2)',
passthroughCSS,
'h1{border:none;border-top:6px solid #000;border-bottom:1px solid #fff}',
);
15 changes: 10 additions & 5 deletions packages/postcss-merge-longhand/src/lib/decl/borders.js
Expand Up @@ -13,6 +13,7 @@ import remove from '../remove';
import trbl from '../trbl';
import isCustomProp from '../isCustomProp';
import canExplode from '../canExplode';
import getLastNode from '../getLastNode';

const wsc = ['width', 'style', 'color'];
const defaults = ['medium', 'none', 'currentColor'];
Expand Down Expand Up @@ -252,19 +253,18 @@ function merge (rule) {
const none = 'none none currentColor';

if (reduced.length === 2 && reduced[0] === none || reduced[1] === none) {
const noOfNones = mapped.filter(value => value === none).length;
rule.insertBefore(lastNode, Object.assign(lastNode.clone(), {
prop: 'border',
value: noOfNones > 2 ? 'none' : mapped.filter(value => value !== none)[0],
value: mapped[1] === none ? 'none' : mapped.filter(value => value !== none)[0],
}));
directions.forEach((dir, i) => {
if (noOfNones > 2 && mapped[i] !== none) {
if (mapped[1] === none && mapped[i] !== none) {
rule.insertBefore(lastNode, Object.assign(lastNode.clone(), {
prop: dir,
value: mapped[i],
}));
}
if (noOfNones <= 2 && mapped[i] === none) {
if (mapped[1] !== none && mapped[i] === none) {
rule.insertBefore(lastNode, Object.assign(lastNode.clone(), {
prop: dir,
value: 'none',
Expand All @@ -282,7 +282,12 @@ function merge (rule) {
const lastNode = decls[decls.length - 1];
wsc.forEach((d, i) => {
const names = directions.filter(name => name !== lastNode.prop).map(name => `${name}-${d}`);
const props = rule.nodes.filter(node => node.prop && ~names.indexOf(node.prop) && node.important === lastNode.important);
let nodes = rule.nodes.slice(0, rule.nodes.indexOf(lastNode));
const border = getLastNode(nodes, 'border');
if (border) {
nodes = nodes.slice(nodes.indexOf(border));
}
const props = nodes.filter(node => node.prop && ~names.indexOf(node.prop) && node.important === lastNode.important);
const rules = getRules(props, names);
if (hasAllProps(rules, ...names) && !rules.some(detect)) {
const values = rules.map(node => node ? node.value : null);
Expand Down
2 changes: 1 addition & 1 deletion packages/postcss-merge-longhand/src/lib/getLastNode.js
@@ -1,3 +1,3 @@
export default (rule, prop) => {
return rule.filter(n => n.prop && ~n.prop.indexOf(prop)).pop();
return rule.filter(n => n.prop && n.prop === prop).pop();
};