Skip to content

Commit

Permalink
[BUGFIX] Fix first-child and last-child selectors
Browse files Browse the repository at this point in the history
Fixes #51
Fixes #192
  • Loading branch information
oliverklee committed Oct 12, 2015
1 parent 5d0a61f commit e0fd86c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 12 deletions.
6 changes: 3 additions & 3 deletions Classes/Emogrifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ private function parseCssRules($css)
foreach ($selectors as $selector) {
// don't process pseudo-elements and behavioral (dynamic) pseudo-classes;
// only allow structural pseudo-classes
if (strpos($selector, ':') !== false && !preg_match('/:\\S+\\-(child|type)\\(/i', $selector)) {
if (strpos($selector, ':') !== false && !preg_match('/:\\S+\\-(child|type\\()/i', $selector)) {
continue;
}

Expand Down Expand Up @@ -1012,8 +1012,8 @@ function (array $matches) {
'child' => '/',
'adjacent sibling' => '/following-sibling::*[1]/self::',
'descendant' => '//',
':first-child' => '*[1]/self::\\1',
':last-child' => '*[last()]/self::\\1',
':first-child' => '\\1/*[1]',
':last-child' => '\\1/*[last()]',
'attribute only' => '*[@\\1]',
'attribute' => '\\1[@\\2]',
'exact attribute' => '\\1[@\\2="\\3"]',
Expand Down
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,6 @@ Emogrifier currently support the following
* attribute presence
* attribute value
* attribute only

The following selectors are implemented, but currently are broken:

* first-child (currently broken)
* last-child (currently broken)

Expand Down
24 changes: 18 additions & 6 deletions Tests/Unit/EmogrifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ public function emogrifyCanAssignStyleRulesFromTwoDifferentMatchersToElement()
/**
* Data provide for selectors.
*
* @return array[]
* @return string[][]
*/
public function selectorDataProvider()
{
Expand Down Expand Up @@ -633,18 +633,29 @@ public function selectorDataProvider()
=> ['span[title="bonjour"] {' . $styleRule . '} ', '#<span title="buenas dias">#'],
'attribute value selector SPAN[title] not matches element without any attributes'
=> ['span[title="bonjour"] {' . $styleRule . '} ', '#<span>#'],
'BODY:first-child matches first child'
=> ['body:first-child {' . $styleRule . '} ', '#<p class="p-1" style="' . $styleRule . '">#'],
'BODY:first-child not matches middle child'
=> ['body:first-child {' . $styleRule . '} ', '#<p class="p-2">#'],
'BODY:first-child not matches last child'
=> ['body:first-child {' . $styleRule . '} ', '#<p class="p-3">#'],
'BODY:last-child not matches first child' => ['body:last-child {' . $styleRule . '} ', '#<p class="p-1">#'],
'BODY:last-child not matches middle child'
=> ['body:last-child {' . $styleRule . '} ', '#<p class="p-2">#'],
'BODY:last-child matches last child'
=> ['body:last-child {' . $styleRule . '} ', '#<p class="p-3" style="' . $styleRule . '">#'],
];
}

/**
* @test
*
* @param string $css the complete CSS
* @param string $containedHtml regular expression for the the HTML that needs to be contained in the merged HTML
* @param string $htmlRegularExpression regular expression for the the HTML that needs to be contained in the HTML
*
* @dataProvider selectorDataProvider
*/
public function emogrifierMatchesSelectors($css, $containedHtml)
public function emogrifierMatchesSelectors($css, $htmlRegularExpression)
{
$html = $this->html5DocumentType .
'<html id="html">' .
Expand All @@ -654,13 +665,14 @@ public function emogrifierMatchesSelectors($css, $containedHtml)
' <p class="p-3"><span title="buenas dias">some</span> more text</p>' .
' </body>' .
'</html>';

$this->subject->setHtml($html);
$this->subject->setCss($css);

$result = $this->subject->emogrify();

self::assertRegExp(
$containedHtml,
$this->subject->emogrify()
$htmlRegularExpression,
$result
);
}

Expand Down

0 comments on commit e0fd86c

Please sign in to comment.