Opal-Estate-Pro/node_modules/csso/lib/compressor/restructure/prepare/specificity.js

49 lines
1.1 KiB
JavaScript
Raw Normal View History

2019-09-13 06:27:52 +02:00
module.exports = function specificity(simpleSelector) {
var A = 0;
var B = 0;
var C = 0;
simpleSelector.sequence.each(function walk(data) {
switch (data.type) {
case 'SimpleSelector':
case 'Negation':
data.sequence.each(walk);
break;
case 'Id':
A++;
break;
case 'Class':
case 'Attribute':
case 'FunctionalPseudo':
B++;
break;
case 'Identifier':
if (data.name !== '*') {
C++;
}
break;
case 'PseudoElement':
C++;
break;
case 'PseudoClass':
var name = data.name.toLowerCase();
if (name === 'before' ||
name === 'after' ||
name === 'first-line' ||
name === 'first-letter') {
C++;
} else {
B++;
}
break;
}
});
return [A, B, C];
};