Skip to content

Commit 3f89502

Browse files
committed
Merge pull request MyIntervals#20 from ju1ius/shorthands
bugfixes and tests
2 parents 6b8f33c + 804ed93 commit 3f89502

File tree

2 files changed

+249
-31
lines changed

2 files changed

+249
-31
lines changed

lib/CSSRuleSet.php

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -419,22 +419,22 @@ public function createBackgroundShorthand()
419419
'background-position', 'background-attachment'
420420
);
421421
$aRules = $this->getRules();
422-
$oNewRule = new CSSRule('background');
423-
foreach($aProperties as $sProperty)
424-
{
422+
$aNewValues = array();
423+
foreach($aProperties as $sProperty) {
425424
if(!isset($aRules[$sProperty])) continue;
426425
$oRule = $aRules[$sProperty];
427-
if(!$oRule->getIsImportant())
428-
{
429-
foreach($aRules[$sProperty]->getValues() as $aValues)
430-
{
431-
$oNewRule->addValue($aValues);
426+
if(!$oRule->getIsImportant()) {
427+
foreach($aRules[$sProperty]->getValues() as $aValues) {
428+
$aNewValues[] = $aValues;
432429
}
433430
$this->removeRule($sProperty);
434431
}
435432
}
436-
if(count($oNewRule->getValues()) > 0)
437-
{
433+
if(count($aNewValues)) {
434+
$oNewRule = new CSSRule('background');
435+
foreach ($aNewValues as $mValue) {
436+
$oNewRule->addValue($mValue);
437+
}
438438
$this->addRule($oNewRule);
439439
}
440440
}
@@ -445,42 +445,37 @@ public function createBackgroundShorthand()
445445
*
446446
* TODO: this is extremely similar to createBackgroundShorthand and should be combined
447447
**/
448-
public function createBorderShorthand()
449-
{
448+
public function createBorderShorthand() {
450449
$aBorderRules = array(
451450
'border-width', 'border-style', 'border-color'
452451
);
453-
$oNewRule = new CSSRule('border');
454452
$aRules = $this->getRules();
455-
foreach ($aBorderRules as $sBorderRule)
456-
{
453+
$aNewValues = array();
454+
foreach ($aBorderRules as $sBorderRule) {
457455
if(!isset($aRules[$sBorderRule])) continue;
458-
459456
$oRule = $aRules[$sBorderRule];
460-
if(!$oRule->getIsImportant())
461-
{
457+
if(!$oRule->getIsImportant()) {
462458
// Can't merge if multiple values !
463459
if(count($oRule->getValues()) > 1) continue;
464-
foreach($oRule->getValues() as $aValues)
465-
{
460+
foreach($oRule->getValues() as $aValues) {
466461
$mValue = $aValues[0];
467-
if($mValue instanceof CSSValue)
468-
{
462+
if($mValue instanceof CSSValue) {
469463
$mNewValue = clone $mValue;
470-
$oNewRule->addValue(array($mNewValue));
464+
$aNewValues[] = $mNewValue;
471465
}
472-
else
473-
{
474-
$oNewRule->addValue(array($mValue));
466+
else {
467+
$aNewValues[] = $mValue;
475468
}
476469
}
477470
}
478-
}
479-
if(count($oNewRule->getValues()))
480-
{
471+
}
472+
if(count($aNewValues)) {
473+
$oNewRule = new CSSRule('border');
474+
foreach($aNewValues as $mNewValue) {
475+
$oNewRule->addValue(array($mNewValue));
476+
}
481477
$this->addRule($oNewRule);
482-
foreach ($aBorderRules as $sRuleName)
483-
{
478+
foreach($aBorderRules as $sRuleName) {
484479
$this->removeRule($sRuleName);
485480
}
486481
}

tests/CSSDeclarationBlockTest.php

Lines changed: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
1+
<?php
2+
require_once __DIR__.'/../CSSParser.php';
3+
/**
4+
*
5+
*/
6+
class CSSDeclarationBlockTest extends PHPUnit_Framework_TestCase
7+
{
8+
/**
9+
* @dataProvider expandBorderShorthandProvider
10+
**/
11+
public function testExpandBorderShorthand($sCss, $sExpected)
12+
{
13+
$oParser = new CSSParser($sCss);
14+
$oDoc = $oParser->parse();
15+
foreach($oDoc->getAllDeclarationBlocks() as $oDeclaration)
16+
{
17+
$oDeclaration->expandBorderShorthand();
18+
}
19+
$this->assertEquals((string)$oDoc, $sExpected);
20+
}
21+
public function expandBorderShorthandProvider()
22+
{
23+
return array(
24+
array('body{ border: 2px solid rgb(0,0,0) }', 'body {border-width: 2px;border-style: solid;border-color: rgb(0,0,0);}'),
25+
array('body{ border: none }', 'body {border-style: none;}'),
26+
array('body{ border: 2px }', 'body {border-width: 2px;}'),
27+
array('body{ border: rgb(255,0,0) }', 'body {border-color: rgb(255,0,0);}'),
28+
array('body{ border: 1em solid }', 'body {border-width: 1em;border-style: solid;}'),
29+
array('body{ margin: 1em; }', 'body {margin: 1em;}')
30+
);
31+
}
32+
33+
/**
34+
* @dataProvider expandFontShorthandProvider
35+
**/
36+
public function testExpandFontShorthand($sCss, $sExpected)
37+
{
38+
$oParser = new CSSParser($sCss);
39+
$oDoc = $oParser->parse();
40+
foreach($oDoc->getAllDeclarationBlocks() as $oDeclaration)
41+
{
42+
$oDeclaration->expandFontShorthand();
43+
}
44+
$this->assertEquals((string)$oDoc, $sExpected);
45+
}
46+
public function expandFontShorthandProvider()
47+
{
48+
return array(
49+
array(
50+
'body{ margin: 1em; }',
51+
'body {margin: 1em;}'
52+
),
53+
array(
54+
'body {font: 12px serif;}',
55+
'body {font-style: normal;font-variant: normal;font-weight: normal;font-size: 12px;line-height: normal;font-family: serif;}'
56+
),
57+
array(
58+
'body {font: italic 12px serif;}',
59+
'body {font-style: italic;font-variant: normal;font-weight: normal;font-size: 12px;line-height: normal;font-family: serif;}'
60+
),
61+
array(
62+
'body {font: italic bold 12px serif;}',
63+
'body {font-style: italic;font-variant: normal;font-weight: bold;font-size: 12px;line-height: normal;font-family: serif;}'
64+
),
65+
array(
66+
'body {font: italic bold 12px/1.6 serif;}',
67+
'body {font-style: italic;font-variant: normal;font-weight: bold;font-size: 12px;line-height: 1.6;font-family: serif;}'
68+
),
69+
array(
70+
'body {font: italic small-caps bold 12px/1.6 serif;}',
71+
'body {font-style: italic;font-variant: small-caps;font-weight: bold;font-size: 12px;line-height: 1.6;font-family: serif;}'
72+
),
73+
);
74+
}
75+
76+
/**
77+
* @dataProvider expandBackgroundShorthandProvider
78+
**/
79+
public function testExpandBackgroundShorthand($sCss, $sExpected)
80+
{
81+
$oParser = new CSSParser($sCss);
82+
$oDoc = $oParser->parse();
83+
foreach($oDoc->getAllDeclarationBlocks() as $oDeclaration)
84+
{
85+
$oDeclaration->expandBackgroundShorthand();
86+
}
87+
$this->assertEquals((string)$oDoc, $sExpected);
88+
}
89+
public function expandBackgroundShorthandProvider()
90+
{
91+
return array(
92+
array('body {border: 1px;}', 'body {border: 1px;}'),
93+
array('body {background: rgb(255,0,0);}','body {background-color: rgb(255,0,0);background-image: none;background-repeat: repeat;background-attachment: scroll;background-position: 0% 0%;}'),
94+
array('body {background: rgb(255,0,0) url("foobar.png");}','body {background-color: rgb(255,0,0);background-image: url("foobar.png");background-repeat: repeat;background-attachment: scroll;background-position: 0% 0%;}'),
95+
array('body {background: rgb(255,0,0) url("foobar.png") no-repeat;}','body {background-color: rgb(255,0,0);background-image: url("foobar.png");background-repeat: no-repeat;background-attachment: scroll;background-position: 0% 0%;}'),
96+
array('body {background: rgb(255,0,0) url("foobar.png") no-repeat center;}','body {background-color: rgb(255,0,0);background-image: url("foobar.png");background-repeat: no-repeat;background-attachment: scroll;background-position: center center;}'),
97+
array('body {background: rgb(255,0,0) url("foobar.png") no-repeat top left;}','body {background-color: rgb(255,0,0);background-image: url("foobar.png");background-repeat: no-repeat;background-attachment: scroll;background-position: top left;}'),
98+
);
99+
}
100+
101+
/**
102+
* @dataProvider expandDimensionsShorthandProvider
103+
**/
104+
public function testExpandDimensionsShorthand($sCss, $sExpected)
105+
{
106+
$oParser = new CSSParser($sCss);
107+
$oDoc = $oParser->parse();
108+
foreach($oDoc->getAllDeclarationBlocks() as $oDeclaration)
109+
{
110+
$oDeclaration->expandDimensionsShorthand();
111+
}
112+
$this->assertEquals((string)$oDoc, $sExpected);
113+
}
114+
public function expandDimensionsShorthandProvider()
115+
{
116+
return array(
117+
array('body {border: 1px;}', 'body {border: 1px;}'),
118+
array('body {margin-top: 1px;}', 'body {margin-top: 1px;}'),
119+
array('body {margin: 1em;}','body {margin-top: 1em;margin-right: 1em;margin-bottom: 1em;margin-left: 1em;}'),
120+
array('body {margin: 1em 2em;}','body {margin-top: 1em;margin-right: 2em;margin-bottom: 1em;margin-left: 2em;}'),
121+
array('body {margin: 1em 2em 3em;}','body {margin-top: 1em;margin-right: 2em;margin-bottom: 3em;margin-left: 2em;}'),
122+
);
123+
}
124+
125+
/**
126+
* @dataProvider createBorderShorthandProvider
127+
**/
128+
public function testCreateBorderShorthand($sCss, $sExpected)
129+
{
130+
$oParser = new CSSParser($sCss);
131+
$oDoc = $oParser->parse();
132+
foreach($oDoc->getAllDeclarationBlocks() as $oDeclaration)
133+
{
134+
$oDeclaration->createBorderShorthand();
135+
}
136+
$this->assertEquals((string)$oDoc, $sExpected);
137+
}
138+
public function createBorderShorthandProvider()
139+
{
140+
return array(
141+
array('body {border-width: 2px;border-style: solid;border-color: rgb(0,0,0);}', 'body {border: 2px solid rgb(0,0,0);}'),
142+
array('body {border-style: none;}', 'body {border: none;}'),
143+
array('body {border-width: 1em;border-style: solid;}', 'body {border: 1em solid;}'),
144+
array('body {margin: 1em;}', 'body {margin: 1em;}')
145+
);
146+
}
147+
148+
/**
149+
* @dataProvider createFontShorthandProvider
150+
**/
151+
public function testCreateFontShorthand($sCss, $sExpected)
152+
{
153+
$oParser = new CSSParser($sCss);
154+
$oDoc = $oParser->parse();
155+
foreach($oDoc->getAllDeclarationBlocks() as $oDeclaration)
156+
{
157+
$oDeclaration->createFontShorthand();
158+
}
159+
$this->assertEquals((string)$oDoc, $sExpected);
160+
}
161+
public function createFontShorthandProvider()
162+
{
163+
return array(
164+
array('body {font-size: 12px; font-family: serif}', 'body {font: 12px serif;}'),
165+
array('body {font-size: 12px; font-family: serif; font-style: italic;}', 'body {font: italic 12px serif;}'),
166+
array('body {font-size: 12px; font-family: serif; font-style: italic; font-weight: bold;}', 'body {font: italic bold 12px serif;}'),
167+
array('body {font-size: 12px; font-family: serif; font-style: italic; font-weight: bold; line-height: 1.6;}', 'body {font: italic bold 12px/1.6 serif;}'),
168+
array('body {font-size: 12px; font-family: serif; font-style: italic; font-weight: bold; line-height: 1.6; font-variant: small-caps;}', 'body {font: italic small-caps bold 12px/1.6 serif;}'),
169+
array('body {margin: 1em;}', 'body {margin: 1em;}')
170+
);
171+
}
172+
173+
/**
174+
* @dataProvider createDimensionsShorthandProvider
175+
**/
176+
public function testCreateDimensionsShorthand($sCss, $sExpected)
177+
{
178+
$oParser = new CSSParser($sCss);
179+
$oDoc = $oParser->parse();
180+
foreach($oDoc->getAllDeclarationBlocks() as $oDeclaration)
181+
{
182+
$oDeclaration->createDimensionsShorthand();
183+
}
184+
$this->assertEquals((string)$oDoc, $sExpected);
185+
}
186+
public function createDimensionsShorthandProvider()
187+
{
188+
return array(
189+
array('body {border: 1px;}', 'body {border: 1px;}'),
190+
array('body {margin-top: 1px;}', 'body {margin-top: 1px;}'),
191+
array('body {margin-top: 1em; margin-right: 1em; margin-bottom: 1em; margin-left: 1em;}','body {margin: 1em;}'),
192+
array('body {margin-top: 1em; margin-right: 2em; margin-bottom: 1em; margin-left: 2em;}','body {margin: 1em 2em;}'),
193+
array('body {margin-top: 1em; margin-right: 2em; margin-bottom: 3em; margin-left: 2em;}','body {margin: 1em 2em 3em;}'),
194+
);
195+
}
196+
197+
/**
198+
* @dataProvider createBackgroundShorthandProvider
199+
**/
200+
public function testCreateBackgroundShorthand($sCss, $sExpected)
201+
{
202+
$oParser = new CSSParser($sCss);
203+
$oDoc = $oParser->parse();
204+
foreach($oDoc->getAllDeclarationBlocks() as $oDeclaration)
205+
{
206+
$oDeclaration->createBackgroundShorthand();
207+
}
208+
$this->assertEquals((string)$oDoc, $sExpected);
209+
}
210+
public function createBackgroundShorthandProvider()
211+
{
212+
return array(
213+
array('body {border: 1px;}', 'body {border: 1px;}'),
214+
array('body {background-color: rgb(255,0,0);}', 'body {background: rgb(255,0,0);}'),
215+
array('body {background-color: rgb(255,0,0);background-image: url(foobar.png);}', 'body {background: rgb(255,0,0) url("foobar.png");}'),
216+
array('body {background-color: rgb(255,0,0);background-image: url(foobar.png);background-repeat: no-repeat;}', 'body {background: rgb(255,0,0) url("foobar.png") no-repeat;}'),
217+
array('body {background-color: rgb(255,0,0);background-image: url(foobar.png);background-repeat: no-repeat;}', 'body {background: rgb(255,0,0) url("foobar.png") no-repeat;}'),
218+
array('body {background-color: rgb(255,0,0);background-image: url(foobar.png);background-repeat: no-repeat;background-position: center;}', 'body {background: rgb(255,0,0) url("foobar.png") no-repeat center;}'),
219+
array('body {background-color: rgb(255,0,0);background-image: url(foobar.png);background-repeat: no-repeat;background-position: top left;}', 'body {background: rgb(255,0,0) url("foobar.png") no-repeat top left;}'),
220+
);
221+
}
222+
223+
}

0 commit comments

Comments
 (0)