Skip to content
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

Consider exposing calculateSpecificityOfSelectorObject #17

Open
bramus opened this issue Dec 30, 2022 · 4 comments · May be fixed by #23
Open

Consider exposing calculateSpecificityOfSelectorObject #17

bramus opened this issue Dec 30, 2022 · 4 comments · May be fixed by #23

Comments

@bramus
Copy link
Owner

bramus commented Dec 30, 2022

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'

const calculateSpecificityOfSelectorObject = (selectorObj) => {

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.

@bartveneman
Copy link
Contributor

Maybe calculateFromAstNode or calculateSelectorNode, calculateNode? The return type is Specificity, so no real need to include that in the function name.

@bartveneman
Copy link
Contributor

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 }) {}

@bramus
Copy link
Owner Author

bramus commented Jan 17, 2023

If we could expose a function that would accept a walk function and a Selector Node, that'd be 💯

What would that look like? Got some pseudo/theoretical code to clarify?

@bartveneman
Copy link
Contributor

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?

@bartveneman bartveneman linked a pull request Feb 18, 2024 that will close this issue
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants