taoqf / node-html-parser

A very fast HTML parser, generating a simplified DOM, with basic element query support.
MIT License
1.11k stars 107 forks source link

CSS child selector or adjacent sibling selector #94

Closed blankstar85 closed 3 years ago

blankstar85 commented 3 years ago

Hello,

is there a way to only get the first set of matches? I'm trying to select only all of the first set of tr under tbody and not any tr under first set of tr.

html:

<tbody>
  <tr>
      <tbody>
          <tr>
              more
          </tr>
      </tbody>
  </tr>
  <tr>
      etc
  </tr>
</tbody>

example.querySelectorAll('tbody tr');

I get all 3 tr instead of the upper 2. Is this possible?

I'm able to take a slightly longer route to achieve this

  .querySelector('tbody')
  .childNodes.filter((node: HTMLElement) => node.rawTagName === 'tr')

just wondering if there is an easier way to accomplish this just using the query selecter. I tried using > tbody > tr as I would a css selector, but no results are returned.

taoqf commented 3 years ago

I am afraid the answer is No, not for now. querySelector just could do the simple query.

blankstar85 commented 3 years ago

Thanks. Appreciate the reply.

taoqf commented 3 years ago

You are welcome.