Skip to content

Commit

Permalink
[TASK] Test/document addExcludedSelector with subtree (#768)
Browse files Browse the repository at this point in the history
Resolves #347.
  • Loading branch information
JakeQZ authored and oliverklee committed Oct 1, 2019
1 parent c8f7fe6 commit 0b6788b
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 1 deletion.
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 and document excluding entire subtree with `addExcludedSelector()`
([#347](https://github.com/MyIntervals/emogrifier/issues/347),
[#768](https://github.com/MyIntervals/emogrifier/pull/768))
- Test that rules with `:optional` or `:required` are copied to the `<style>`
element ([#748](https://github.com/MyIntervals/emogrifier/issues/748),
[#765](https://github.com/MyIntervals/emogrifier/pull/765))
Expand Down
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,15 @@ the `emogrify` method):
* `->removeAllowedMediaType(string $mediaName)` - You can use this
method to remove media types that Emogrifier keeps.
* `->addExcludedSelector(string $selector)` - Keeps elements from
being affected by CSS inlining.
being affected by CSS inlining. Note that only elements matching the supplied
selector(s) will be excluded from CSS inlining, not necessarily their
descendants. If you wish to exclude an entire subtree, you should provide
selector(s) which will match all elements in the subtree, for example by using
the universal selector:
```php
$cssInliner->addExcludedSelector('.message-preview');
$cssInliner->addExcludedSelector('.message-preview *');
```

### Using the legacy Emogrifier class

Expand Down
15 changes: 15 additions & 0 deletions tests/Unit/CssInlinerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2648,6 +2648,21 @@ public function addExcludedSelectorKeepsNonMatchingElements()
self::assertContains('<p style="margin: 0;"></p>', $subject->renderBodyContent());
}

/**
* @test
*/
public function addExcludedSelectorCanExcludeSubtree()
{
$htmlSubtree = '<div class="message-preview"><p><em>Message</em> <strong>preview.</strong></p></div>';
$subject = $this->buildDebugSubject('<html><body>' . $htmlSubtree . '<p>Another paragraph.</p></body></html>');

$subject->addExcludedSelector('.message-preview');
$subject->addExcludedSelector('.message-preview *');
$subject->inlineCss('p { margin: 0; } em { font-style: italic; } strong { font-weight: bold; }');

self::assertContains($htmlSubtree, $subject->renderBodyContent());
}

/**
* @test
*/
Expand Down
16 changes: 16 additions & 0 deletions tests/Unit/EmogrifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2907,6 +2907,22 @@ public function addExcludedSelectorKeepsNonMatchingElementsInEmogrification()
self::assertContains('<p style="margin: 0;"></p>', $result);
}

/**
* @test
*/
public function addExcludedSelectorCanExcludeSubtree()
{
$htmlSubtree = '<div class="message-preview"><p><em>Message</em> <strong>preview.</strong></p></div>';
$this->subject->setHtml('<html><body>' . $htmlSubtree . '<p>Another paragraph.</p></body></html>');
$this->subject->setCss('p { margin: 0; } em { font-style: italic; } strong { font-weight: bold; }');

$this->subject->addExcludedSelector('.message-preview');
$this->subject->addExcludedSelector('.message-preview *');
$result = $this->subject->emogrify();

self::assertContains($htmlSubtree, $result);
}

/**
* @test
*/
Expand Down

0 comments on commit 0b6788b

Please sign in to comment.