|
| 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