1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace InlineStyle\Tests; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Test class for InlineStyle. |
7
|
|
|
* Generated by PHPUnit on 2010-03-10 at 21:52:44. |
8
|
|
|
*/ |
9
|
|
|
use InlineStyle\InlineStyle; |
10
|
|
|
|
11
|
|
|
class InlineStyleTest extends \PHPUnit_Framework_TestCase |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var \InlineStyle\InlineStyle |
15
|
|
|
*/ |
16
|
|
|
protected $object; |
17
|
|
|
protected $basedir; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Sets up the fixture, for example, opens a network connection. |
21
|
|
|
* This method is called before a test is executed. |
22
|
|
|
*/ |
23
|
|
|
protected function setUp() |
24
|
|
|
{ |
25
|
|
|
$this->basedir = __DIR__."/testfiles"; |
26
|
|
|
$this->object = new InlineStyle($this->basedir."/test.html"); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Tears down the fixture, for example, closes a network connection. |
31
|
|
|
* This method is called after a test is executed. |
32
|
|
|
*/ |
33
|
|
|
protected function tearDown() |
34
|
|
|
{ |
35
|
|
|
unset($this->object); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function testGetHTML() |
39
|
|
|
{ |
40
|
|
|
$this->assertEquals( |
41
|
|
|
file_get_contents($this->basedir."/testGetHTML.html"), |
42
|
|
|
$this->object->getHTML()); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function testApplyStyleSheet() |
46
|
|
|
{ |
47
|
|
|
$this->object->applyStyleSheet("p:not(.p2) { color: red }"); |
48
|
|
|
$this->assertEquals( |
49
|
|
|
file_get_contents($this->basedir."/testApplyStylesheet.html"), |
50
|
|
|
$this->object->getHTML()); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function testApplyRule() |
54
|
|
|
{ |
55
|
|
|
$this->object->applyRule("p:not(.p2)", "color: red"); |
56
|
|
|
$this->assertEquals( |
57
|
|
|
file_get_contents($this->basedir."/testApplyStylesheet.html"), |
58
|
|
|
$this->object->getHTML()); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function testExtractStylesheets() |
62
|
|
|
{ |
63
|
|
|
$stylesheets = $this->object->extractStylesheets(null, $this->basedir); |
64
|
|
|
$this->assertEquals(include $this->basedir."/testExtractStylesheets.php",$stylesheets); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function testApplyExtractedStylesheet() |
68
|
|
|
{ |
69
|
|
|
$stylesheets = $this->object->extractStylesheets(null, $this->basedir); |
70
|
|
|
$this->object->applyStylesheet($stylesheets); |
71
|
|
|
|
72
|
|
|
$this->assertEquals( |
73
|
|
|
file_get_contents($this->basedir."/testApplyExtractedStylesheet.html"), |
74
|
|
|
$this->object->getHTML()); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
View Code Duplication |
public function testParseStyleSheet() |
|
|
|
|
78
|
|
|
{ |
79
|
|
|
$parsed = $this->object->parseStylesheet("p:not(.p2) { color: red }"); |
80
|
|
|
$this->assertEquals( |
81
|
|
|
array(array("p:not(.p2)", "color: red")), |
82
|
|
|
$parsed); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
View Code Duplication |
public function testParseStyleSheetWithComments() |
|
|
|
|
86
|
|
|
{ |
87
|
|
|
$parsed = $this->object->parseStylesheet("p:not(.p2) { /* blah */ color: red }"); |
88
|
|
|
$this->assertEquals( |
89
|
|
|
array(array("p:not(.p2)", "color: red")), |
90
|
|
|
$parsed); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function testIllegalXmlUtf8Chars() |
94
|
|
|
{ |
95
|
|
|
// check an exception is not thrown when loading up illegal XML UTF8 chars |
96
|
|
|
new InlineStyle("<html><body>".chr(2).chr(3).chr(4).chr(5)."</body></html>"); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function testGetScoreForSelector() |
100
|
|
|
{ |
101
|
|
|
$this->assertEquals( |
102
|
|
|
array(1,1,3), |
103
|
|
|
$this->object->getScoreForSelector('ul#nav li.active a'), |
104
|
|
|
'ul#nav li.active a' |
105
|
|
|
); |
106
|
|
|
|
107
|
|
|
$this->assertEquals( |
108
|
|
|
array(0,2,3), |
109
|
|
|
$this->object->getScoreForSelector('body.ie7 .col_3 h2 ~ h2'), |
110
|
|
|
'body.ie7 .col_3 h2 ~ h2' |
111
|
|
|
); |
112
|
|
|
|
113
|
|
|
$this->assertEquals( |
114
|
|
|
array(1,0,2), |
115
|
|
|
$this->object->getScoreForSelector('#footer *:not(nav) li'), |
116
|
|
|
'#footer *:not(nav) li' |
117
|
|
|
); |
118
|
|
|
|
119
|
|
|
$this->assertEquals( |
120
|
|
|
array(0,0,7), |
121
|
|
|
$this->object->getScoreForSelector('ul > li ul li ol li:first-letter'), |
122
|
|
|
'ul > li ul li ol li:first-letter' |
123
|
|
|
); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
function testSortingParsedStylesheet() |
|
|
|
|
127
|
|
|
{ |
128
|
|
|
$parsed = $this->object->parseStylesheet(<<<CSS |
129
|
|
|
ul#nav li.active a, body.ie7 .col_3 h2 ~ h2 { |
130
|
|
|
color: blue; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
ul > li ul li ol li:first-letter { |
134
|
|
|
color: red; |
135
|
|
|
} |
136
|
|
|
CSS |
137
|
|
|
); |
138
|
|
|
$this->assertEquals(array ( |
139
|
|
|
array ( |
140
|
|
|
'ul#nav li.active a', |
141
|
|
|
'color: blue', |
142
|
|
|
), |
143
|
|
|
array ( |
144
|
|
|
'body.ie7 .col_3 h2 ~ h2', |
145
|
|
|
'color: blue', |
146
|
|
|
), |
147
|
|
|
array ( |
148
|
|
|
'ul > li ul li ol li:first-letter', |
149
|
|
|
'color: red', |
150
|
|
|
), |
151
|
|
|
), $parsed); |
152
|
|
|
|
153
|
|
|
$parsed = $this->object->sortSelectorsOnSpecificity($parsed); |
154
|
|
|
|
155
|
|
|
$this->assertEquals(array ( |
156
|
|
|
array ( |
157
|
|
|
'ul > li ul li ol li:first-letter', |
158
|
|
|
'color: red', |
159
|
|
|
), |
160
|
|
|
array ( |
161
|
|
|
'body.ie7 .col_3 h2 ~ h2', |
162
|
|
|
'color: blue', |
163
|
|
|
), |
164
|
|
|
array ( |
165
|
|
|
'ul#nav li.active a', |
166
|
|
|
'color: blue', |
167
|
|
|
), |
168
|
|
|
), $parsed); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
function testApplyStylesheetObeysSpecificity() |
|
|
|
|
172
|
|
|
{ |
173
|
|
|
$this->object->applyStylesheet(<<<CSS |
174
|
|
|
p { |
175
|
|
|
color: red; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
.p2 { |
179
|
|
|
color: blue; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
p.p2 { |
183
|
|
|
color: green; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
CSS |
187
|
|
|
); |
188
|
|
|
$this->assertEquals( |
189
|
|
|
file_get_contents($this->basedir."/testApplyStylesheetObeysSpecificity.html"), |
190
|
|
|
$this->object->getHTML()); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
function testDocDocumentDirectly() |
|
|
|
|
194
|
|
|
{ |
195
|
|
|
$dom = new \DOMDocument(); |
196
|
|
|
$dom->formatOutput = false; |
197
|
|
|
$dom->loadHTML('<!doctype html><html><body><div></div></body></html>'); |
198
|
|
|
|
199
|
|
|
$this->object->loadDomDocument($dom); |
200
|
|
|
|
201
|
|
|
$this->object->applyRule('div', 'color: red'); |
202
|
|
|
|
203
|
|
|
$this->assertEquals('<!DOCTYPE html> |
204
|
|
|
<html><body><div style="color: red"></div></body></html> |
205
|
|
|
', $dom->saveHTML()); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Regression test for #14 Selectors are sometimes sorted into the wrong cascading order |
210
|
|
|
*/ |
211
|
|
|
function testSortingOnSpecifitySameSpecificity() |
|
|
|
|
212
|
|
|
{ |
213
|
|
|
$parsed = $this->object->parseStylesheet(<<<CSS |
214
|
|
|
ul { |
215
|
|
|
color: blue; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
ul.class { |
219
|
|
|
color: green; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
ul { |
223
|
|
|
color: red; |
224
|
|
|
} |
225
|
|
|
CSS |
226
|
|
|
); |
227
|
|
|
|
228
|
|
|
$parsed = $this->object->sortSelectorsOnSpecificity($parsed); |
229
|
|
|
|
230
|
|
|
$this->assertEquals(array( |
231
|
|
|
array( |
232
|
|
|
'ul', |
233
|
|
|
'color: blue' |
234
|
|
|
), |
235
|
|
|
array( |
236
|
|
|
'ul', |
237
|
|
|
'color: red' |
238
|
|
|
), |
239
|
|
|
array( |
240
|
|
|
'ul.class', |
241
|
|
|
'color: green' |
242
|
|
|
), |
243
|
|
|
), $parsed); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
function testNonWorkingPseudoSelectors() |
|
|
|
|
247
|
|
|
{ |
248
|
|
|
// Regressiontest for #5 |
249
|
|
|
$this->object->applyStylesheet(<<<CSS |
250
|
|
|
ul#nav li.active a:link, body.ie7 .col_3:visited h2 ~ h2 { |
251
|
|
|
color: blue; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
ul > li ul li:active ol li:first-letter { |
255
|
|
|
color: red; |
256
|
|
|
} |
257
|
|
|
CSS |
258
|
|
|
); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Regression tests for #10 _styleToArray crashes when presented with an invalid property name |
263
|
|
|
*/ |
264
|
|
|
function testInvalidCssProperties() |
|
|
|
|
265
|
|
|
{ |
266
|
|
|
$this->object->applyStylesheet(<<<CSS |
267
|
|
|
ul { |
268
|
|
|
asohdtoairet; |
269
|
|
|
garbage: )&%)*(%); |
270
|
|
|
} |
271
|
|
|
CSS |
272
|
|
|
); |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
function testRegression24() { |
|
|
|
|
276
|
|
|
$content = '<p style="text-align:center;">Hello World!</p>'; |
277
|
|
|
$htmldoc = new InlineStyle($content); |
278
|
|
|
$htmldoc->applyStylesheet('p{ |
279
|
|
|
text-align: left; |
280
|
|
|
}'); |
281
|
|
|
$expected = <<<HTML |
282
|
|
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"> |
283
|
|
|
<html><body><p style="text-align:center">Hello World!</p></body></html> |
284
|
|
|
|
285
|
|
|
HTML; |
286
|
|
|
|
287
|
|
|
|
288
|
|
|
$this->assertEquals($expected, $htmldoc->getHTML()); |
289
|
|
|
$htmldoc->applyStylesheet('p{ |
290
|
|
|
text-align: left; |
291
|
|
|
}'); |
292
|
|
|
$this->assertEquals($expected, $htmldoc->getHTML()); |
293
|
|
|
} |
294
|
|
|
|
295
|
|
View Code Duplication |
function testMultipleStylesheets28() { |
|
|
|
|
296
|
|
|
$htmldoc = new InlineStyle(file_get_contents($this->basedir . '/testMultipleStylesheets.html')); |
297
|
|
|
$htmldoc->applyStylesheet($htmldoc->extractStylesheets(null, $this->basedir)); |
298
|
|
|
$expected = <<<HTML |
299
|
|
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"> |
300
|
|
|
<html> |
301
|
|
|
<head><title>Example</title></head> |
302
|
|
|
<body> |
303
|
|
|
<p style='margin:0;padding:0 0 10px 0;background-image: url("someimage.jpg")'>Paragraph</p> |
304
|
|
|
<strong style="font-weight: bold">Strong</strong> |
305
|
|
|
<br> |
306
|
|
|
</body> |
307
|
|
|
</html> |
308
|
|
|
|
309
|
|
|
HTML; |
310
|
|
|
$this->assertEquals($expected, $htmldoc->getHTML()); |
311
|
|
|
} |
312
|
|
|
|
313
|
|
View Code Duplication |
function testMediaStylesheets31() { |
|
|
|
|
314
|
|
|
$htmldoc = new InlineStyle(file_get_contents($this->basedir . '/testMediaStylesheets31.html')); |
315
|
|
|
$htmldoc->applyStylesheet($htmldoc->extractStylesheets(null, $this->basedir)); |
316
|
|
|
$expected = <<<HTML |
317
|
|
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"> |
318
|
|
|
<html> |
319
|
|
|
<head><title>Example</title></head> |
320
|
|
|
<body> |
321
|
|
|
<style type="text/css" media="print"> |
322
|
|
|
h1{ |
323
|
|
|
display:none; |
324
|
|
|
} |
325
|
|
|
</style> |
326
|
|
|
<h1 style="color:yellow">An example title</h1> |
327
|
|
|
<p style="color:yellow !important;line-height:1.5em">Paragraph 1</p> |
328
|
|
|
</body> |
329
|
|
|
</html> |
330
|
|
|
|
331
|
|
|
HTML; |
332
|
|
|
$this->assertEquals($expected, $htmldoc->getHTML()); |
333
|
|
|
} |
334
|
|
|
|
335
|
|
View Code Duplication |
function testLinkedMediaStylesheets31() { |
|
|
|
|
336
|
|
|
$htmldoc = new InlineStyle(file_get_contents($this->basedir . '/testLinkedMediaStylesheets31.html')); |
337
|
|
|
$htmldoc->applyStylesheet($htmldoc->extractStylesheets(null, $this->basedir)); |
338
|
|
|
$expected = <<<HTML |
339
|
|
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"> |
340
|
|
|
<html> |
341
|
|
|
<head> |
342
|
|
|
<title>Example</title> |
343
|
|
|
<link rel="stylesheet" href="external.css" media="print"> |
344
|
|
|
</head> |
345
|
|
|
<body> |
346
|
|
|
<h1>An example title</h1> |
347
|
|
|
<p>Paragraph <strong style="font-weight: bold">1</strong></p> |
348
|
|
|
</body> |
349
|
|
|
</html> |
350
|
|
|
|
351
|
|
|
HTML; |
352
|
|
|
$this->assertEquals($expected, $htmldoc->getHTML()); |
353
|
|
|
} |
354
|
|
|
} |
355
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.