# Parsel: A tiny, permissive CSS selector parser

DevFeed: [Parsel: A tiny, permissive CSS selector parser](<https://devfeed.tech/articles/parsel-a-tiny-permissive-css-selector-parser-52122.md>)

Original publisher: [Read original article](<https://lea.verou.me/2020/09/parsel-a-tiny-permissive-css-selector-parser/>)

Author: Lea Verou

Published: 2020-09-07T00:00:00Z

Content type: release

Language: en

Sources: [Lea Verou's blog](<https://devfeed.tech/sources/lea-verou-s-blog.md>)

Topics: [Parser](<https://devfeed.tech/topics/parser.md>), [selectors](<https://devfeed.tech/topics/selectors.md>), [CSS](<https://devfeed.tech/topics/css.md>), [Front end](<https://devfeed.tech/topics/frontend.md>)

Tags: [blog](<https://devfeed.tech/tags/blog.md>), [css](<https://devfeed.tech/tags/css.md>), [original](<https://devfeed.tech/tags/original.md>), [releases](<https://devfeed.tech/tags/releases.md>), [selector](<https://devfeed.tech/tags/selector.md>), [specificity](<https://devfeed.tech/tags/specificity.md>), [tiny](<https://devfeed.tech/tags/tiny.md>), [web-almanac](<https://devfeed.tech/tags/web-almanac.md>)

## AI overview

Parsel is a small, permissive CSS selector parser created for Web Almanac statistics after existing options were considered too large, dependency-heavy, or incomplete for the task. It produces an AST, supports AST walking, and calculates specificity as an array or comparable number. It can be used as an ES module or as a global. The author prioritizes realistic valid selectors being parsed correctly; unusual handling of invalid selectors is an accepted tradeoff intended to keep the code small and simple. Its hybrid parser scans character by character to extract strings and top-level parentheses, then uses ordered token regexes on the remaining text before restoring and nesting tokens into an AST. The excerpt notes that its regexes use ES2018 named capture groups and can be transpiled for wider support.

## Source excerpt

I've posted before about my work for the Web Almanac this year. To make it easier to calculate the stats about CSS selectors, we looked to use an existing selector parser, but most were too big and/or had dependencies or didn't account for all selectors we wanted to parse, and we'd need to write our own walk and specificity methods anyway. So I did what I usually do in these cases: I wrote my own! You can find it here: https://projects.verou.me/parsel/ It not only parses CSS selectors, but also includes methods to walk the AST produced, as well as calculate specificity as an array and convert it to a number for easy comparison. It is one of my first libraries released as an ES module, and there are instructions about both using it as a module, and as a global, for those who would rather not deal with ES modules yet, because convenient as ESM are, I wouldn't want to exclude those less familiar with modern JS. Please try it out and report any bugs! We plan to use it for Almanac stats in the next few days, so if you can spot bugs sooner rather than later, you can help that volunteer effort. I'm primarily interested in (realistic) valid selectors that are parsed incorrectly. I'm aware there are many invalid selectors that are parsed weirdly, but that's not a focus (hence the "permissive" aspect, there are many invalid selectors it won't throw on, and that's by design to keep the code small, the logic simple, and the functionality future-proof). How it works If you're just interested in using this selector parser, read no further. This section is about how the parser works, for those interested in this kind of thing. :) I first started by writing a typical parser, with character-by-character gobbling and different modes, with code somewhat inspired by my familiarity with jsep. I quickly realized that was a more fragile approach for what I wanted to do, and would result in a much larger module. I also missed the ease and flexibility of doing things with regexes. However,