Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

thead and tbody are being ignored when querying its table row element #956

Closed
christian-bromann opened this issue Mar 3, 2021 · 3 comments

Comments

@christian-bromann
Copy link

I am not sure if this is the right place to report this. I came across this weird behavior and wonder if it is the desired or not.

Given I have the following HTML structure:

<table id="outer-table">
  <thead>
    <tr>
      <th>outer-table-column-header</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>outer-table-first-row-cell</td>
    </tr>
    <tr>
      <td>
        <table id="inner-table">
          <thead>
            <tr>
              <th>inner-table-column-header</th>
            </tr>
          </thead>
          <tbody>
            <tr>
              <td>inner-table-first-row-cell</td>
            </tr>
          </tbody>
        </table>
      </td>
    </tr>
  </tbody>
</table>

There seems to be a funky behavior when trying to query the first row cell from the inner vs outer table:

> $('#outer-table').querySelector('tbody tr').innerText
"outer-table-first-row-cell"
> $('#inner-table tbody tr').innerText
"inner-table-first-row-cell"

// whereas
> $('#inner-table').querySelector('tbody tr').innerText
"inner-table-column-header"

Is this expected?

@annevk annevk transferred this issue from whatwg/html Mar 3, 2021
@annevk
Copy link
Member

annevk commented Mar 3, 2021

Yeah, this is how querySelector() works. It matches against the entire tree and then filters the results based on "this".

@DV8FromTheWorld
Copy link

DV8FromTheWorld commented Mar 4, 2021

The confusion here is that the expectation is that Element.querySelector would only match selectors based on the context of the baseElement (in this case, #inner-table) such that it feels correct to believe that
document.querySelector('#inner-table tbody tr') and document.querySelector('#inner-table').querySelector('tbody tr') are the same, as if the latter would simply prepend #inner-table onto the selector.

However, this is not the case.

Given the docs on how Element.querySelector works, the entire document is matched against the query, and then it filters all matched elements to find those that are decedents of the baseElement, which is #inner-table in this case.

Thus, the first element that satisfies tbody tr and is also a descendent of #inner-table would be #outer-table > tbody > tr > td > #inner-table > thead > tr due to all of #inner-table being inside of a tbody

@christian-bromann
Copy link
Author

@DV8FromTheWorld that is a fantastic explanation! This can be closed from my side then. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants