Skip to content

Commit

Permalink
[TASK] Allow chaining all configuration methods (#854)
Browse files Browse the repository at this point in the history
The example given in the documentation using `disableStyleBlocksParsing()` will
now actually work.

Fixes #824
  • Loading branch information
JakeQZ authored Mar 31, 2020
1 parent ddf0174 commit 9a1cba2
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 7 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
- Configuration setting methods now all return `$this` to allow chaining
([#824](https://github.com/MyIntervals/emogrifier/pull/824),
[#854](https://github.com/MyIntervals/emogrifier/pull/854))
- Add support for PHP 7.4
([#821](https://github.com/MyIntervals/emogrifier/pull/821))
- Disable php-cs-fixer Yoda conditions
Expand Down
28 changes: 21 additions & 7 deletions src/CssInliner.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,47 +211,55 @@ public function inlineCss(string $css = ''): self
/**
* Disables the parsing of inline styles.
*
* @return void
* @return self fluent interface
*/
public function disableInlineStyleAttributesParsing()
{
$this->isInlineStyleAttributesParsingEnabled = false;

return $this;
}

/**
* Disables the parsing of <style> blocks.
*
* @return void
* @return self fluent interface
*/
public function disableStyleBlocksParsing()
{
$this->isStyleBlocksParsingEnabled = false;

return $this;
}

/**
* Marks a media query type to keep.
*
* @param string $mediaName the media type name, e.g., "braille"
*
* @return void
* @return self fluent interface
*/
public function addAllowedMediaType(string $mediaName)
{
$this->allowedMediaTypes[$mediaName] = true;

return $this;
}

/**
* Drops a media query type from the allowed list.
*
* @param string $mediaName the tag name, e.g., "braille"
*
* @return void
* @return self fluent interface
*/
public function removeAllowedMediaType(string $mediaName)
{
if (isset($this->allowedMediaTypes[$mediaName])) {
unset($this->allowedMediaTypes[$mediaName]);
}

return $this;
}

/**
Expand All @@ -261,37 +269,43 @@ public function removeAllowedMediaType(string $mediaName)
*
* @param string $selector the selector to exclude, e.g., ".editor"
*
* @return void
* @return self fluent interface
*/
public function addExcludedSelector(string $selector)
{
$this->excludedSelectors[$selector] = true;

return $this;
}

/**
* No longer excludes the nodes matching this selector from emogrification.
*
* @param string $selector the selector to no longer exclude, e.g., ".editor"
*
* @return void
* @return self fluent interface
*/
public function removeExcludedSelector(string $selector)
{
if (isset($this->excludedSelectors[$selector])) {
unset($this->excludedSelectors[$selector]);
}

return $this;
}

/**
* Sets the debug mode.
*
* @param bool $debug set to true to enable debug mode
*
* @return void
* @return self fluent interface
*/
public function setDebug(bool $debug)
{
$this->debug = $debug;

return $this;
}

/**
Expand Down
84 changes: 84 additions & 0 deletions tests/Unit/CssInlinerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,18 @@ public function isAbstractHtmlProcessor()
self::assertInstanceOf(AbstractHtmlProcessor::class, $subject);
}

/**
* @test
*/
public function setDebugProvidesFluentInterface()
{
$subject = CssInliner::fromHtml('<html></html>');

$result = $subject->setDebug(false);

self::assertSame($subject, $result);
}

/**
* @test
*/
Expand Down Expand Up @@ -1687,6 +1699,18 @@ public function inlineCssKeepsRulesCopiedToStyleElementInSpecifiedOrder(
self::assertContainsCss($rule1 . $rule2, $subject->render());
}

/**
* @test
*/
public function removeAllowedMediaTypeProvidesFluentInterface()
{
$subject = CssInliner::fromHtml('<html></html>');

$result = $subject->removeAllowedMediaType('screen');

self::assertSame($subject, $result);
}

/**
* @test
*/
Expand All @@ -1701,6 +1725,18 @@ public function removeAllowedMediaTypeRemovesStylesForTheGivenMediaType()
self::assertNotContains('@media', $subject->render());
}

/**
* @test
*/
public function addAllowedMediaTypeProvidesFluentInterface()
{
$subject = CssInliner::fromHtml('<html></html>');

$result = $subject->addAllowedMediaType('braille');

self::assertSame($subject, $result);
}

/**
* @test
*/
Expand Down Expand Up @@ -2334,6 +2370,18 @@ public function inlineCssAppliesCssFromStyleNodes()
self::assertContains('<html style="' . $styleAttributeValue . '">', $subject->render());
}

/**
* @test
*/
public function disableStyleBlocksParsingProvidesFluentInterface()
{
$subject = CssInliner::fromHtml('<html></html>');

$result = $subject->disableStyleBlocksParsing();

self::assertSame($subject, $result);
}

/**
* @test
*/
Expand Down Expand Up @@ -2367,6 +2415,18 @@ public function inlineCssWhenStyleBlocksParsingDisabledKeepInlineStyles()
self::assertContains('<p style="' . $styleAttributeValue . '">', $subject->renderBodyContent());
}

/**
* @test
*/
public function disableInlineStyleAttributesParsingProvidesFluentInterface()
{
$subject = CssInliner::fromHtml('<html></html>');

$result = $subject->disableInlineStyleAttributesParsing();

self::assertSame($subject, $result);
}

/**
* @test
*/
Expand Down Expand Up @@ -2640,6 +2700,18 @@ public function importantStyleRuleFromInlineCssOverwritesImportantStyleRuleFromE
self::assertContains('<p style="padding: 1px; text-align: center; margin: 2px;">', $result);
}

/**
* @test
*/
public function addExcludedSelectorProvidesFluentInterface()
{
$subject = CssInliner::fromHtml('<html></html>');

$result = $subject->addExcludedSelector('p.x');

self::assertSame($subject, $result);
}

/**
* @test
*/
Expand Down Expand Up @@ -2694,6 +2766,18 @@ public function addExcludedSelectorCanExcludeSubtree()
self::assertContains($htmlSubtree, $subject->renderBodyContent());
}

/**
* @test
*/
public function removeExcludedSelectorProvidesFluentInterface()
{
$subject = CssInliner::fromHtml('<html></html>');

$result = $subject->removeExcludedSelector('p.x');

self::assertSame($subject, $result);
}

/**
* @test
*/
Expand Down

0 comments on commit 9a1cba2

Please sign in to comment.