php-src icon indicating copy to clipboard operation
php-src copied to clipboard

Dom: querySelector() rejects a selector whose identifier starts with a non-ASCII character, like #été

Open phcorp opened this issue 2 weeks ago • 0 comments

Description

A selector whose identifier starts with a non-ASCII character is rejected:

<?php
$doc = Dom\HTMLDocument::createFromString('<p id="été">a</p><p class="élan">b</p>', LIBXML_NOERROR);
var_dump($doc->querySelector('#\e9 t\e9')?->textContent);
var_dump($doc->querySelector('#été')?->textContent);

Resulted in this output:

string(1) "a"

Fatal error: Uncaught DOMException: Invalid selector (Selectors. Unexpected token: té)

But I expected this output instead:

string(1) "a"
string(1) "a"

CSS Syntax allows any code point from U+0080 in an ident and a hash token, and browsers accept document.querySelector('#été'). .élan fails the same way; a name whose non-ASCII character is not the first one, like .café, works, and so does the escaped form above.

The bundled lexbor tokenizer checks its ASCII name table before the non-ASCII branch in lxb_css_syntax_state_hash() (ext/lexbor/lexbor/css/syntax/state.c, the same on PHP-8.5 and master). lexbor fixed it after its last release, in lexbor/lexbor@ce777c50f2 ("CSS::Syntax: fixed non-ASCII ident code points") and lexbor/lexbor@3a5217afde ("CSS::Syntax: fixed non-ASCII ident code point ranges"); backporting them would fix it.

PHP Version

PHP 8.5.12 (lexbor 2.7.0); master has the same code.

Operating System

macOS (not OS-specific)

phcorp avatar Nov 16 '26 21:11 phcorp