Stew

Stew is a JavaScript library that implements the CSS selector syntax, and extends it with regular expression tag names, class names, ids, attribute names and attribute values.

For example, given a variable dom containing a document tree, the JavaScript snippet:

var links = stew.select(dom,'a[href]');

will return an array of all the anchor tags (<a>) found in dom that include an href attribute.

While the JavaScript snippet:

var metadata = stew.select(dom,'head meta[name=/^dc\.|:/i]');

will extract the Dublin Core metadata from a document by selecting every <meta> tag found in the <head> that has a name attribute that starts with DC. or DC: (ignoring case).

Stew is often used as a toolkit for "screen-scraping" web pages (extracting data from HTML and XML documents).

(The name "stew" is inspired by the Python library BeautifulSoup, Simon Willison's soupselect extension of BeautifulSoup, and Harry Fuecks' Node.js port of soupselect. Stew is a meatier soup.)

Read on for more information, or:

(Links not working? Try it from heyrod.com/stew.)

Installing

The source code and documentation for Stew is available on GitHub at rodw/stew. You can clone the repository via:

git clone git@github.com:rodw/stew.git

Stew is deployed as an npm module under the name stew-select. Hence you can install a pre-packaged version with the command:

npm install stew-select

and you can add it to your project as a dependency by adding a line like:

"stew-select": "latest"

to the dependencies or devDependencies part of your package.json file.

Features

Core CSS Selectors

Stew supports the full Version 2.1 CSS selector syntax and much of Version 3, including

And of course, you can use arbitrary combinations of these selectors:

stew.select( dom, 'article div.credits > a[rel=license]' );
stew.select( dom, 'h1, h2, h3, h4, h5, h6, .heading' );
stew.select( dom, 'h1.title + h2.subtitle' );
stew.select( dom, 'ul > li > a[rel=author][href]' );

Regular Expressions

Stew extends the CSS selector syntax by allowing the use of regular expressions to specify tag names, class names, ids, and attributes (both name and value).

For example,

var metadata = stew.select(dom,'a[href=/^https?:/i]');

will select all anchor (<a>) tags with an href attribute that starts with http: or https: (with a case-insensitive comparison).

Another example, the snippet:

var metadata = stew.select(dom,'[/^data-/]');

selects all tags with an attribute whose name starts with data-.

Any name or value that starts and ends with / will be treated as a regular expression. (Or, more accurately, any name or value that starts with / and ends with / with an optional suffix of any combination of the letters g, m and i. E.g., /example/gi.)

The regular expression is processed using JavaScript's standard regular expression syntax, including support for \b and other special class markers.

Here are some example CSS selectors using regular expressions:

These may be used in any combination, and freely mixed with "regular" CSS selectors.

Current Limitations

Stew currently has a couple of known issues that crop up during specific (and rare) edge-cases. We intend to eliminate these in future releases, but want to make you aware of them so that you're not surprised.

(Developers: If you'd like to help address these issues, we'd love your help. Feel free to submit a pull request or reach out for more information.)

CSS 3 Selectors aren't (yet) fully supported.

Our intention is to fully support the most recent CSS selector syntax.

Stew supports all of the CSS 2.1 Selectors. (To the extent that it makes sense to do so. It's hard to see how to interpret :hover and :visited and so on when looking at static-HTML from the server side, although :first-child is supported.)

Not quite all of the CSS 3 Selectors are supported. Currently certain structural pseudo-classes and pseduo-elements are not supported (yet).

Stew may not report all syntax errors.

Stew will accept and properly parse any valid CSS selectors (unless listed as limitation elsewhere in this section).

However, (currently) Stew does not always reject every invalid selector. In particular, Stew's parser may ignore the invalid parts of improperly formed selectors, which can lead to unexpected results.

Stew requires white-space around the "generalized sibling" operator: E ~ F works, but E~F doesn't.

Stew parsers most operators (including +, > and ,) with or without white-space. In other words, Stew treats the following selectors as equivalent:

Unfortantely, due to a quirk of Stew's current parser, the same is not true for the "preceeding sibling" operator (~). That is, Stew supports E ~ F but does not properly parse E~F. Currently the ~ character must be surrounded by white-space.

(If you're curious, the ~= operator is the complicating factor for ~ right now. The same patterns we use for +, , and > don't quite work for ~.)

Licensing

The Stew library and related documentation are made available under an MIT License. For details, please see the file MIT-LICENSE.txt in the root directory of the repository.