postcss-unique-selectors
Advanced tools
Comparing version 7.0.0 to 7.0.1
{ | ||
"name": "postcss-unique-selectors", | ||
"version": "7.0.0", | ||
"version": "7.0.1", | ||
"description": "Ensure CSS selectors are unique.", | ||
@@ -26,3 +26,3 @@ "main": "src/index.js", | ||
"dependencies": { | ||
"postcss-selector-parser": "^6.0.16" | ||
"postcss-selector-parser": "^6.1.0" | ||
}, | ||
@@ -29,0 +29,0 @@ "bugs": { |
@@ -6,19 +6,41 @@ 'use strict'; | ||
* @param {string} selectors | ||
* @param {selectorParser.SyncProcessor<void>} callback | ||
* @return {string} | ||
*/ | ||
function parseSelectors(selectors, callback) { | ||
return selectorParser(callback).processSync(selectors); | ||
} | ||
function generateUniqueSelector(selectors) { | ||
/** @type {Map<string, string>} */ | ||
const uniqueSelectors = new Map(); | ||
/** | ||
* @param {import('postcss').Rule} rule | ||
* @return {string} | ||
*/ | ||
function unique(rule) { | ||
const selector = [...new Set(rule.selectors)]; | ||
selector.sort(); | ||
return selector.join(); | ||
/** @type {selectorParser.SyncProcessor<void>} */ | ||
const collectUniqueSelectors = (selNode) => { | ||
for (const node of selNode.nodes) { | ||
/** @type {string[]} */ | ||
const comments = []; | ||
// Duplicates are removed by stripping the comments and using the results as the Map key. | ||
const keyNode = node.clone(); | ||
keyNode.walk((sel) => { | ||
if (sel.type === 'comment') { | ||
comments.push(sel.value); | ||
sel.remove(); | ||
} | ||
}); | ||
const key = keyNode.toString().trim(); | ||
const dupeSelector = uniqueSelectors.get(key); | ||
if (!dupeSelector) { | ||
uniqueSelectors.set(key, node.toString()); | ||
} else if (comments.length) { | ||
// If the duplicate selector has a comment, it is concatenated to the end of the selector. | ||
uniqueSelectors.set(key, `${dupeSelector}${comments.join('')}`); | ||
} | ||
} | ||
}; | ||
selectorParser(collectUniqueSelectors).processSync(selectors); | ||
return [...uniqueSelectors.entries()] | ||
.sort(([a], [b]) => (a > b ? 1 : a < b ? -1 : 0)) | ||
.map(([, selector]) => selector) | ||
.join(); | ||
} | ||
/** | ||
@@ -33,23 +55,9 @@ * @type {import('postcss').PluginCreator<void>} | ||
css.walkRules((nodes) => { | ||
/** @type {string[]} */ | ||
let comments = []; | ||
/** @type {selectorParser.SyncProcessor<void>} */ | ||
const removeAndSaveComments = (selNode) => { | ||
selNode.walk((sel) => { | ||
if (sel.type === 'comment') { | ||
comments.push(sel.value); | ||
sel.remove(); | ||
return; | ||
} else { | ||
return; | ||
} | ||
}); | ||
}; | ||
if (nodes.raws.selector && nodes.raws.selector.raw) { | ||
parseSelectors(nodes.raws.selector.raw, removeAndSaveComments); | ||
nodes.raws.selector.raw = unique(nodes); | ||
nodes.raws.selector.raw = generateUniqueSelector( | ||
nodes.raws.selector.raw | ||
); | ||
} else { | ||
nodes.selector = generateUniqueSelector(nodes.selector); | ||
} | ||
nodes.selector = parseSelectors(nodes.selector, removeAndSaveComments); | ||
nodes.selector = unique(nodes); | ||
nodes.selectors = nodes.selectors.concat(comments); | ||
}); | ||
@@ -56,0 +64,0 @@ }, |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
4979
69