parsel

A tiny, permissive CSS selector parser

Specificity: parsel.specificity(selector);

Tokens parsel.tokenize(selector);

AST parsel.parse(selector);

Usage

Parsel is an ES module. You import it like so:

import * as parsel from "https://projects.verou.me/parsel/parsel.js"

Note that to use that your script needs to use type="module" or be imported from a script that does. If you can't or don't want to use ES modules you can use parsel_nomodule.js in a regular <script> tag:


		<script src="https://projects.verou.me/parsel/parsel_nomodule.js"></script>
	

After that, you can use parsel as a global.

Fun fact: You could also use the module version of Parsel and convert it to a global this way:


		<script type="module">
			import * as parsel from "https://projects.verou.me/parsel/parsel.js";
			window.parsel = parsel;
		</script>
	

Then, assuming your code runs after the DOMContentLoaded event, you can use the global normally. In fact, we are assigning parsel to a global in this very page this way, so you can open your console and play with it!

You can also install via npm: npm install parsel-js

API

Get list of tokens as a flat array:

parsel.tokenize(selector)

Get AST:

parsel.parse(selector)

You can also provide options:

parsel.parse(selector, {recursive: false, list: false})

The recursive option parses the arguments of pseudo-classes whose argument is a selector like :not(), :is(), :where() etc into a subtree property. The list option parses selector lists (A, B, C). The only reason to turn it off is as a performance optimization when you are processing a large volume of selectors that are not lists (e.g. the output of certain CSS parsers like Rework)

Traverse all tokens of a (sub)tree:

parsel.walk(node, callback)

callback is called with each node as the only argument.

Calculate specificity (returns an array of 3 numbers):

parsel.specificity(selectorOrNode)

To convert the specificity array to a number, you can use parsel.specificityToNumber(specificity [, base]). If a base is not provided, it will be the max specificity component + 1.

Try it out! In this page, parsel is assigned to a global so you can experiment with the API in the console!

Extensibility

You can import TOKENS and add new types. All values need to be regular expression objects with the global flag on. For example, to add the nesting selector:


		parsel.TOKENS.nesting = /&/g;
	

Do note that this way, new tokens are added to the end of the object literal. You may want to add tokens before other tokens, e.g. to add support for @nest. This is a little tricky, because you cannot just replace the object literal with another, so the only way to add a property after another property is to delete all properties after that property, add your new property, then re-add them.

So, to add support for @nest, we need to add it before the type token, since @nest tokens are currently incorrectly parsed as type tokens. Since type is the very last token, we only need to delete and re-add that:


		// Delete property type
		let temp = {};
		temp.type = parsel.TOKENS.type;
		delete parsel.TOKENS.type;

		// Add new token
		parsel.TOKENS.nest = /@nest\b/g;

		// Re-add type
		parsel.TOKENS.type = temp.type;
	

This can get tedious, so you can use a helper function for that:


		function insert(obj, {before, property, value}) {
			let found, temp = {};

			for (let p in obj) {
				if (p === before) {
					found = true;
				}

				if (found) {
					temp[p] = obj[p];
					delete obj[p];
				}
			}

			Object.assign(obj, {property: value, ...temp});
		}
	

Then you can do:


		insert(parsel.TOKENS, {before: "type", property: "nest", value: /@nest\b/g});
	

For convenience, you can also find this helper function in insert.js, and you can just import it:


		import insert from "./parsel/insert.js";
		insert(parsel.TOKENS, {before: "type", property: "nest", value: /@nest\b/g});
	

There are also some alternative implementations of this helper available.