Skip to content

Commit

Permalink
fix: correct calculation of pseudo-class selectors
Browse files Browse the repository at this point in the history
  • Loading branch information
isellsoap committed Mar 16, 2021
1 parent 18bd48b commit f1f3653
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 15 deletions.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -26,7 +26,7 @@
"javascript-natural-sort": "0.7.1",
"lodash": "4.17.10",
"normalize.css": "8.0.0",
"specificity": "0.4.1",
"parsel-js": "^1.0.1",
"webfontloader": "1.6.28"
},
"devDependencies": {
Expand Down
30 changes: 17 additions & 13 deletions src/scripts/main.js
Expand Up @@ -4,7 +4,7 @@ import naturalSort from 'javascript-natural-sort';
import uniq from 'lodash/uniq';
import indexOf from 'lodash/indexOf';
import postcss from 'postcss';
import {calculate} from 'specificity';
import { specificity } from 'parsel-js';

import {
renderMainChart,
Expand Down Expand Up @@ -38,9 +38,7 @@ const isValidAtRule = (rule) =>

const isValidSelector = (rule) => rule.parent.type === 'root' || isValidAtRule(rule);

const removeInlineStyleCategory = (str) => str.replace('0,', '');

// build up comma-separted string with all valid selectors
// build up array with all valid selectors
const getAllSelectors = () => {
const selectors = [];

Expand All @@ -50,7 +48,7 @@ const getAllSelectors = () => {
}
});

return selectors.join();
return selectors;
};

const getSortedKeysByValue = (obj) => Object.keys(obj).sort((keyA, keyB) => -(obj[keyA] - obj[keyB]));
Expand Down Expand Up @@ -88,26 +86,32 @@ elementForm.addEventListener('submit', (event) => {
elementErrorMessage.classList.add(classFormErrorHidden);

// calculate data for main chart
specificities = calculate(getAllSelectors());
specificities = getAllSelectors().map((selector) => {
return {
selector,
specificity: specificity(selector).join()
};
});

mainChartYAxisCategories = uniq(
specificities.map((element) => removeInlineStyleCategory(element.specificity))
specificities.map((element) => element.specificity)
).sort(naturalSort);

specificities.forEach((element, index) => {
if (element.specificity.indexOf('0,0,0,') === 0) {
if (element.specificity.indexOf('0,0,') === 0) {
mainChartDataSeries.elementCategory.push([
index,
indexOf(mainChartYAxisCategories, removeInlineStyleCategory(element.specificity))
indexOf(mainChartYAxisCategories, element.specificity)
]);
} else if (element.specificity.indexOf('0,0,') === 0) {
} else if (element.specificity.indexOf('0,') === 0) {
mainChartDataSeries.classCategory.push([
index,
indexOf(mainChartYAxisCategories, removeInlineStyleCategory(element.specificity))
indexOf(mainChartYAxisCategories, element.specificity)
]);
} else {
mainChartDataSeries.idCategory.push([
index,
indexOf(mainChartYAxisCategories, removeInlineStyleCategory(element.specificity))
indexOf(mainChartYAxisCategories, element.specificity)
]);
}

Expand All @@ -116,7 +120,7 @@ elementForm.addEventListener('submit', (event) => {

specificityUsagesDataSeries = getSortedKeysByValue(specificityUsages).map((element) => {
return {
name: removeInlineStyleCategory(element),
name: element,
y: specificityUsages[element]
};
});
Expand Down
7 changes: 6 additions & 1 deletion yarn.lock
Expand Up @@ -4415,6 +4415,11 @@ parse-json@^4.0.0:
error-ex "^1.3.1"
json-parse-better-errors "^1.0.1"

parsel-js@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/parsel-js/-/parsel-js-1.0.1.tgz#0a48ab55f5bd2adc9a2924f60c16d92eaddceab4"
integrity sha512-4K2JCGQ0c19sxMvjx5LUriTJJDtweWw2Zs/adjl0QIKPgYuawVXEMv1SsPd2qwpP83xHn93cOJho6agHiTaHtA==

parseurl@~1.3.2:
version "1.3.2"
resolved "https://registry.yarnpkg.com/parseurl/-/parseurl-1.3.2.tgz#fc289d4ed8993119460c156253262cdc8de65bf3"
Expand Down Expand Up @@ -5568,7 +5573,7 @@ spdy@^3.4.1:
select-hose "^2.0.0"
spdy-transport "^2.0.18"

specificity@0.4.1, specificity@^0.4.0:
specificity@^0.4.0:
version "0.4.1"
resolved "https://registry.yarnpkg.com/specificity/-/specificity-0.4.1.tgz#aab5e645012db08ba182e151165738d00887b019"

Expand Down

0 comments on commit f1f3653

Please sign in to comment.