specificity
specificity copied to clipboard
Consider exposing `calculateSpecificityOfSelectorObject`
As per request by @bartveneman on Twitter:
Would be nice to expose calculateSpecificityOfSelectorObject so we can avoid
convertToAST-> typeof source -> source instanceof Object -> source.type == 'Selector'https://github.com/bramus/specificity/blob/12557b78948bb4cb6438ac51e4c81c850cde5a4e/src/core/calculate.js#L5
This adds up quickly on several thousands of selectors like on our website.
Considered this in the past but hadn’t done set yet because I’m not too happy with the name of the function. Open to suggestions for a shorter - yet also clear - name. If no alternative can be found, then I’m fine with exposing as is.
Maybe calculateFromAstNode or calculateSelectorNode, calculateNode? The return type is Specificity, so no real need to include that in the function name.
1 more way to improve this function is to allow our own csstree walker. Project Wallace is powered by CSSTree too, so we already have a walker function available, next to the full AST. If we could expose a function that would accept a walk function and a Selector Node, that'd be 💯 , because there will not be conflict issues when our versions of CSSTree are different. function getSpeficiticity({ walker, node }) {}
If we could expose a function that would accept a
walkfunction and aSelectorNode, that'd be 💯
What would that look like? Got some pseudo/theoretical code to clarify?
Ah, sorry, misread the code: it doesn't use an AST walker, it just loops over some of the nodes 😬. So that already makes it pretty lightweight. The only dependency it seems to have is the max function and it returns a new Specificity at the end, which we maybe can skip?