Skip to content

Commit

Permalink
[FEATURE] Add CssInliner::getMatchingUninlinableSelectors
Browse files Browse the repository at this point in the history
Part of #380.

The basic functionality of the method is unit-tested.  More extensive unit tests
involving all kinds CSS rules and HTML already exist for testing the full
content of the `<style>` element containing the uninlinable rules.
  • Loading branch information
JakeQZ committed Sep 12, 2019
1 parent 14f540f commit eac5952
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Expand Up @@ -6,6 +6,9 @@ This project adheres to [Semantic Versioning](https://semver.org/).
## x.y.z

### Added
- Add `CssInliner::getMatchingUninlinableSelectors`
([#380](https://github.com/MyIntervals/emogrifier/issues/380),
[#707](https://github.com/MyIntervals/emogrifier/pull/707))
- Add tests for `:nth-child` and `:nth-of-type`
([#71](https://github.com/MyIntervals/emogrifier/issues/71),
[#698](https://github.com/MyIntervals/emogrifier/pull/698))
Expand Down
14 changes: 14 additions & 0 deletions src/Emogrifier/CssInliner.php
Expand Up @@ -626,6 +626,20 @@ private function attributeValueIsImportant($attributeValue)
return \strtolower(\substr(\trim($attributeValue), -10)) === '!important';
}

/**
* Gets the array of selectors present in the CSS provided to `inlineCss()` for which the declarations could not be
* applied as inline styles, but which may affect elements in the HTML. The relevant CSS will have been placed in a
* `<style>` element. The selectors may include those used within @media rules or those involving dynamic
* pseudo-classes (such as `:hover`) or pseudo-elements (such as `::after`). `inlineCss()` must have been called
* first, otherwise an empty array is returned.
*
* @return string[]
*/
public function getMatchingUninlinableSelectors()
{
return \array_column($this->matchingUninlinableCssRules, 'selector');
}

/**
* Determines which of `$cssRules` actually apply to `$this->domDocument`, and sets them in
* `$this->matchingUninlinableCssRules`.
Expand Down
53 changes: 53 additions & 0 deletions tests/Unit/CssInlinerTest.php
Expand Up @@ -2618,6 +2618,59 @@ public function inlineCssNotInDebugModeKeepsInvalidOrUnrecognizedSelectorsInMedi
self::assertContainsCss($css, $subject->render());
}

/**
* @test
*/
public function getMatchingUninlinableSelectorsReturnsMatchingUninlinableSelector()
{
$subject = $this->buildDebugSubject('<html><p>foo</p></html>');
$subject->inlineCss('p:hover { color: green; }');

$result = $subject->getMatchingUninlinableSelectors();

static::assertContains('p:hover', $result);
}

/**
* @test
*/
public function getMatchingUninlinableSelectorsReturnsMultipleMatchingUninlinableSelectors()
{
$subject = $this->buildDebugSubject('<html><p>foo</p></html>');
$subject->inlineCss('p:hover { color: green; } p::after { content: "bar"; }');

$result = $subject->getMatchingUninlinableSelectors();

static::assertContains('p:hover', $result);
static::assertContains('p::after', $result);
}

/**
* @test
*/
public function getMatchingUninlinableSelectorsNotReturnsNonMatchingUninlinableSelector()
{
$subject = $this->buildDebugSubject('<html><p>foo</p></html>');
$subject->inlineCss('a:hover { color: red; }');

$result = $subject->getMatchingUninlinableSelectors();

static::assertSame([], $result);
}

/**
* @test
*/
public function getMatchingUninlinableSelectorsNotReturnsMatchingInlinableSelector()
{
$subject = $this->buildDebugSubject('<html><p>foo</p></html>');
$subject->inlineCss('p { color: red; }');

$result = $subject->getMatchingUninlinableSelectors();

static::assertSame([], $result);
}

/**
* @test
*/
Expand Down

0 comments on commit eac5952

Please sign in to comment.