Page MenuHomePhabricator

Create a library which extends CSS with JQ-style selectors into JSON data attributes
Open, Needs TriagePublic

Description

Parsoid MediaWiki DOM Spec contains a number of HTML attributes which contain JSON-encoded structured data, like data-mw and data-parsoid. We can efficiently do CSS-style selector queries to find specific nodes, but in order to find (for example) all the instances of a given Template, we also need to interrogate the data-mw.

The following is a notional query string for a combined "CSS with JQ-style selectors" that would pull out all the instances of {{citation needed}} on a page.

[typeof="mw:Transclusion"][data-mw/.parts[].template?.target.href == "./Template:Citation_needed"]

Note that this uses the attribute comparator / to switch to "JQ mode" and compares the href rather than the wt in order to ensure that (eg) redirects or case variations in the wikitext source don't affect the match. It uses the ? suffix on 'template' to ensure that it does not error if there are string or non-template parts.

This would be a very useful library for Parsoid clients, and could be potentially be provided both for JS and PHP by extending the zest CSS selector library.