Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

Closed
@JakeQZ

Description

@JakeQZ

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