Skip to content

Commit

Permalink
[TASK] Test :nth-last-child (#750)
Browse files Browse the repository at this point in the history
Also updated the regex for supported pseudo-classes (`PSEUDO_CLASS_MATCHER`) so
that those supported are explicitly specified (rather than ‘anything ending with
`-child` or anything ending with `-type(`’), without acually affecting the valid
pseudo-class names it will match.  Except in the `Emogrifier` class – this
doesn’t support `:nth-last-child` so that has been removed (to the effect that
rules with it will now be copied to the `<style>` element rather than attempted
to be inlined).

And added `:nth-last-child` to the list of supported pseudo-classes in the
README.  (Also, for consistency, added `()` to the other `:nth…` pseudo-classes
in that section.)

Part of #747.
  • Loading branch information
JakeQZ authored and oliverklee committed Sep 29, 2019
1 parent db22527 commit a5bea23
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ This project adheres to [Semantic Versioning](https://semver.org/).
## x.y.z

### Added
- Test `:nth-last-child`
([#747](https://github.com/MyIntervals/emogrifier/issues/747),
[#750](https://github.com/MyIntervals/emogrifier/pull/750))
- Test general sibling combinator
([#723](https://github.com/MyIntervals/emogrifier/issues/723),
[#745](https://github.com/MyIntervals/emogrifier/pull/745))
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -347,8 +347,9 @@ Emogrifier currently supports the following
* [first-child](https://developer.mozilla.org/en-US/docs/Web/CSS/:first-child)
* [last-child](https://developer.mozilla.org/en-US/docs/Web/CSS/:last-child)
* [not()](https://developer.mozilla.org/en-US/docs/Web/CSS/:not)
* [nth-child](https://developer.mozilla.org/en-US/docs/Web/CSS/:nth-child)
* [nth-of-type](https://developer.mozilla.org/en-US/docs/Web/CSS/:nth-of-type)
* [nth-child()](https://developer.mozilla.org/en-US/docs/Web/CSS/:nth-child)
* [nth-last-child()](https://developer.mozilla.org/en-US/docs/Web/CSS/:nth-last-child)
* [nth-of-type()](https://developer.mozilla.org/en-US/docs/Web/CSS/:nth-of-type)
(with a type, e.g. `p:nth-of-type(2n)` but not `*:nth-of-type(2n)` which
will behave as `*:nth-child(2n)`)

Expand Down
2 changes: 1 addition & 1 deletion src/Emogrifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class Emogrifier
*
* @var string
*/
const PSEUDO_CLASS_MATCHER = '[\\w\\-]+\\-(?:child|type\\()|not\\([[:ascii:]]*\\)';
const PSEUDO_CLASS_MATCHER = '(?:first|last|nth|only)-child|nth(?:-last)?+-of-type|not\\([[:ascii:]]*\\)';

/**
* @var string
Expand Down
3 changes: 2 additions & 1 deletion src/Emogrifier/CssInliner.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ class CssInliner extends AbstractHtmlProcessor
*
* @var string
*/
const PSEUDO_CLASS_MATCHER = '[\\w\\-]+\\-(?:child|type\\()|not\\([[:ascii:]]*\\)';
const PSEUDO_CLASS_MATCHER
= '(?:first|last|nth(?:-last)?+|only)-child|nth(?:-last)?+-of-type|not\\([[:ascii:]]*\\)';

/**
* @var bool[]
Expand Down
67 changes: 67 additions & 0 deletions tests/Unit/CssInlinerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,46 @@ public function matchedCssDataProvider()
':nth-child(-n+3) => 2nd of many' => [':nth-child(-n+3) { %1$s }', '<p class="p-2" style="%1$s">'],
':nth-child(-n+3) => 3rd of many' => [':nth-child(-n+3) { %1$s }', '<div class="div-3" style="%1$s">'],
'type & :nth-child(even) => 2nd of many' => ['p:nth-child(even) { %1$s }', '<p class="p-2" style="%1$s">'],
':nth-last-child(even) => 2nd last of many' => [
':nth-last-child(even) { %1$s }',
'<p class="p-6" style="%1$s">',
],
':nth-last-child(even) => 4th last of many' => [
':nth-last-child(even) { %1$s }',
'<p class="p-4" id="p4" style="%1$s">',
],
':nth-last-child(2n) => 2nd last of many' => [
':nth-last-child(2n) { %1$s }',
'<p class="p-6" style="%1$s">',
],
':nth-last-child(2n) => 4th last of many' => [
':nth-last-child(2n) { %1$s }',
'<p class="p-4" id="p4" style="%1$s">',
],
':nth-last-child(3) => 3rd last of many' => [
':nth-last-child(3) { %1$s }',
'<p class="p-5 additional-class" style="%1$s">',
],
':nth-last-child(2n+3) => 3rd last of many' => [
':nth-last-child(2n+3) { %1$s }',
'<p class="p-5 additional-class" style="%1$s">',
],
':nth-last-child(2n+3) => 5th last of many' => [
':nth-last-child(2n+3) { %1$s }',
'<div class="div-3" style="%1$s">',
],
':nth-last-child(-n+3) => 2nd last of many' => [
':nth-last-child(-n+3) { %1$s }',
'<p class="p-6" style="%1$s">',
],
':nth-last-child(-n+3) => 3rd last of many' => [
':nth-last-child(-n+3) { %1$s }',
'<p class="p-5 additional-class" style="%1$s">',
],
'type & :nth-last-child(even) => 2nd last of many' => [
'p:nth-last-child(even) { %1$s }',
'<p class="p-6" style="%1$s">',
],
// broken: nth-of-type without preceding type
'type & :nth-of-type(even) => 2nd of many of type' => [
'p:nth-of-type(even) { %1$s }',
Expand Down Expand Up @@ -734,6 +774,33 @@ public function nonMatchedCssDataProvider()
':nth-child(2n+3) => not 4th of many' => [':nth-child(2n+3) { %1$s }', '<p class="p-4" id="p4">'],
':nth-child(-n+3) => not 4th of many' => [':nth-child(-n+3) { %1$s }', '<p class="p-4" id="p4">'],
':nth-child(-n+3) => not 5th of many' => [':nth-child(-n+3) { %1$s }', '<p class="p-5 additional-class">'],
':nth-last-child(even) => not last of many' => [':nth-last-child(even) { %1$s }', '<p class="p-7">'],
':nth-last-child(even) => not 3rd last of many' => [
':nth-last-child(even) { %1$s }',
'<p class="p-5 additional-class">',
],
':nth-last-child(2n) => not last of many' => [':nth-last-child(2n) { %1$s }', '<p class="p-7">'],
':nth-last-child(2n) => not 3rd last of many' => [
':nth-last-child(2n) { %1$s }',
'<p class="p-5 additional-class">',
],
':nth-last-child(3) => not last of many' => [':nth-last-child(3) { %1$s }', '<p class="p-7">'],
':nth-last-child(3) => not 2nd last of many' => [':nth-last-child(3) { %1$s }', '<p class="p-6">'],
':nth-last-child(3) => not 4th last of many' => [':nth-last-child(3) { %1$s }', '<p class="p-4" id="p4">'],
':nth-last-child(3) => not 6th last of many' => [':nth-last-child(3) { %1$s }', '<p class="p-2">'],
':nth-last-child(2n+3) => not last of many' => [':nth-last-child(2n+3) { %1$s }', '<p class="p-7">'],
':nth-last-child(2n+3) => not 4th last of many' => [
':nth-last-child(2n+3) { %1$s }',
'<p class="p-4" id="p4">',
],
':nth-last-child(-n+3) => not 4th last of many' => [
':nth-last-child(-n+3) { %1$s }',
'<p class="p-4" id="p4">',
],
':nth-last-child(-n+3) => not 5th last of many' => [
':nth-last-child(-n+3) { %1$s }',
'<div class="div-3">',
],
'type & :nth-of-type(even) => not 1st of many of type' => [
'p:nth-of-type(even) { %1$s }',
'<p class="p-1">',
Expand Down
1 change: 1 addition & 0 deletions tests/Unit/EmogrifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,7 @@ public function matchedCssDataProvider()
':nth-child(3) => 3rd of many' => ['*:nth-child(3) { %1$s }', '<div class="div-3" style="%1$s">'],
// broken: nth-child(Xn+Y)
'type & :nth-child(even) => 2nd of many' => ['p:nth-child(even) { %1$s }', '<p class="p-2" style="%1$s">'],
// broken: nth-last-child
// broken: nth-of-type without preceding type
'type & :nth-of-type(even) => 2nd of many of type' => [
'p:nth-of-type(even) { %1$s }',
Expand Down

0 comments on commit a5bea23

Please sign in to comment.