gumbo-query
gumbo-query copied to clipboard
Getting nodes with a specific class
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.
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']");
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 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.
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.
Thank you for explanation.