Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[CssSelector] Incorrect XPath for :only-of-type #33773

Copy link
Copy link
Closed
@JakeQZ

Description

@JakeQZ
Issue body actions

Symfony version(s) affected: 4.3.4

Description
The XPath expression returned by, e.g., CssSelectorConverter::toXPath("span:only-of-type") is descendant-or-self::span[last() = 1]. This will only match a <span> element if it is the only one in the entire HTML (not just among its siblings).

How to reproduce

$converter = new \Symfony\Component\CssSelector\CssSelectorConverter();
$xPathExpression = $converter->toXPath("span:only-of-type");
$domXPath = new \DOMXPath($domDocument);
$nodeList = $domXPath->query($xPathExpression);

where $domDocument is a \DOMDocument containing the following HTML:

<html>
  <body>
    <p>
      <span></span>
    </p>
    <p>
      <span></span>
      <span></span>
    </p>
  </body>
</html>

$nodeList will be empty but should contain the <span> from the first paragraph. If the second paragraph is removed, the behaviour is as expected ($nodeList will contain the <span> from the first paragraph), becuase there is only one <span> element in the entire document.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.