[html] Check UTF-16 code units instead of runes in :blank selector Reuse `allWhitespace` (`codeUnitAt` + `isWhitespaceCC`) instead of iterating `String.runes` when checking `:blank` text nodes. All five HTML ASCII whitespace characters (`<= 0x20`) are disjoint from UTF-16 surrogate code units (`0xD800..0xDFFF`), so checking code units directly preserves behavior for surrogate pairs and lone surrogates while avoiding rune decoding and iterable allocations.
diff --git a/pkgs/html/lib/src/query_selector.dart b/pkgs/html/lib/src/query_selector.dart index 964f568..a35c116 100644 --- a/pkgs/html/lib/src/query_selector.dart +++ b/pkgs/html/lib/src/query_selector.dart
@@ -9,8 +9,8 @@ import 'package:csslib/visitor.dart'; import '../dom.dart'; -import 'constants.dart' show isWhitespaceCC; import 'css_class_set.dart' show hasHtmlClassToken; +import 'utils.dart' show allWhitespace; Element? querySelector(Node node, String selector) => SelectorEvaluator().querySelector(node, _parseSelectorList(selector)); @@ -171,9 +171,8 @@ // http://dev.w3.org/csswg/selectors-4/#the-blank-pseudo case 'blank': - return !_element!.nodes.any((n) => - n is Element || - n is Text && n.text.runes.any((r) => !isWhitespaceCC(r))); + return !_element!.nodes + .any((n) => n is Element || n is Text && !allWhitespace(n.text)); // http://dev.w3.org/csswg/selectors-4/#the-first-child-pseudo case 'first-child':
diff --git a/pkgs/html/test/query_selector_test.dart b/pkgs/html/test/query_selector_test.dart index 1840d0b..9bab9c8 100644 --- a/pkgs/html/test/query_selector_test.dart +++ b/pkgs/html/test/query_selector_test.dart
@@ -43,16 +43,52 @@ List<String> ids(Document doc, String selector) => doc.querySelectorAll(selector).map((e) => e.id).toList(); + Document buildPseudoDoc() { + final doc = parse( + '<p id=empty></p>' + '<p id=comment><!--c--></p>' + '<p id=child-el><b></b><!--c--></p>' + '<p id=text>x</p>' + '<p id=space> </p>' + '<p id=tab>\t</p>' + '<p id=lf>\n</p>' + '<p id=ff>\f</p>' + '<p id=nbsp>\u00A0</p>' + '<p id=em-space>\u2003</p>' + '<p id=emoji>\u{1F600}</p>' + '<p id=space-emoji> \u{1F600}</p>', + ); + doc.body! + ..append(Element.tag('p') + ..id = 'cr' + ..append(Text('\r'))) + ..append(Element.tag('p') + ..id = 'empty-text' + ..append(Text(''))) + ..append(Element.tag('p') + ..id = 'empty-then-text' + ..append(Text('')) + ..append(Text('x'))) + ..append(Element.tag('p') + ..id = 'lead-surrogate' + ..append(Text('\uD800'))) + ..append(Element.tag('p') + ..id = 'space-trail-surrogate' + ..append(Text(' \uDC00'))); + return doc; + } + test(':empty matches elements with only comments or empty text', () { - final doc = parse('<p id=a></p><p id=b><!--c--></p>' - '<p id=c><b></b><!--c--></p><p id=d>x</p><p id=e> </p>'); - expect(ids(doc, 'p:empty'), ['a', 'b']); + final doc = buildPseudoDoc(); + expect(ids(doc, 'p:empty'), ['empty', 'comment', 'empty-text']); }); test(':blank also matches elements with only whitespace text', () { - final doc = parse('<p id=a></p><p id=b><!--c--></p>' - '<p id=c><b></b><!--c--></p><p id=d>x</p><p id=e> </p>'); - expect(ids(doc, 'p:blank'), ['a', 'b', 'e']); + final doc = buildPseudoDoc(); + expect( + ids(doc, 'p:blank'), + ['empty', 'comment', 'space', 'tab', 'lf', 'ff', 'cr', 'empty-text'], + ); }); }); }