Skip to content

feat: handle selector groups #247

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

Merged
merged 3 commits into from
Oct 16, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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
36 changes: 17 additions & 19 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ const postcss = require('postcss');
const parser = require('postcss-selector-parser');
const {name} = require('../package.json');


/**
* Ensure that attributes with different quotes match.
* @param {Object} selector - postcss selector node
Expand Down Expand Up @@ -32,6 +31,8 @@ function sortGroups(selector) {
return a.value < b.value ? -1 : 1;
});
});

selector.sort((a, b) => (a.nodes.join('') < b.nodes.join('') ? -1 : 1));
}

/**
Expand All @@ -50,12 +51,10 @@ function removeDupProperties(selector) {
}
}

const uniformStyle = parser(
(selector) => {
normalizeAttributes(selector);
sortGroups(selector);
}
);
const uniformStyle = parser((selector) => {
normalizeAttributes(selector);
sortGroups(selector);
});

const defaultOptions = {
removeDuplicatedProperties: false,
Expand All @@ -74,25 +73,24 @@ module.exports = postcss.plugin(name, (options) => {
// Check selector parent for any at rule
if (rule.parent.type === 'atrule') {
// Use name and query params as the key
const query = rule.parent.name.toLowerCase()
+ rule.parent.params.replace(/\s+/g, '');
const query =
rule.parent.name.toLowerCase() +
rule.parent.params.replace(/\s+/g, '');

// See if this query key is already in the map table
map = mapTable.has(query) ?
// If it is use it
mapTable.get(query) :
// if not set it and get it
mapTable.set(query, new Map()).get(query);
map = mapTable.has(query)
? // If it is use it
mapTable.get(query)
: // if not set it and get it
mapTable.set(query, new Map()).get(query);
} else {
// Otherwise we are dealing with a selector in the root
map = mapTable.get('root');
}

const selector = uniformStyle.processSync(
rule.selector, {
lossless: false,
}
);
const selector = uniformStyle.processSync(rule.selector, {
lossless: false,
});

if (map.has(selector)) {
// store original rule as destination
Expand Down
14 changes: 14 additions & 0 deletions test/duplicated-css.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,20 @@ test(
'.one.two {}'
);

test(
'selector groups',
css,
'.one .two, .one .three {} .one .two, .one .three {}',
'.one .two, .one .three {}'
);

test(
'selector groups with different order',
css,
'.one .two, .one .three {} .one .three, .one .two {}',
'.one .two, .one .three {}'
);

test(
'selectors and seperately selectors within media query',
css,
Expand Down