Skip to content

Commit 37f5aab

Browse files
author
Foivos
committed
Merge branch 'link-tag-css'
Load the css from link tags
2 parents c24d71f + 52ae0fb commit 37f5aab

File tree

7 files changed

+169
-11
lines changed

7 files changed

+169
-11
lines changed

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ The goal of this package is to automate the process of inlining that CSS before
1515
## How?
1616
Using a wonderful [CSS inliner package](https://github.com/tijsverkoyen/CssToInlineStyles) wraped in a SwiftMailer plugin and served as a Service Provider it justs works without any configuration.
1717

18-
Turns:
18+
Turns style tag:
1919
```html
2020
<html>
2121
<head>
@@ -31,6 +31,17 @@ Turns:
3131
</body>
3232
</html>
3333
```
34+
Or the link tag:
35+
```html
36+
<html>
37+
<head>
38+
<link rel="stylesheet" type="text/css" href="./tests/css/test.css">
39+
</head>
40+
<body>
41+
<h1>Hey you</h1>
42+
</body>
43+
</html>
44+
```
3445

3546
Into this:
3647
```html

src/CssInlinerPlugin.php

Lines changed: 53 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,7 @@ class CssInlinerPlugin implements \Swift_Events_SendListener
2222
public function __construct(array $options)
2323
{
2424
$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-
}
25+
$this->loadOptions($options);
3126
}
3227

3328
/**
@@ -41,12 +36,14 @@ public function beforeSendPerformed(\Swift_Events_SendEvent $evt)
4136
|| ($message->getContentType() === 'multipart/alternative' && $message->getBody())
4237
|| ($message->getContentType() === 'multipart/mixed' && $message->getBody())
4338
) {
44-
$message->setBody($this->converter->convert($message->getBody(), $this->css));
39+
$body = $this->loadCssFilesFromLinks($message->getBody());
40+
$message->setBody($this->converter->convert($body, $this->css));
4541
}
4642

4743
foreach ($message->getChildren() as $part) {
4844
if (strpos($part->getContentType(), 'text/html') === 0) {
49-
$part->setBody($this->converter->convert($part->getBody(), $this->css));
45+
$body = $this->loadCssFilesFromLinks($part->getBody());
46+
$part->setBody($this->converter->convert($body, $this->css));
5047
}
5148
}
5249
}
@@ -60,4 +57,52 @@ public function sendPerformed(\Swift_Events_SendEvent $evt)
6057
{
6158
// Do Nothing
6259
}
60+
61+
/**
62+
* Load the options
63+
* @param array $options Options array
64+
*/
65+
public function loadOptions($options){
66+
if (isset($options['css-files']) && count($options['css-files']) > 0) {
67+
$this->css = '';
68+
foreach ($options['css-files'] as $file) {
69+
$this->css .= file_get_contents($file);
70+
}
71+
}
72+
}
73+
74+
/**
75+
* Find CSS stylesheet links and load them
76+
*
77+
* Loads the body of the message and passes
78+
* any link stylesheets to $this->css
79+
* Removes any link elements
80+
*
81+
* @return string $message The message
82+
*/
83+
public function loadCssFilesFromLinks($message){
84+
$dom = new \DOMDocument();
85+
$dom->loadHTML($message);
86+
$link_tags = $dom->getElementsByTagName('link');
87+
88+
if($link_tags->length > 0){
89+
do {
90+
if($link_tags->item(0)->getAttribute('rel') == "stylesheet"){
91+
$options['css-files'][] = $link_tags->item(0)->getAttribute('href');
92+
93+
// remove the link node
94+
$link_tags->item(0)->parentNode->removeChild($link_tags->item(0));
95+
}
96+
} while($link_tags->length > 0);
97+
98+
if(isset($options)){
99+
// reload the options
100+
$this->loadOptions($options);
101+
}
102+
103+
return $dom->saveHTML();
104+
}
105+
106+
return $message;
107+
}
63108
}

tests/CssInlinerPluginTest.php

Lines changed: 42 additions & 2 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', 'original-html-with-css','converted-html',
13-
'converted-html-with-css'
12+
'plain-text', 'original-html', 'original-html-with-css', 'original-html-with-link-css', 'original-html-with-links-css', 'converted-html',
13+
'converted-html-with-css', 'converted-html-with-links-css'
1414
);
1515

1616
public function setUp()
@@ -125,4 +125,44 @@ public function itShouldConvertHtmlBodyAsAPart()
125125

126126
$this->assertEquals($this->stubs['converted-html'], $children[0]->getBody());
127127
}
128+
129+
/** @test **/
130+
public function itShouldConvertHtmlBodyWithLinkCss()
131+
{
132+
$mailer = Swift_Mailer::newInstance(Swift_NullTransport::newInstance());
133+
134+
$mailer->registerPlugin(new CssInlinerPlugin($this->options));
135+
136+
$message = Swift_Message::newInstance();
137+
138+
$message->setFrom('[email protected]');
139+
$message->setTo('[email protected]');
140+
$message->setSubject('Test');
141+
142+
$message->setBody($this->stubs['original-html-with-link-css'], 'text/html');
143+
144+
$mailer->send($message);
145+
146+
$this->assertEquals($this->stubs['converted-html-with-css'], $message->getBody());
147+
}
148+
149+
/** @test **/
150+
public function itShouldConvertHtmlBodyWithLinksCss()
151+
{
152+
$mailer = Swift_Mailer::newInstance(Swift_NullTransport::newInstance());
153+
154+
$mailer->registerPlugin(new CssInlinerPlugin($this->options));
155+
156+
$message = Swift_Message::newInstance();
157+
158+
$message->setFrom('[email protected]');
159+
$message->setTo('[email protected]');
160+
$message->setSubject('Test');
161+
162+
$message->setBody($this->stubs['original-html-with-links-css'], 'text/html');
163+
164+
$mailer->send($message);
165+
166+
$this->assertEquals($this->stubs['converted-html-with-links-css'], $message->getBody());
167+
}
128168
}

tests/css/test2.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
body {
2+
background-color: red;
3+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
2+
<html>
3+
<head></head>
4+
<body style="background-color: red;">
5+
<div class="pixels-10" style="width: 10px;">
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>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<html>
2+
<head>
3+
<link rel="stylesheet" type="text/css" href="./tests/css/test.css">
4+
</head>
5+
<body>
6+
<div class="pixels-10">
7+
text
8+
9+
<ul>
10+
<li>
11+
Big list
12+
</li>
13+
<li>
14+
Small list
15+
</li>
16+
</ul>
17+
</div>
18+
</body>
19+
</html>
20+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<html>
2+
<head>
3+
<link rel="stylesheet" type="text/css" href="./tests/css/test.css">
4+
<link rel="stylesheet" type="text/css" href="./tests/css/test2.css">
5+
</head>
6+
<body>
7+
<div class="pixels-10">
8+
text
9+
10+
<ul>
11+
<li>
12+
Big list
13+
</li>
14+
<li>
15+
Small list
16+
</li>
17+
</ul>
18+
</div>
19+
</body>
20+
</html>
21+

0 commit comments

Comments
 (0)