jsoup icon indicating copy to clipboard operation
jsoup copied to clipboard

cssSelector has a problem with '<div data-v-7a225e99="" class="flex-center gap-[12px]">'

Open 666asd opened this issue 1 year ago • 1 comments

The page contains such elements <div data-v-7a225e99="" class="flex-center gap-[12px]">. It seems to be caused by a difference in the handling of "" by the two functions. org.jsoup.select.QueryParser#consumeSubQuery org.jsoup.parser.TokenQueue#chompBalanced image image image

666asd avatar Jun 21 '24 09:06 666asd

Can you provide a small test case that demonstrates what you're trying to do and what you expect?

jhy avatar Jul 01 '24 04:07 jhy

An error will be raised when getting the cssSelector of the element.

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;


public class Demo {
    public static void main(String[] args) throws Exception {
        String html = "<html>\n" +
                "\n" +
                "<head>\n" +
                "  <title>Jsoup Example</title>\n" +
                "</head>\n" +
                "\n" +
                "<body>\n" +
                "  <div data-v-7a225e99=\"\" class=\"flex-center gap-[12px]\">\n" +
                "</body>\n" +
                "\n" +
                "</html>";
        Document doc = Jsoup.parse(html,"UTF-8");

        doc.getElementsByTag("div").first().cssSelector();

    }
}

Exception in thread "main" org.jsoup.select.Selector$SelectorParseException: Did not find balanced marker at '12px\]'
	at org.jsoup.select.QueryParser.parse(QueryParser.java:48)
	at org.jsoup.select.Selector.select(Selector.java:101)
	at org.jsoup.nodes.Element.select(Element.java:474)
	at org.jsoup.nodes.Element.cssSelectorComponent(Element.java:950)
	at org.jsoup.nodes.Element.cssSelector(Element.java:928)
	at Demo.main(Demo.java:21)

666asd avatar Jul 04 '24 08:07 666asd

Thanks, actual code is worth a thousand pictures :)

jhy avatar Jul 04 '24 22:07 jhy

Thanks, fixed.

This fixes the case that consumeSubQuery() didn't handle escape chars.

Per #1998, we also can do a better job of making the escapes, in the first place.

jhy avatar Jul 08 '24 02:07 jhy