Cheerio: SyntaxError: unmatched pseudo-class :foo

Created on 7 Apr 2015  路  9Comments  路  Source: cheeriojs/cheerio

@MatthewMueller

Trying to use cheerio to match against a custom HTML node receives that error.

Here is a simple test to try it out:

var cheerio = require('cheerio');
var markup = '<html><body><esi:foo attr="test"></esi:foo></body></html>';

var $ = cheerio.load(markup);
$('esi:foo').text();

Most helpful comment

@fb55 double backslash fixed it. I originally tried one but not two, smh.

All 9 comments

The use case is basically for running unit tests on rendered HTML that will be parsed by apache traffic server (ats) and execute these esi tags.

@fb55 does the html parser support the colon in <esi:foo>?

In the meantime, @redonkulus you may want to try:

var $ = cheerio.load(markup, { xmlMode: true });

and see if that works.

@MatthewMueller same issue with xmlMode flag passed in.

Try escaping the colon with a backslash (which needs to be escaped itself
with an additional backslash inside JS strings). Otherwise, it is parsed as
a pseudo selector.

@fb55 double backslash fixed it. I originally tried one but not two, smh.

Thanks!

Try escaping the colon with a backslash (which needs to be escaped itself
with an additional backslash inside JS strings). Otherwise, it is parsed as
a pseudo selector.

Do pseudo selectors apply to HTML? Or is this just for <style>div:hover { color: blue }</style>?

<div:hover> will produce an HTMLUnknownElement named DIV:HOVER. If you'd like to query for it in an CSS selector, you'll need to escape the colon (div\:hover). Otherwise, you'll match elements named DIV in their hover state. Same applies to classes (eg. [foo\.bar]).

Because JS uses backslashes for escaping inside itself, this weird double escape is required.

Double black slash \\ before the : fixed it! :
\\:

Was this page helpful?
0 / 5 - 0 ratings

Related issues

AlbertoElias picture AlbertoElias  路  4Comments

tndev picture tndev  路  4Comments

rajkumarpb picture rajkumarpb  路  3Comments

M3kH picture M3kH  路  4Comments

miguelmota picture miguelmota  路  3Comments