Skip to content

Regression in adjacent-descendant selector traversal #160

@koal44

Description

@koal44

Here's a minimal repro

import { JSDOM } from 'jsdom';

describe('nwsapi', () => {
  it('should find the target', () => {
    const doc = new JSDOM(`
      <div>
        <div class="neighbor"></div>
        <div>
          <a>
            <img class="target"></img>
          </a>
        </div>
      </div>
    `).window.document;

    const plusDiv = doc.querySelector('.neighbor + div .target');
    const plusStar = doc.querySelector('.neighbor + * .target');

    assert.ok(plusDiv, '+ div should match');
    assert.ok(plusStar, '+ * should also match'); // fails
  });
});

I traced the regression to 3d8f8d2 and more specifically to this area of the code:

  case '~':
    source = 'var N' + k + '=e;while(e&&(e=e.previousElementSibling)){' + source + '}e=N' + k + ';';
  case '+':
    source = 'var N' + k + '=e;if(e&&(e=e.previousElementSibling)){' + source + '}e=N' + k + ';';
  case '\x20':
    source = 'var N' + k + '=e;while(e&&(e=e.parentElement)){' + source + '}e=N' + k + ';';

In the commit, the restoration of e after branch traversal was dropped.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions