Skip to content

feature-request: allow specificity calculation of AST's of popular parsers #32

@bartveneman

Description

@bartveneman

TIL that this semi-copy exists (14M weekly downloads) for the sole reason that PostCSS AST's can be used to calculate specificity instead of CSSTree. Unfortunately I ran into the same issue for Project Wallace where I re-implemented specificity to cater for @projectwallace/css-parser's AST.

It'd be nice to have a more generic calculator, so I want to use this issue for some bikeshedding and gathering ideas. I'm going to make some proposals as a starting point:

calculate(selector: string): Specificity
// which calls CSSTree by default, as it does now
import type { Selector } from 'csstree'
calculateForAST(node: Selector): Specificity

// But then we could also expose

import type { Node } from 'postcss'
calculatePostCSS(node): Specificity

import type { Selector } from '@projectwallace/css-parser'
calculateProjectWallace(node: Selector): Specificity

The hard-to-design part here is to make one function that handles the specificity calculation, regardless of parser or node types:

// all above calculate() methods call this under the hood:

calculateGeneric()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions