Skip to content

Commit c58e663

Browse files
Merge pull request #38 from Belphemur/master
Update css-to-inline-styles to v2.1
2 parents e1869cb + e84dff0 commit c58e663

10 files changed

+94
-121
lines changed

.travis.yml

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
1+
12
language: php
23

3-
php:
4-
- 5.4
5-
- 5.5
6-
- 5.6
7-
- hhvm
4+
sudo: false
5+
6+
matrix:
7+
include:
8+
- php: 5.5
9+
- php: 5.6
10+
- php: 7.0
11+
- php: hhvm
12+
allow_failures:
13+
- php: hhvm
814

915
install:
1016
- travis_retry composer install --no-interaction --prefer-source

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"require": {
1313
"php": ">=5.4.0",
1414
"illuminate/support": "~5.0",
15-
"tijsverkoyen/css-to-inline-styles": "~1.2"
15+
"tijsverkoyen/css-to-inline-styles": "~2.0"
1616
},
1717
"require-dev" : {
1818
"jakub-onderka/php-parallel-lint": "0.8.*",

config/css-inliner.php

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,14 @@
44

55
/*
66
|--------------------------------------------------------------------------
7-
| Strip styles
7+
| Css Files
88
|--------------------------------------------------------------------------
99
|
10-
| Settings this to false prevents the inliner from removing the style
11-
| definitions that have been inlined.
12-
|
13-
| Notice that media query styles are not inlined, and hence never
14-
| stripped.
15-
|
16-
*/
17-
18-
'strip-styles' => true,
19-
20-
/*
21-
|--------------------------------------------------------------------------
22-
| Remove classes
23-
|--------------------------------------------------------------------------
24-
|
25-
| Settings this to false disables the removal of class attributes from
26-
| your html elements (do not enable this if you use media queries)
10+
| Css file of your style for your emails
11+
| The content of these files will be added directly into the inliner
2712
|
2813
*/
2914

30-
'strip-classes' => true,
15+
'css-files' => [],
3116

3217
];

src/CssInlinerPlugin.php

Lines changed: 21 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -7,68 +7,54 @@
77
class CssInlinerPlugin implements \Swift_Events_SendListener
88
{
99
/**
10-
* @var array
10+
* @var CssToInlineStyles
1111
*/
12-
protected $options;
12+
private $converter;
13+
14+
/**
15+
* @var string
16+
*/
17+
protected $css;
1318

1419
/**
1520
* @param array $options options defined in the configuration file.
1621
*/
1722
public function __construct(array $options)
1823
{
19-
$this->options = $options;
24+
$this->converter = new CssToInlineStyles();
25+
if (isset($options['css-files']) && count($options['css-files']) > 0) {
26+
$this->css = '';
27+
foreach ($options['css-files'] as $file) {
28+
$this->css .= file_get_contents($file);
29+
}
30+
}
2031
}
2132

2233
/**
23-
* @param Swift_Events_SendEvent $evt
34+
* @param \Swift_Events_SendEvent $evt
2435
*/
2536
public function beforeSendPerformed(\Swift_Events_SendEvent $evt)
2637
{
2738
$message = $evt->getMessage();
2839

29-
$converter = new CssToInlineStyles();
30-
$this->applySettings($converter);
31-
32-
if ($message->getContentType() === 'text/html' ||
33-
($message->getContentType() === 'multipart/alternative' && $message->getBody()) ||
34-
($message->getContentType() === 'multipart/mixed' && $message->getBody())
40+
if ($message->getContentType() === 'text/html'
41+
|| ($message->getContentType() === 'multipart/alternative' && $message->getBody())
42+
|| ($message->getContentType() === 'multipart/mixed' && $message->getBody())
3543
) {
36-
$converter->setHTML($message->getBody());
37-
$message->setBody($converter->convert());
44+
$message->setBody($this->converter->convert($message->getBody(), $this->css));
3845
}
3946

4047
foreach ($message->getChildren() as $part) {
4148
if (strpos($part->getContentType(), 'text/html') === 0) {
42-
$converter->setHTML($part->getBody());
43-
$part->setBody($converter->convert());
49+
$part->setBody($this->converter->convert($part->getBody(), $this->css));
4450
}
4551
}
4652
}
4753

48-
/**
49-
* Applies the configuration settings.
50-
*
51-
* @param CssToInlineStyles $converter
52-
*/
53-
private function applySettings(CssToInlineStyles $converter)
54-
{
55-
// Always enabled because there is no way to specify an external style sheet
56-
// when using this plugin
57-
$converter->setUseInlineStylesBlock();
58-
59-
if ($this->options['strip-styles']) {
60-
$converter->setStripOriginalStyleTags();
61-
}
62-
63-
if ($this->options['strip-classes']) {
64-
$converter->setCleanup();
65-
}
66-
}
67-
6854
/**
6955
* Do nothing
7056
*
71-
* @param Swift_Events_SendEvent $evt
57+
* @param \Swift_Events_SendEvent $evt
7258
*/
7359
public function sendPerformed(\Swift_Events_SendEvent $evt)
7460
{

tests/CssInlinerPluginTest.php

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ class CssInlinerPluginTest extends PHPUnit_Framework_TestCase
99
protected $options;
1010

1111
protected static $stubDefinitions = array(
12-
'plain-text', 'original-html', 'converted-html', 'converted-html-with-classes',
13-
'converted-html-with-styles'
12+
'plain-text', 'original-html', 'original-html-with-css','converted-html',
13+
'converted-html-with-css'
1414
);
1515

1616
public function setUp()
@@ -42,31 +42,9 @@ public function itShouldConvertHtmlBody()
4242
}
4343

4444
/** @test **/
45-
public function itShouldConvertHtmlBodyKeepingClasses()
45+
public function itShouldConvertHtmlBodyWithGivenCss()
4646
{
47-
$this->options['strip-classes'] = false;
48-
49-
$mailer = Swift_Mailer::newInstance(Swift_NullTransport::newInstance());
50-
51-
$mailer->registerPlugin(new CssInlinerPlugin($this->options));
52-
53-
$message = Swift_Message::newInstance();
54-
55-
$message->setFrom('[email protected]');
56-
$message->setTo('[email protected]');
57-
$message->setSubject('Test');
58-
$message->setBody($this->stubs['original-html'], 'text/html');
59-
60-
$mailer->send($message);
61-
62-
$this->assertEquals($this->stubs['converted-html-with-classes'], $message->getBody());
63-
}
64-
65-
/** @test **/
66-
public function itShouldConvertHtmlBodyKeepingStyles()
67-
{
68-
$this->options['strip-styles'] = false;
69-
47+
$this->options['css-files'] = [__DIR__ . '/css/test.css'];
7048
$mailer = Swift_Mailer::newInstance(Swift_NullTransport::newInstance());
7149

7250
$mailer->registerPlugin(new CssInlinerPlugin($this->options));
@@ -76,11 +54,11 @@ public function itShouldConvertHtmlBodyKeepingStyles()
7654
$message->setFrom('[email protected]');
7755
$message->setTo('[email protected]');
7856
$message->setSubject('Test');
79-
$message->setBody($this->stubs['original-html'], 'text/html');
57+
$message->setBody($this->stubs['original-html-with-css'], 'text/html');
8058

8159
$mailer->send($message);
8260

83-
$this->assertEquals($this->stubs['converted-html-with-styles'], $message->getBody());
61+
$this->assertEquals($this->stubs['converted-html-with-css'], $message->getBody());
8462
}
8563

8664
/** @test **/

tests/css/test.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
div.pixels-10 {
2+
width: 10px;
3+
}
Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
2-
<html><head><style></style></head><body>
3-
<div class="block" style="height: 20px; width: 100px;">
2+
<html>
3+
<head></head>
4+
<body>
5+
<div class="pixels-10" style="width: 10px;">
46
text
57

6-
<ul><li>
8+
<ul>
9+
<li>
710
Big list
811
</li>
9-
<li class="small" style="margin: 10px;">
12+
<li>
1013
Small list
1114
</li>
12-
</ul></div>
13-
</body></html>
15+
</ul>
16+
</div>
17+
</body>
18+
</html>

tests/stubs/converted-html-with-styles.stub

Lines changed: 0 additions & 21 deletions
This file was deleted.

tests/stubs/converted-html.stub

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,26 @@
11
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
2-
<html><head><style></style></head><body>
3-
<div style="height: 20px; width: 100px;">
2+
<html>
3+
<head><style>
4+
.block {
5+
width: 100px;
6+
height: 20px;
7+
}
8+
div.block ul li.small {
9+
margin: 10px;
10+
}
11+
</style></head>
12+
<body>
13+
<div class="block" style="width: 100px; height: 20px;">
414
text
515

6-
<ul><li>
16+
<ul>
17+
<li>
718
Big list
819
</li>
9-
<li style="margin: 10px;">
20+
<li class="small" style="margin: 10px;">
1021
Small list
1122
</li>
12-
</ul></div>
13-
</body></html>
23+
</ul>
24+
</div>
25+
</body>
26+
</html>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<html>
2+
<head>
3+
</head>
4+
<body>
5+
<div class="pixels-10">
6+
text
7+
8+
<ul>
9+
<li>
10+
Big list
11+
</li>
12+
<li>
13+
Small list
14+
</li>
15+
</ul>
16+
</div>
17+
</body>
18+
</html>

0 commit comments

Comments
 (0)