Skip to content

Commit ed038d7

Browse files
committed
modified unit tests for pull requests MyIntervals#33 and MyIntervals#34
1 parent 64b7582 commit ed038d7

File tree

4 files changed

+90
-27
lines changed

4 files changed

+90
-27
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,14 +353,14 @@ To output the entire CSS document into a variable, just use `->__toString()`:
353353
* Options for output (compact, verbose, etc.)
354354
* Support for @namespace
355355
* Named color support (using `CSSColor` instead of an anonymous string literal)
356-
* Allow for function-like property values other than hsl(), rgb(), rgba(), and url() (like -moz-linear-gradient(), for example).
357356
* Test suite
358357
* Adopt lenient parsing rules
359358

360359
## Contributors/Thanks to
361360

362361
* [ju1ius](https://github.com/ju1ius) for the specificity parsing code and the ability to expand/compact shorthand properties.
363362
* [GaryJones](https://github.com/GaryJones) for lots of input and [http://css-specificity.info/](http://css-specificity.info/).
363+
* [docteurklein](https://github.com/docteurklein) for output formatting and CSSList->remove() inspiration.
364364

365365
## License
366366

lib/CSSList.php

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,41 @@ public function append($oItem) {
1515
$this->aContents[] = $oItem;
1616
}
1717

18-
public function remove(CSSDeclarationBlock $item) {
19-
foreach ($this->aContents as $key => $oItem) {
20-
if($oItem->getSelectors() === $item->getSelectors()) {
21-
unset($this->aContents[$key]);
22-
}
23-
}
24-
}
18+
/**
19+
* Removes an item from the CSS list.
20+
* @param CSSRuleSet|CSSImport|CSSCharset|CSSList $oItemToRemove May be a CSSRuleSet (most likely a CSSDeclarationBlock), a CSSImport, a CSSCharset or another CSSList (most likely a CSSMediaQuery)
21+
*/
22+
public function remove($oItemToRemove) {
23+
$iKey = array_search($oItemToRemove, $this->aContents, true);
24+
if($iKey !== false) {
25+
unset($this->aContents[$iKey]);
26+
}
27+
}
28+
29+
public function removeDeclarationBlockBySelector($mSelector, $bRemoveAll = false) {
30+
if($mSelector instanceof CSSDeclarationBlock) {
31+
$mSelector = $mSelector->getSelectors();
32+
}
33+
if(!is_array($mSelector)) {
34+
$mSelector = explode(',', $mSelector);
35+
}
36+
foreach($mSelector as $iKey => &$mSel) {
37+
if(!($mSel instanceof CSSSelector)) {
38+
$mSel = new CSSSelector($mSel);
39+
}
40+
}
41+
foreach($this->aContents as $iKey => $mItem) {
42+
if(!($mItem instanceof CSSDeclarationBlock)) {
43+
continue;
44+
}
45+
if($mItem->getSelectors() == $mSelector) {
46+
unset($this->aContents[$iKey]);
47+
if(!$bRemoveAll) {
48+
return;
49+
}
50+
}
51+
}
52+
}
2553

2654
public function __toString() {
2755
$sResult = '';

tests/CSSDeclarationBlockTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public function testExpandBorderShorthand($sCss, $sExpected)
1616
{
1717
$oDeclaration->expandBorderShorthand();
1818
}
19-
$this->assertEquals((string)$oDoc, $sExpected);
19+
$this->assertSame(trim((string)$oDoc), $sExpected);
2020
}
2121
public function expandBorderShorthandProvider()
2222
{
@@ -41,7 +41,7 @@ public function testExpandFontShorthand($sCss, $sExpected)
4141
{
4242
$oDeclaration->expandFontShorthand();
4343
}
44-
$this->assertEquals((string)$oDoc, $sExpected);
44+
$this->assertSame(trim((string)$oDoc), $sExpected);
4545
}
4646
public function expandFontShorthandProvider()
4747
{
@@ -84,7 +84,7 @@ public function testExpandBackgroundShorthand($sCss, $sExpected)
8484
{
8585
$oDeclaration->expandBackgroundShorthand();
8686
}
87-
$this->assertEquals((string)$oDoc, $sExpected);
87+
$this->assertSame(trim((string)$oDoc), $sExpected);
8888
}
8989
public function expandBackgroundShorthandProvider()
9090
{
@@ -109,7 +109,7 @@ public function testExpandDimensionsShorthand($sCss, $sExpected)
109109
{
110110
$oDeclaration->expandDimensionsShorthand();
111111
}
112-
$this->assertEquals((string)$oDoc, $sExpected);
112+
$this->assertSame(trim((string)$oDoc), $sExpected);
113113
}
114114
public function expandDimensionsShorthandProvider()
115115
{
@@ -133,7 +133,7 @@ public function testCreateBorderShorthand($sCss, $sExpected)
133133
{
134134
$oDeclaration->createBorderShorthand();
135135
}
136-
$this->assertEquals((string)$oDoc, $sExpected);
136+
$this->assertSame(trim((string)$oDoc), $sExpected);
137137
}
138138
public function createBorderShorthandProvider()
139139
{
@@ -156,7 +156,7 @@ public function testCreateFontShorthand($sCss, $sExpected)
156156
{
157157
$oDeclaration->createFontShorthand();
158158
}
159-
$this->assertEquals((string)$oDoc, $sExpected);
159+
$this->assertSame(trim((string)$oDoc), $sExpected);
160160
}
161161
public function createFontShorthandProvider()
162162
{
@@ -181,7 +181,7 @@ public function testCreateDimensionsShorthand($sCss, $sExpected)
181181
{
182182
$oDeclaration->createDimensionsShorthand();
183183
}
184-
$this->assertEquals((string)$oDoc, $sExpected);
184+
$this->assertSame(trim((string)$oDoc), $sExpected);
185185
}
186186
public function createDimensionsShorthandProvider()
187187
{
@@ -205,7 +205,7 @@ public function testCreateBackgroundShorthand($sCss, $sExpected)
205205
{
206206
$oDeclaration->createBackgroundShorthand();
207207
}
208-
$this->assertEquals((string)$oDoc, $sExpected);
208+
$this->assertSame(trim((string)$oDoc), $sExpected);
209209
}
210210
public function createBackgroundShorthandProvider()
211211
{

tests/CSSParserTests.php

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -146,26 +146,28 @@ function testSpecificity() {
146146

147147
function testManipulation() {
148148
$oDoc = $this->parsedStructureForFile('atrules');
149-
$this->assertSame('@charset "utf-8";@font-face {font-family: "CrassRoots";src: url("../media/cr.ttf");}html, body {font-size: 1.6em;}', $oDoc->__toString());
149+
$this->assertSame('@charset "utf-8";@font-face {font-family: "CrassRoots";src: url("../media/cr.ttf");}html, body {font-size: 1.6em;}'."\n", $oDoc->__toString());
150150
foreach($oDoc->getAllDeclarationBlocks() as $oBlock) {
151151
foreach($oBlock->getSelectors() as $oSelector) {
152152
//Loop over all selector parts (the comma-separated strings in a selector) and prepend the id
153153
$oSelector->setSelector('#my_id '.$oSelector->getSelector());
154154
}
155155
}
156-
$this->assertSame('@charset "utf-8";@font-face {font-family: "CrassRoots";src: url("../media/cr.ttf");}#my_id html, #my_id body {font-size: 1.6em;}', $oDoc->__toString());
156+
$this->assertSame('@charset "utf-8";@font-face {font-family: "CrassRoots";src: url("../media/cr.ttf");}#my_id html, #my_id body {font-size: 1.6em;}'."\n", $oDoc->__toString());
157157

158158
$oDoc = $this->parsedStructureForFile('values');
159-
$this->assertSame('#header {margin: 10px 2em 1cm 2%;font-family: Verdana,Helvetica,"Gill Sans",sans-serif;font-size: 10px;color: red !important;}body {color: green;font: 75% "Lucida Grande","Trebuchet MS",Verdana,sans-serif;}', $oDoc->__toString());
159+
$this->assertSame('#header {margin: 10px 2em 1cm 2%;font-family: Verdana,Helvetica,"Gill Sans",sans-serif;font-size: 10px;color: red !important;}
160+
body {color: green;font: 75% "Lucida Grande","Trebuchet MS",Verdana,sans-serif;}'."\n", $oDoc->__toString());
160161
foreach($oDoc->getAllRuleSets() as $oRuleSet) {
161162
$oRuleSet->removeRule('font-');
162163
}
163-
$this->assertSame('#header {margin: 10px 2em 1cm 2%;color: red !important;}body {color: green;}', $oDoc->__toString());
164+
$this->assertSame('#header {margin: 10px 2em 1cm 2%;color: red !important;}
165+
body {color: green;}'."\n", $oDoc->__toString());
164166
}
165167

166168
function testSlashedValues() {
167169
$oDoc = $this->parsedStructureForFile('slashed');
168-
$this->assertSame('.test {font: 12px/1.5 Verdana,Arial,sans-serif;border-radius: 5px 10px 5px 10px/10px 5px 10px 5px;}', $oDoc->__toString());
170+
$this->assertSame('.test {font: 12px/1.5 Verdana,Arial,sans-serif;border-radius: 5px 10px 5px 10px/10px 5px 10px 5px;}'."\n", $oDoc->__toString());
169171
foreach($oDoc->getAllValues(null) as $mValue) {
170172
if($mValue instanceof CSSSize && $mValue->isSize() && !$mValue->isRelative()) {
171173
$mValue->setSize($mValue->getSize()*3);
@@ -191,12 +193,16 @@ function testSlashedValues() {
191193
$this->assertEquals(' ', $oSpaceList1->getListSeparator());
192194
$this->assertEquals(' ', $oSpaceList2->getListSeparator());
193195
}
194-
$this->assertSame('.test {font: 36px/1.5 Verdana,Arial,sans-serif;border-radius: 15px 30px 15px 30px/30px 15px 30px 15px;}', $oDoc->__toString());
196+
$this->assertSame('.test {font: 36px/1.5 Verdana,Arial,sans-serif;border-radius: 15px 30px 15px 30px/30px 15px 30px 15px;}'."\n", $oDoc->__toString());
195197
}
196198

197199
function testFunctionSyntax() {
198200
$oDoc = $this->parsedStructureForFile('functions');
199-
$sExpected = 'div.main {background-image: linear-gradient(rgb(0,0,0),rgb(255,255,255));}.collapser::before, .collapser::-moz-before, .collapser::-webkit-before {content: "»";font-size: 1.2em;margin-right: 0.2em;-moz-transition-property: -moz-transform;-moz-transition-duration: 0.2s;-moz-transform-origin: center 60%;}.collapser.expanded::before, .collapser.expanded::-moz-before, .collapser.expanded::-webkit-before {-moz-transform: rotate(90deg);}.collapser + * {height: 0;overflow: hidden;-moz-transition-property: height;-moz-transition-duration: 0.3s;}.collapser.expanded + * {height: auto;}';
201+
$sExpected = 'div.main {background-image: linear-gradient(rgb(0,0,0),rgb(255,255,255));}
202+
.collapser::before, .collapser::-moz-before, .collapser::-webkit-before {content: "»";font-size: 1.2em;margin-right: 0.2em;-moz-transition-property: -moz-transform;-moz-transition-duration: 0.2s;-moz-transform-origin: center 60%;}
203+
.collapser.expanded::before, .collapser.expanded::-moz-before, .collapser.expanded::-webkit-before {-moz-transform: rotate(90deg);}
204+
.collapser + * {height: 0;overflow: hidden;-moz-transition-property: height;-moz-transition-duration: 0.3s;}
205+
.collapser.expanded + * {height: auto;}'."\n";
200206
$this->assertSame($sExpected, $oDoc->__toString());
201207

202208
foreach($oDoc->getAllValues(null, true) as $mValue) {
@@ -218,25 +224,54 @@ function testFunctionSyntax() {
218224

219225
function testExpandShorthands() {
220226
$oDoc = $this->parsedStructureForFile('expand-shorthands');
221-
$sExpected = 'body {font: italic 500 14px/1.618 "Trebuchet MS",Georgia,serif;border: 2px solid rgb(255,0,255);background: rgb(204,204,204) url("/images/foo.png") no-repeat left top;margin: 1em !important;padding: 2px 6px 3px;}';
227+
$sExpected = 'body {font: italic 500 14px/1.618 "Trebuchet MS",Georgia,serif;border: 2px solid rgb(255,0,255);background: rgb(204,204,204) url("/images/foo.png") no-repeat left top;margin: 1em !important;padding: 2px 6px 3px;}'."\n";
222228
$this->assertSame($sExpected, $oDoc->__toString());
223229
$oDoc->expandShorthands();
224-
$sExpected = 'body {margin-top: 1em !important;margin-right: 1em !important;margin-bottom: 1em !important;margin-left: 1em !important;padding-top: 2px;padding-right: 6px;padding-bottom: 3px;padding-left: 6px;border-top-color: rgb(255,0,255);border-right-color: rgb(255,0,255);border-bottom-color: rgb(255,0,255);border-left-color: rgb(255,0,255);border-top-style: solid;border-right-style: solid;border-bottom-style: solid;border-left-style: solid;border-top-width: 2px;border-right-width: 2px;border-bottom-width: 2px;border-left-width: 2px;font-style: italic;font-variant: normal;font-weight: 500;font-size: 14px;line-height: 1.618;font-family: "Trebuchet MS",Georgia,serif;background-color: rgb(204,204,204);background-image: url("/images/foo.png");background-repeat: no-repeat;background-attachment: scroll;background-position: left top;}';
230+
$sExpected = 'body {margin-top: 1em !important;margin-right: 1em !important;margin-bottom: 1em !important;margin-left: 1em !important;padding-top: 2px;padding-right: 6px;padding-bottom: 3px;padding-left: 6px;border-top-color: rgb(255,0,255);border-right-color: rgb(255,0,255);border-bottom-color: rgb(255,0,255);border-left-color: rgb(255,0,255);border-top-style: solid;border-right-style: solid;border-bottom-style: solid;border-left-style: solid;border-top-width: 2px;border-right-width: 2px;border-bottom-width: 2px;border-left-width: 2px;font-style: italic;font-variant: normal;font-weight: 500;font-size: 14px;line-height: 1.618;font-family: "Trebuchet MS",Georgia,serif;background-color: rgb(204,204,204);background-image: url("/images/foo.png");background-repeat: no-repeat;background-attachment: scroll;background-position: left top;}'."\n";
225231
$this->assertSame($sExpected, $oDoc->__toString());
226232
}
227233

228234
function testCreateShorthands() {
229235
$oDoc = $this->parsedStructureForFile('create-shorthands');
230-
$sExpected = 'body {font-size: 2em;font-family: Helvetica,Arial,sans-serif;font-weight: bold;border-width: 2px;border-color: rgb(153,153,153);border-style: dotted;background-color: rgb(255,255,255);background-image: url("foobar.png");background-repeat: repeat-y;margin-top: 2px;margin-right: 3px;margin-bottom: 4px;margin-left: 5px;}';
236+
$sExpected = 'body {font-size: 2em;font-family: Helvetica,Arial,sans-serif;font-weight: bold;border-width: 2px;border-color: rgb(153,153,153);border-style: dotted;background-color: rgb(255,255,255);background-image: url("foobar.png");background-repeat: repeat-y;margin-top: 2px;margin-right: 3px;margin-bottom: 4px;margin-left: 5px;}'."\n";
231237
$this->assertSame($sExpected, $oDoc->__toString());
232238
$oDoc->createShorthands();
233-
$sExpected = 'body {background: rgb(255,255,255) url("foobar.png") repeat-y;margin: 2px 5px 4px 3px;border: 2px dotted rgb(153,153,153);font: bold 2em Helvetica,Arial,sans-serif;}';
239+
$sExpected = 'body {background: rgb(255,255,255) url("foobar.png") repeat-y;margin: 2px 5px 4px 3px;border: 2px dotted rgb(153,153,153);font: bold 2em Helvetica,Arial,sans-serif;}'."\n";
234240
$this->assertSame($sExpected, $oDoc->__toString());
235241
}
236242

243+
function testListValueRemoval() {
244+
$oDoc = $this->parsedStructureForFile('atrules');
245+
foreach($oDoc->getContents() as $oItem) {
246+
if($oItem instanceof CSSAtRule) {
247+
$oDoc->remove($oItem);
248+
break;
249+
}
250+
}
251+
$this->assertSame('@charset "utf-8";html, body {font-size: 1.6em;}'."\n", $oDoc->__toString());
252+
253+
$oDoc = $this->parsedStructureForFile('nested');
254+
foreach($oDoc->getAllDeclarationBlocks() as $oBlock) {
255+
$oDoc->removeDeclarationBlockBySelector($oBlock, false);
256+
break;
257+
}
258+
$this->assertSame('html {some-other: -test(val1);}
259+
@media screen {html {some: -test(val2);}
260+
}#unrelated {other: yes;}'."\n", $oDoc->__toString());
261+
262+
$oDoc = $this->parsedStructureForFile('nested');
263+
foreach($oDoc->getAllDeclarationBlocks() as $oBlock) {
264+
$oDoc->removeDeclarationBlockBySelector($oBlock, true);
265+
break;
266+
}
267+
$this->assertSame('@media screen {html {some: -test(val2);}
268+
}#unrelated {other: yes;}'."\n", $oDoc->__toString());
269+
}
270+
237271
function parsedStructureForFile($sFileName) {
238272
$sFile = dirname(__FILE__).DIRECTORY_SEPARATOR.'files'.DIRECTORY_SEPARATOR."$sFileName.css";
239273
$oParser = new CSSParser(file_get_contents($sFile));
240274
return $oParser->parse();
241275
}
276+
242277
}

0 commit comments

Comments
 (0)