gumbo-query icon indicating copy to clipboard operation
gumbo-query copied to clipboard

Getting nodes with a specific class

Open RachidTagzen opened this issue 7 years ago • 5 comments

Hi,

I want to get all nodes with a class containing (not matching exactly) a specific value. I used 'find', but it returns only the nodes with class matching exactly the value. For example, if I have a:

<div class="the-doc the-row'">
      Hello world
</div>

This do not work:

    CSelection c = doc.find("div[class=' the-row']");
    for (int i = 0; i < c.nodeNum(); i++) {
        qDebug() << c.nodeAt(i).text().c_str();
    }

this works:

    CSelection c = doc.find("div[class='the-doc the-row']");
    for (int i = 0; i < c.nodeNum(); i++) {
        qDebug() << c.nodeAt(i).text().c_str();
    }

Thanks.

RachidTagzen avatar Mar 09 '17 12:03 RachidTagzen

IIRC, this is one of dozens of bugs. However, you might try getting rid of the space in your not-working example:

CSelection c = doc.find("div[class=' the-row']");

Should be:

CSelection c = doc.find("div[class='the-row']");

TechnikEmpire avatar Mar 09 '17 12:03 TechnikEmpire

On second thought you're not even doing a class selector correctly. See source here.

Class selector should be ".the-row". You're doing an attribute selector, which is requiring a full, complete match.

TechnikEmpire avatar Mar 09 '17 12:03 TechnikEmpire

@TechnikEmpire That's working now. Oddly, I have tried the correct Selector way in the past without a good results, and it works now. I don't know why!!

I have an other question: it is possible to match 2 or more conditions with 'find'? e.g: finding the nodes with: class="the-row" And/Or href="http://www.exampe.com"

Thank you.

RachidTagzen avatar Mar 09 '17 13:03 RachidTagzen

You should be able to run basically any css3 selector. Google how to compose such selectors and they should work. I rewrote this library quite a while ago and there are a lot of bugs that cause certain selectors and other things to fail, but I can't remember them all. So you may find that perfectly valid selectors fail or give improper results. If your code is remotely critical, write some tests to verify it because I'd bet my bottom dollar they're not all going to work correctly. It's not the author of this port's fault either, a great many of the bugs were ported over from cascadia, the original source.

TechnikEmpire avatar Mar 09 '17 13:03 TechnikEmpire

Thank you for explanation.

RachidTagzen avatar Mar 09 '17 14:03 RachidTagzen