Skip to content

Commit

Permalink
feature #33861 [CssSelector] Support *:only-of-type (jakzal)
Browse files Browse the repository at this point in the history
This PR was merged into the 4.4 branch.

Discussion
----------

[CssSelector] Support *:only-of-type

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Tickets       | -
| License       | MIT
| Doc PR        | -

Before, this pseudo class selector only worked when the element was specified, i.e. `p:only-of-type` would select all paragraphs which are the only child of its parent.

Now, `*:only-of-type` is also supported to select any single child.

Commits
-------

eae2ebc [CssSelector] Support *:only-of-type pseudo class selector
  • Loading branch information
fabpot committed Oct 5, 2019
2 parents 2044ba8 + eae2ebc commit 3a66d59
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 7 deletions.
5 changes: 5 additions & 0 deletions src/Symfony/Component/CssSelector/CHANGELOG.md
@@ -1,6 +1,11 @@
CHANGELOG
=========

4.4.0
-----

* Added support for `*:only-of-type`

2.8.0
-----

Expand Down
Expand Up @@ -308,6 +308,8 @@ public function getHtmlIdsTestData()
['li div:only-child', ['li-div']],
['div *:only-child', ['li-div', 'foobar-span']],
['p:only-of-type', ['paragraph']],
[':only-of-type', ['html', 'li-div', 'foobar-span', 'paragraph']],
['div#foobar-div :only-of-type', ['foobar-span']],
['a:empty', ['name-anchor']],
['a:EMpty', ['name-anchor']],
['li:empty', ['third-li', 'fourth-li', 'fifth-li', 'sixth-li']],
Expand Down
Expand Up @@ -100,17 +100,10 @@ public function translateOnlyChild(XPathExpr $xpath): XPathExpr
->addCondition('last() = 1');
}

/**
* @throws ExpressionErrorException
*/
public function translateOnlyOfType(XPathExpr $xpath): XPathExpr
{
$element = $xpath->getElement();

if ('*' === $element) {
throw new ExpressionErrorException('"*:only-of-type" is not implemented.');
}

return $xpath->addCondition(sprintf('count(preceding-sibling::%s)=0 and count(following-sibling::%s)=0', $element, $element));
}

Expand Down

0 comments on commit 3a66d59

Please sign in to comment.