| 1 |
# PHP CSS Parser |
| 2 |
|
| 3 |
[](https://github.com/MyIntervals/PHP-CSS-Parser/actions/) |
| 4 |
[](https://coveralls.io/github/MyIntervals/PHP-CSS-Parser?branch=main) |
| 5 |
|
| 6 |
A Parser for CSS Files written in PHP. Allows extraction of CSS files into a data structure, manipulation of said structure and output as (optimized) CSS. |
| 7 |
|
| 8 |
## Usage |
| 9 |
|
| 10 |
### Installation using Composer |
| 11 |
|
| 12 |
```bash |
| 13 |
composer require sabberworm/php-css-parser |
| 14 |
``` |
| 15 |
|
| 16 |
### Extraction |
| 17 |
|
| 18 |
To use the CSS Parser, create a new instance. The constructor takes the following form: |
| 19 |
|
| 20 |
```php |
| 21 |
new \Sabberworm\CSS\Parser($css); |
| 22 |
``` |
| 23 |
|
| 24 |
To read a file, for example, you’d do the following: |
| 25 |
|
| 26 |
```php |
| 27 |
$parser = new \Sabberworm\CSS\Parser(file_get_contents('somefile.css')); |
| 28 |
$cssDocument = $parser->parse(); |
| 29 |
``` |
| 30 |
|
| 31 |
The resulting CSS document structure can be manipulated prior to being output. |
| 32 |
|
| 33 |
### Options |
| 34 |
|
| 35 |
#### Charset |
| 36 |
|
| 37 |
The charset option will only be used if the CSS file does not contain an `@charset` declaration. UTF-8 is the default, so you won’t have to create a settings object at all if you don’t intend to change that. |
| 38 |
|
| 39 |
```php |
| 40 |
$settings = \Sabberworm\CSS\Settings::create() |
| 41 |
->withDefaultCharset('windows-1252'); |
| 42 |
$parser = new \Sabberworm\CSS\Parser($css, $settings); |
| 43 |
``` |
| 44 |
|
| 45 |
#### Strict parsing |
| 46 |
|
| 47 |
To have the parser throw an exception when encountering invalid/unknown constructs (as opposed to trying to ignore them and carry on parsing), supply a thusly configured `\Sabberworm\CSS\Settings` object: |
| 48 |
|
| 49 |
```php |
| 50 |
$parser = new \Sabberworm\CSS\Parser( |
| 51 |
file_get_contents('somefile.css'), |
| 52 |
\Sabberworm\CSS\Settings::create()->beStrict() |
| 53 |
); |
| 54 |
``` |
| 55 |
|
| 56 |
Note that this will also disable a workaround for parsing the unquoted variant of the legacy IE-specific `filter` rule. |
| 57 |
|
| 58 |
#### Disable multibyte functions |
| 59 |
|
| 60 |
To achieve faster parsing, you can choose to have PHP-CSS-Parser use regular string functions instead of `mb_*` functions. This should work fine in most cases, even for UTF-8 files, as all the multibyte characters are in string literals. Still it’s not recommended using this with input you have no control over as it’s not thoroughly covered by test cases. |
| 61 |
|
| 62 |
```php |
| 63 |
$settings = \Sabberworm\CSS\Settings::create()->withMultibyteSupport(false); |
| 64 |
$parser = new \Sabberworm\CSS\Parser($css, $settings); |
| 65 |
``` |
| 66 |
|
| 67 |
### Manipulation |
| 68 |
|
| 69 |
The resulting data structure consists mainly of five basic types: `CSSList`, `RuleSet`, `Rule`, `Selector` and `Value`. There are two additional types used: `Import` and `Charset`, which you won’t use often. |
| 70 |
|
| 71 |
#### CSSList |
| 72 |
|
| 73 |
`CSSList` represents a generic CSS container, most likely containing declaration blocks (rule sets with a selector), but it may also contain at-rules, charset declarations, etc. |
| 74 |
|
| 75 |
To access the items stored in a `CSSList` – like the document you got back when calling `$parser->parse()` –, use `getContents()`, then iterate over that collection and use `instanceof` to check whether you’re dealing with another `CSSList`, a `RuleSet`, a `Import` or a `Charset`. |
| 76 |
|
| 77 |
To append a new item (selector, media query, etc.) to an existing `CSSList`, construct it using the constructor for this class and use the `append($oItem)` method. |
| 78 |
|
| 79 |
#### RuleSet |
| 80 |
|
| 81 |
`RuleSet` is a container for individual rules. The most common form of a rule set is one constrained by a selector. The following concrete subtypes exist: |
| 82 |
|
| 83 |
* `AtRuleSet` – for generic at-rules for generic at-rules which are not covered by specific classes, i.e., not `@import`, `@charset` or `@media`. A common example for this is `@font-face`. |
| 84 |
* `DeclarationBlock` – a `RuleSet` constrained by a `Selector`; contains an array of selector objects (comma-separated in the CSS) as well as the rules to be applied to the matching elements. |
| 85 |
|
| 86 |
Note: A `CSSList` can contain other `CSSList`s (and `Import`s as well as a `Charset`), while a `RuleSet` can only contain `Rule`s. |
| 87 |
|
| 88 |
If you want to manipulate a `RuleSet`, use the methods `addRule(Rule $rule)`, `getRules()` and `removeRule($rule)` (which accepts either a `Rule` or a rule name; optionally suffixed by a dash to remove all related rules). |
| 89 |
|
| 90 |
#### Rule |
| 91 |
|
| 92 |
`Rule`s just have a string key (the rule) and a `Value`. |
| 93 |
|
| 94 |
#### Value |
| 95 |
|
| 96 |
`Value` is an abstract class that only defines the `render` method. The concrete subclasses for atomic value types are: |
| 97 |
|
| 98 |
* `Size` – consists of a numeric `size` value and a unit. |
| 99 |
* `Color` – colors can be input in the form #rrggbb, #rgb or schema(val1, val2, …) but are always stored as an array of ('s' => val1, 'c' => val2, 'h' => val3, …) and output in the second form. |
| 100 |
* `CSSString` – this is just a wrapper for quoted strings to distinguish them from keywords; always output with double quotes. |
| 101 |
* `URL` – URLs in CSS; always output in `URL("")` notation. |
| 102 |
|
| 103 |
There is another abstract subclass of `Value`, `ValueList`: A `ValueList` represents a lists of `Value`s, separated by some separation character (mostly `,`, whitespace, or `/`). |
| 104 |
|
| 105 |
There are two types of `ValueList`s: |
| 106 |
|
| 107 |
* `RuleValueList` – The default type, used to represent all multivalued rules like `font: bold 12px/3 Helvetica, Verdana, sans-serif;` (where the value would be a whitespace-separated list of the primitive value `bold`, a slash-separated list and a comma-separated list). |
| 108 |
* `CSSFunction` – A special kind of value that also contains a function name and where the values are the function’s arguments. Also handles equals-sign-separated argument lists like `filter: alpha(opacity=90);`. |
| 109 |
|
| 110 |
#### Convenience methods |
| 111 |
|
| 112 |
There are a few convenience methods on `Document` to ease finding, manipulating and deleting rules: |
| 113 |
|
| 114 |
* `getAllDeclarationBlocks()` – does what it says; no matter how deeply nested the selectors are. Aliased as `getAllSelectors()`. |
| 115 |
* `getAllRuleSets()` – does what it says; no matter how deeply nested the rule sets are. |
| 116 |
* `getAllValues()` – finds all `Value` objects inside `Rule`s. |
| 117 |
|
| 118 |
## To-Do |
| 119 |
|
| 120 |
* More convenience methods (like `selectorsWithElement($sId/Class/TagName)`, `attributesOfType($type)`, `removeAttributesOfType($type)`) |
| 121 |
* Real multibyte support. Currently, only multibyte charsets whose first 255 code points take up only one byte and are identical with ASCII are supported (yes, UTF-8 fits this description). |
| 122 |
* Named color support (using `Color` instead of an anonymous string literal) |
| 123 |
|
| 124 |
## Use cases |
| 125 |
|
| 126 |
### Use `Parser` to prepend an ID to all selectors |
| 127 |
|
| 128 |
```php |
| 129 |
$myId = "#my_id"; |
| 130 |
$parser = new \Sabberworm\CSS\Parser($css); |
| 131 |
$cssDocument = $parser->parse(); |
| 132 |
foreach ($cssDocument->getAllDeclarationBlocks() as $block) { |
| 133 |
foreach ($block->getSelectors() as $selector) { |
| 134 |
// Loop over all selector parts (the comma-separated strings in a |
| 135 |
// selector) and prepend the ID. |
| 136 |
$selector->setSelector($myId.' '.$selector->getSelector()); |
| 137 |
} |
| 138 |
} |
| 139 |
``` |
| 140 |
|
| 141 |
### Shrink all absolute sizes to half |
| 142 |
|
| 143 |
```php |
| 144 |
$parser = new \Sabberworm\CSS\Parser($css); |
| 145 |
$cssDocument = $parser->parse(); |
| 146 |
foreach ($cssDocument->getAllValues() as $value) { |
| 147 |
if ($value instanceof CSSSize && !$value->isRelative()) { |
| 148 |
$value->setSize($value->getSize() / 2); |
| 149 |
} |
| 150 |
} |
| 151 |
``` |
| 152 |
|
| 153 |
### Remove unwanted rules |
| 154 |
|
| 155 |
```php |
| 156 |
$parser = new \Sabberworm\CSS\Parser($css); |
| 157 |
$cssDocument = $parser->parse(); |
| 158 |
foreach($cssDocument->getAllRuleSets() as $oRuleSet) { |
| 159 |
// Note that the added dash will make this remove all rules starting with |
| 160 |
// `font-` (like `font-size`, `font-weight`, etc.) as well as a potential |
| 161 |
// `font` rule. |
| 162 |
$oRuleSet->removeRule('font-'); |
| 163 |
$oRuleSet->removeRule('cursor'); |
| 164 |
} |
| 165 |
``` |
| 166 |
|
| 167 |
### Output |
| 168 |
|
| 169 |
To output the entire CSS document into a variable, just use `->render()`: |
| 170 |
|
| 171 |
```php |
| 172 |
$parser = new \Sabberworm\CSS\Parser(file_get_contents('somefile.css')); |
| 173 |
$cssDocument = $parser->parse(); |
| 174 |
print $cssDocument->render(); |
| 175 |
``` |
| 176 |
|
| 177 |
If you want to format the output, pass an instance of type `\Sabberworm\CSS\OutputFormat`: |
| 178 |
|
| 179 |
```php |
| 180 |
$format = \Sabberworm\CSS\OutputFormat::create() |
| 181 |
->indentWithSpaces(4)->setSpaceBetweenRules("\n"); |
| 182 |
print $cssDocument->render($format); |
| 183 |
``` |
| 184 |
|
| 185 |
Or use one of the predefined formats: |
| 186 |
|
| 187 |
```php |
| 188 |
print $cssDocument->render(Sabberworm\CSS\OutputFormat::createPretty()); |
| 189 |
print $cssDocument->render(Sabberworm\CSS\OutputFormat::createCompact()); |
| 190 |
``` |
| 191 |
|
| 192 |
To see what you can do with output formatting, look at the tests in `tests/OutputFormatTest.php`. |
| 193 |
|
| 194 |
## Examples |
| 195 |
|
| 196 |
### Example 1 (At-Rules) |
| 197 |
|
| 198 |
#### Input |
| 199 |
|
| 200 |
```css |
| 201 |
@charset "utf-8"; |
| 202 |
|
| 203 |
@font-face { |
| 204 |
font-family: "CrassRoots"; |
| 205 |
src: url("../media/cr.ttf"); |
| 206 |
} |
| 207 |
|
| 208 |
html, body { |
| 209 |
font-size: 1.6em; |
| 210 |
} |
| 211 |
|
| 212 |
@keyframes mymove { |
| 213 |
from { top: 0px; } |
| 214 |
to { top: 200px; } |
| 215 |
} |
| 216 |
|
| 217 |
``` |
| 218 |
|
| 219 |
<details> |
| 220 |
<summary><b>Structure (<code>var_dump()</code>)</b></summary> |
| 221 |
|
| 222 |
```php |
| 223 |
class Sabberworm\CSS\CSSList\Document#4 (2) { |
| 224 |
protected $contents => |
| 225 |
array(4) { |
| 226 |
[0] => |
| 227 |
class Sabberworm\CSS\Property\Charset#6 (2) { |
| 228 |
private $charset => |
| 229 |
class Sabberworm\CSS\Value\CSSString#5 (2) { |
| 230 |
private $string => |
| 231 |
string(5) "utf-8" |
| 232 |
protected $lineNumber => |
| 233 |
int(1) |
| 234 |
} |
| 235 |
protected $lineNumber => |
| 236 |
int(1) |
| 237 |
} |
| 238 |
[1] => |
| 239 |
class Sabberworm\CSS\RuleSet\AtRuleSet#7 (4) { |
| 240 |
private $type => |
| 241 |
string(9) "font-face" |
| 242 |
private $arguments => |
| 243 |
string(0) "" |
| 244 |
private $rules => |
| 245 |
array(2) { |
| 246 |
'font-family' => |
| 247 |
array(1) { |
| 248 |
[0] => |
| 249 |
class Sabberworm\CSS\Rule\Rule#8 (4) { |
| 250 |
private $rule => |
| 251 |
string(11) "font-family" |
| 252 |
private $value => |
| 253 |
class Sabberworm\CSS\Value\CSSString#9 (2) { |
| 254 |
private $string => |
| 255 |
string(10) "CrassRoots" |
| 256 |
protected $lineNumber => |
| 257 |
int(4) |
| 258 |
} |
| 259 |
private $isImportant => |
| 260 |
bool(false) |
| 261 |
protected $lineNumber => |
| 262 |
int(4) |
| 263 |
} |
| 264 |
} |
| 265 |
'src' => |
| 266 |
array(1) { |
| 267 |
[0] => |
| 268 |
class Sabberworm\CSS\Rule\Rule#10 (4) { |
| 269 |
private $rule => |
| 270 |
string(3) "src" |
| 271 |
private $value => |
| 272 |
class Sabberworm\CSS\Value\URL#11 (2) { |
| 273 |
private $url => |
| 274 |
class Sabberworm\CSS\Value\CSSString#12 (2) { |
| 275 |
private $string => |
| 276 |
string(15) "../media/cr.ttf" |
| 277 |
protected $lineNumber => |
| 278 |
int(5) |
| 279 |
} |
| 280 |
protected $lineNumber => |
| 281 |
int(5) |
| 282 |
} |
| 283 |
private $isImportant => |
| 284 |
bool(false) |
| 285 |
protected $lineNumber => |
| 286 |
int(5) |
| 287 |
} |
| 288 |
} |
| 289 |
} |
| 290 |
protected $lineNumber => |
| 291 |
int(3) |
| 292 |
} |
| 293 |
[2] => |
| 294 |
class Sabberworm\CSS\RuleSet\DeclarationBlock#13 (3) { |
| 295 |
private $selectors => |
| 296 |
array(2) { |
| 297 |
[0] => |
| 298 |
class Sabberworm\CSS\Property\Selector#14 (2) { |
| 299 |
private $selector => |
| 300 |
string(4) "html" |
| 301 |
private $specificity => |
| 302 |
NULL |
| 303 |
} |
| 304 |
[1] => |
| 305 |
class Sabberworm\CSS\Property\Selector#15 (2) { |
| 306 |
private $selector => |
| 307 |
string(4) "body" |
| 308 |
private $specificity => |
| 309 |
NULL |
| 310 |
} |
| 311 |
} |
| 312 |
private $rules => |
| 313 |
array(1) { |
| 314 |
'font-size' => |
| 315 |
array(1) { |
| 316 |
[0] => |
| 317 |
class Sabberworm\CSS\Rule\Rule#16 (4) { |
| 318 |
private $rule => |
| 319 |
string(9) "font-size" |
| 320 |
private $value => |
| 321 |
class Sabberworm\CSS\Value\Size#17 (4) { |
| 322 |
private $size => |
| 323 |
double(1.6) |
| 324 |
private $unit => |
| 325 |
string(2) "em" |
| 326 |
private $isColorComponent => |
| 327 |
bool(false) |
| 328 |
protected $lineNumber => |
| 329 |
int(9) |
| 330 |
} |
| 331 |
private $isImportant => |
| 332 |
bool(false) |
| 333 |
protected $lineNumber => |
| 334 |
int(9) |
| 335 |
} |
| 336 |
} |
| 337 |
} |
| 338 |
protected $lineNumber => |
| 339 |
int(8) |
| 340 |
} |
| 341 |
[3] => |
| 342 |
class Sabberworm\CSS\CSSList\KeyFrame#18 (4) { |
| 343 |
private $vendorKeyFrame => |
| 344 |
string(9) "keyframes" |
| 345 |
private $animationName => |
| 346 |
string(6) "mymove" |
| 347 |
protected $contents => |
| 348 |
array(2) { |
| 349 |
[0] => |
| 350 |
class Sabberworm\CSS\RuleSet\DeclarationBlock#19 (3) { |
| 351 |
private $selectors => |
| 352 |
array(1) { |
| 353 |
[0] => |
| 354 |
class Sabberworm\CSS\Property\Selector#20 (2) { |
| 355 |
private $selector => |
| 356 |
string(4) "from" |
| 357 |
private $specificity => |
| 358 |
NULL |
| 359 |
} |
| 360 |
} |
| 361 |
private $rules => |
| 362 |
array(1) { |
| 363 |
'top' => |
| 364 |
array(1) { |
| 365 |
[0] => |
| 366 |
class Sabberworm\CSS\Rule\Rule#21 (4) { |
| 367 |
private $rule => |
| 368 |
string(3) "top" |
| 369 |
private $value => |
| 370 |
class Sabberworm\CSS\Value\Size#22 (4) { |
| 371 |
private $size => |
| 372 |
double(0) |
| 373 |
private $unit => |
| 374 |
string(2) "px" |
| 375 |
private $isColorComponent => |
| 376 |
bool(false) |
| 377 |
protected $lineNumber => |
| 378 |
int(13) |
| 379 |
} |
| 380 |
private $isImportant => |
| 381 |
bool(false) |
| 382 |
protected $lineNumber => |
| 383 |
int(13) |
| 384 |
} |
| 385 |
} |
| 386 |
} |
| 387 |
protected $lineNumber => |
| 388 |
int(13) |
| 389 |
} |
| 390 |
[1] => |
| 391 |
class Sabberworm\CSS\RuleSet\DeclarationBlock#23 (3) { |
| 392 |
private $selectors => |
| 393 |
array(1) { |
| 394 |
[0] => |
| 395 |
class Sabberworm\CSS\Property\Selector#24 (2) { |
| 396 |
private $selector => |
| 397 |
string(2) "to" |
| 398 |
private $specificity => |
| 399 |
NULL |
| 400 |
} |
| 401 |
} |
| 402 |
private $rules => |
| 403 |
array(1) { |
| 404 |
'top' => |
| 405 |
array(1) { |
| 406 |
[0] => |
| 407 |
class Sabberworm\CSS\Rule\Rule#25 (4) { |
| 408 |
private $rule => |
| 409 |
string(3) "top" |
| 410 |
private $value => |
| 411 |
class Sabberworm\CSS\Value\Size#26 (4) { |
| 412 |
private $size => |
| 413 |
double(200) |
| 414 |
private $unit => |
| 415 |
string(2) "px" |
| 416 |
private $isColorComponent => |
| 417 |
bool(false) |
| 418 |
protected $lineNumber => |
| 419 |
int(14) |
| 420 |
} |
| 421 |
private $isImportant => |
| 422 |
bool(false) |
| 423 |
protected $lineNumber => |
| 424 |
int(14) |
| 425 |
} |
| 426 |
} |
| 427 |
} |
| 428 |
protected $lineNumber => |
| 429 |
int(14) |
| 430 |
} |
| 431 |
} |
| 432 |
protected $lineNumber => |
| 433 |
int(12) |
| 434 |
} |
| 435 |
} |
| 436 |
protected $lineNumber => |
| 437 |
int(1) |
| 438 |
} |
| 439 |
|
| 440 |
``` |
| 441 |
</details> |
| 442 |
|
| 443 |
#### Output (`render()`) |
| 444 |
|
| 445 |
```css |
| 446 |
@charset "utf-8"; |
| 447 |
@font-face {font-family: "CrassRoots";src: url("../media/cr.ttf");} |
| 448 |
html, body {font-size: 1.6em;} |
| 449 |
@keyframes mymove {from {top: 0px;} to {top: 200px;}} |
| 450 |
``` |
| 451 |
|
| 452 |
### Example 2 (Values) |
| 453 |
|
| 454 |
#### Input |
| 455 |
|
| 456 |
```css |
| 457 |
#header { |
| 458 |
margin: 10px 2em 1cm 2%; |
| 459 |
font-family: Verdana, Helvetica, "Gill Sans", sans-serif; |
| 460 |
color: red !important; |
| 461 |
} |
| 462 |
|
| 463 |
``` |
| 464 |
|
| 465 |
<details> |
| 466 |
<summary><b>Structure (<code>var_dump()</code>)</b></summary> |
| 467 |
|
| 468 |
```php |
| 469 |
class Sabberworm\CSS\CSSList\Document#4 (2) { |
| 470 |
protected $contents => |
| 471 |
array(1) { |
| 472 |
[0] => |
| 473 |
class Sabberworm\CSS\RuleSet\DeclarationBlock#5 (3) { |
| 474 |
private $selectors => |
| 475 |
array(1) { |
| 476 |
[0] => |
| 477 |
class Sabberworm\CSS\Property\Selector#6 (2) { |
| 478 |
private $selector => |
| 479 |
string(7) "#header" |
| 480 |
private $specificity => |
| 481 |
NULL |
| 482 |
} |
| 483 |
} |
| 484 |
private $rules => |
| 485 |
array(3) { |
| 486 |
'margin' => |
| 487 |
array(1) { |
| 488 |
[0] => |
| 489 |
class Sabberworm\CSS\Rule\Rule#7 (4) { |
| 490 |
private $rule => |
| 491 |
string(6) "margin" |
| 492 |
private $value => |
| 493 |
class Sabberworm\CSS\Value\RuleValueList#12 (3) { |
| 494 |
protected $components => |
| 495 |
array(4) { |
| 496 |
[0] => |
| 497 |
class Sabberworm\CSS\Value\Size#8 (4) { |
| 498 |
private $size => |
| 499 |
double(10) |
| 500 |
private $unit => |
| 501 |
string(2) "px" |
| 502 |
private $isColorComponent => |
| 503 |
bool(false) |
| 504 |
protected $lineNumber => |
| 505 |
int(2) |
| 506 |
} |
| 507 |
[1] => |
| 508 |
class Sabberworm\CSS\Value\Size#9 (4) { |
| 509 |
private $size => |
| 510 |
double(2) |
| 511 |
private $unit => |
| 512 |
string(2) "em" |
| 513 |
private $isColorComponent => |
| 514 |
bool(false) |
| 515 |
protected $lineNumber => |
| 516 |
int(2) |
| 517 |
} |
| 518 |
[2] => |
| 519 |
class Sabberworm\CSS\Value\Size#10 (4) { |
| 520 |
private $size => |
| 521 |
double(1) |
| 522 |
private $unit => |
| 523 |
string(2) "cm" |
| 524 |
private $isColorComponent => |
| 525 |
bool(false) |
| 526 |
protected $lineNumber => |
| 527 |
int(2) |
| 528 |
} |
| 529 |
[3] => |
| 530 |
class Sabberworm\CSS\Value\Size#11 (4) { |
| 531 |
private $size => |
| 532 |
double(2) |
| 533 |
private $unit => |
| 534 |
string(1) "%" |
| 535 |
private $isColorComponent => |
| 536 |
bool(false) |
| 537 |
protected $lineNumber => |
| 538 |
int(2) |
| 539 |
} |
| 540 |
} |
| 541 |
protected $separator => |
| 542 |
string(1) " " |
| 543 |
protected $lineNumber => |
| 544 |
int(2) |
| 545 |
} |
| 546 |
private $isImportant => |
| 547 |
bool(false) |
| 548 |
protected $lineNumber => |
| 549 |
int(2) |
| 550 |
} |
| 551 |
} |
| 552 |
'font-family' => |
| 553 |
array(1) { |
| 554 |
[0] => |
| 555 |
class Sabberworm\CSS\Rule\Rule#13 (4) { |
| 556 |
private $rule => |
| 557 |
string(11) "font-family" |
| 558 |
private $value => |
| 559 |
class Sabberworm\CSS\Value\RuleValueList#15 (3) { |
| 560 |
protected $components => |
| 561 |
array(4) { |
| 562 |
[0] => |
| 563 |
string(7) "Verdana" |
| 564 |
[1] => |
| 565 |
string(9) "Helvetica" |
| 566 |
[2] => |
| 567 |
class Sabberworm\CSS\Value\CSSString#14 (2) { |
| 568 |
private $string => |
| 569 |
string(9) "Gill Sans" |
| 570 |
protected $lineNumber => |
| 571 |
int(3) |
| 572 |
} |
| 573 |
[3] => |
| 574 |
string(10) "sans-serif" |
| 575 |
} |
| 576 |
protected $sSeparator => |
| 577 |
string(1) "," |
| 578 |
protected $lineNumber => |
| 579 |
int(3) |
| 580 |
} |
| 581 |
private $isImportant => |
| 582 |
bool(false) |
| 583 |
protected $lineNumber => |
| 584 |
int(3) |
| 585 |
} |
| 586 |
} |
| 587 |
'color' => |
| 588 |
array(1) { |
| 589 |
[0] => |
| 590 |
class Sabberworm\CSS\Rule\Rule#16 (4) { |
| 591 |
private $rule => |
| 592 |
string(5) "color" |
| 593 |
private $value => |
| 594 |
string(3) "red" |
| 595 |
private $isImportant => |
| 596 |
bool(true) |
| 597 |
protected $lineNumber => |
| 598 |
int(4) |
| 599 |
} |
| 600 |
} |
| 601 |
} |
| 602 |
protected $lineNumber => |
| 603 |
int(1) |
| 604 |
} |
| 605 |
} |
| 606 |
protected $lineNumber => |
| 607 |
int(1) |
| 608 |
} |
| 609 |
|
| 610 |
``` |
| 611 |
</details> |
| 612 |
|
| 613 |
#### Output (`render()`) |
| 614 |
|
| 615 |
```css |
| 616 |
#header {margin: 10px 2em 1cm 2%;font-family: Verdana,Helvetica,"Gill Sans",sans-serif;color: red !important;} |
| 617 |
``` |
| 618 |
|
| 619 |
## Class diagram |
| 620 |
|
| 621 |
```mermaid |
| 622 |
classDiagram |
| 623 |
direction LR |
| 624 |
|
| 625 |
class Anchor { |
| 626 |
} |
| 627 |
class AtRule { |
| 628 |
<<interface>> |
| 629 |
} |
| 630 |
class AtRuleBlockList { |
| 631 |
} |
| 632 |
class AtRuleSet { |
| 633 |
} |
| 634 |
class CSSBlockList { |
| 635 |
<<abstract>> |
| 636 |
} |
| 637 |
class CSSElement { |
| 638 |
<<interface>> |
| 639 |
} |
| 640 |
class CSSFunction { |
| 641 |
} |
| 642 |
class CSSList { |
| 643 |
<<abstract>> |
| 644 |
} |
| 645 |
class CSSListItem { |
| 646 |
<<interface>> |
| 647 |
} |
| 648 |
class CSSNamespace { |
| 649 |
} |
| 650 |
class CSSString { |
| 651 |
} |
| 652 |
class CalcFunction { |
| 653 |
} |
| 654 |
class CalcRuleValueList { |
| 655 |
} |
| 656 |
class Charset { |
| 657 |
} |
| 658 |
class Color { |
| 659 |
} |
| 660 |
class Comment { |
| 661 |
} |
| 662 |
class Commentable { |
| 663 |
<<interface>> |
| 664 |
} |
| 665 |
class DeclarationBlock { |
| 666 |
} |
| 667 |
class Document { |
| 668 |
} |
| 669 |
class Import { |
| 670 |
} |
| 671 |
class KeyFrame { |
| 672 |
} |
| 673 |
class KeyframeSelector { |
| 674 |
} |
| 675 |
class LineName { |
| 676 |
} |
| 677 |
class OutputException { |
| 678 |
} |
| 679 |
class OutputFormat { |
| 680 |
} |
| 681 |
class OutputFormatter { |
| 682 |
} |
| 683 |
class Parser { |
| 684 |
} |
| 685 |
class ParserState { |
| 686 |
} |
| 687 |
class Positionable { |
| 688 |
<<interface>> |
| 689 |
} |
| 690 |
class PrimitiveValue { |
| 691 |
<<abstract>> |
| 692 |
} |
| 693 |
class Renderable { |
| 694 |
<<interface>> |
| 695 |
} |
| 696 |
class Rule { |
| 697 |
} |
| 698 |
class RuleContainer { |
| 699 |
<<interface>> |
| 700 |
} |
| 701 |
class RuleSet { |
| 702 |
} |
| 703 |
class RuleValueList { |
| 704 |
} |
| 705 |
class Selector { |
| 706 |
} |
| 707 |
class Settings { |
| 708 |
} |
| 709 |
class Size { |
| 710 |
} |
| 711 |
class SourceException { |
| 712 |
} |
| 713 |
class SpecificityCalculator { |
| 714 |
} |
| 715 |
class URL { |
| 716 |
} |
| 717 |
class UnexpectedEOFException { |
| 718 |
} |
| 719 |
class UnexpectedTokenException { |
| 720 |
} |
| 721 |
class Value { |
| 722 |
<<abstract>> |
| 723 |
} |
| 724 |
class ValueList { |
| 725 |
<<abstract>> |
| 726 |
} |
| 727 |
|
| 728 |
Anchor ..> ParserState: dependency |
| 729 |
CSSListItem <|-- AtRule: inheritance |
| 730 |
AtRule <|.. AtRuleBlockList: realization |
| 731 |
CSSBlockList <|-- AtRuleBlockList: inheritance |
| 732 |
AtRule <|.. AtRuleSet: realization |
| 733 |
RuleSet <|-- AtRuleSet: inheritance |
| 734 |
CSSList <|-- CSSBlockList: inheritance |
| 735 |
Renderable <|-- CSSElement: inheritance |
| 736 |
ValueList <|-- CSSFunction: inheritance |
| 737 |
CSSElement <|.. CSSList: realization |
| 738 |
CSSListItem <|.. CSSList: realization |
| 739 |
CSSList ..> Charset: dependency |
| 740 |
CSSList ..> Import: dependency |
| 741 |
Positionable <|.. CSSList: realization |
| 742 |
Commentable <|-- CSSListItem: inheritance |
| 743 |
Renderable <|-- CSSListItem: inheritance |
| 744 |
AtRule <|.. CSSNamespace: realization |
| 745 |
Positionable <|.. CSSNamespace: realization |
| 746 |
PrimitiveValue <|-- CSSString: inheritance |
| 747 |
CSSFunction <|-- CalcFunction: inheritance |
| 748 |
RuleValueList <|-- CalcRuleValueList: inheritance |
| 749 |
AtRule <|.. Charset: realization |
| 750 |
Charset ..> CSSString: dependency |
| 751 |
Positionable <|.. Charset: realization |
| 752 |
CSSFunction <|-- Color: inheritance |
| 753 |
Positionable <|.. Comment: realization |
| 754 |
Renderable <|.. Comment: realization |
| 755 |
CSSElement <|.. DeclarationBlock: realization |
| 756 |
CSSListItem <|.. DeclarationBlock: realization |
| 757 |
Positionable <|.. DeclarationBlock: realization |
| 758 |
RuleContainer <|.. DeclarationBlock: realization |
| 759 |
DeclarationBlock ..> RuleSet : dependency |
| 760 |
DeclarationBlock ..> Selector: dependency |
| 761 |
CSSBlockList <|-- Document: inheritance |
| 762 |
AtRule <|.. Import: realization |
| 763 |
Positionable <|.. Import: realization |
| 764 |
AtRule <|.. KeyFrame: realization |
| 765 |
CSSList <|-- KeyFrame: inheritance |
| 766 |
Selector <|-- KeyframeSelector: inheritance |
| 767 |
ValueList <|-- LineName: inheritance |
| 768 |
SourceException <|-- OutputException: inheritance |
| 769 |
OutputFormat ..> OutputFormatter: dependency |
| 770 |
OutputFormatter ..> OutputFormat: dependency |
| 771 |
Parser ..> ParserState: dependency |
| 772 |
ParserState ..> Settings: dependency |
| 773 |
Value <|-- PrimitiveValue: inheritance |
| 774 |
CSSElement <|.. Rule: realization |
| 775 |
Commentable <|.. Rule: realization |
| 776 |
Positionable <|.. Rule: realization |
| 777 |
Rule ..> RuleValueList: dependency |
| 778 |
CSSElement <|.. RuleSet: realization |
| 779 |
CSSListItem <|.. RuleSet: realization |
| 780 |
Positionable <|.. RuleSet: realization |
| 781 |
RuleSet ..> Rule: dependency |
| 782 |
RuleContainer <|.. RuleSet: realization |
| 783 |
ValueList <|-- RuleValueList: inheritance |
| 784 |
Renderable <|.. Selector: realization |
| 785 |
PrimitiveValue <|-- Size: inheritance |
| 786 |
Exception <|-- SourceException: inheritance |
| 787 |
Positionable <|.. SourceException: realization |
| 788 |
URL ..> CSSString: dependency |
| 789 |
PrimitiveValue <|-- URL: inheritance |
| 790 |
UnexpectedTokenException <|-- UnexpectedEOFException: inheritance |
| 791 |
SourceException <|-- UnexpectedTokenException: inheritance |
| 792 |
CSSElement <|.. Value: realization |
| 793 |
Positionable <|.. Value: realization |
| 794 |
Value <|-- ValueList: inheritance |
| 795 |
|
| 796 |
CSSList ..> CSSList: dependency |
| 797 |
CSSList ..> Comment: dependency |
| 798 |
CSSList ..> RuleSet: dependency |
| 799 |
CSSNamespace ..> Comment: dependency |
| 800 |
Charset ..> Comment: dependency |
| 801 |
Import ..> Comment: dependency |
| 802 |
OutputFormat ..> OutputFormat: dependency |
| 803 |
Rule ..> Comment: dependency |
| 804 |
RuleSet ..> Comment: dependency |
| 805 |
ValueList ..> Value: dependency |
| 806 |
``` |
| 807 |
|
| 808 |
## API and deprecation policy |
| 809 |
|
| 810 |
Please have a look at our |
| 811 |
[API and deprecation policy](docs/API-and-deprecation-policy.md). |
| 812 |
|
| 813 |
## Contributing |
| 814 |
|
| 815 |
Contributions in the form of bug reports, feature requests, or pull requests are |
| 816 |
more than welcome. :pray: Please have a look at our |
| 817 |
[contribution guidelines](CONTRIBUTING.md) to learn more about how to |
| 818 |
contribute to PHP-CSS-Parser. |
| 819 |
|
| 820 |
## Contributors/Thanks to |
| 821 |
|
| 822 |
* [oliverklee](https://github.com/oliverklee) for lots of refactorings, code modernizations and CI integrations |
| 823 |
* [raxbg](https://github.com/raxbg) for contributions to parse `calc`, grid lines, and various bugfixes. |
| 824 |
* [westonruter](https://github.com/westonruter) for bugfixes and improvements. |
| 825 |
* [FMCorz](https://github.com/FMCorz) for many patches and suggestions, for being able to parse comments and IE hacks (in lenient mode). |
| 826 |
* [Lullabot](https://github.com/Lullabot) for a patch that allows to know the line number for each parsed token. |
| 827 |
* [ju1ius](https://github.com/ju1ius) for the specificity parsing code and the ability to expand/compact shorthand properties. |
| 828 |
* [ossinkine](https://github.com/ossinkine) for a 150 time performance boost. |
| 829 |
* [GaryJones](https://github.com/GaryJones) for lots of input and [https://css-specificity.info/](https://css-specificity.info/). |
| 830 |
* [docteurklein](https://github.com/docteurklein) for output formatting and `CSSList->remove()` inspiration. |
| 831 |
* [nicolopignatelli](https://github.com/nicolopignatelli) for PSR-0 compatibility. |
| 832 |
* [diegoembarcadero](https://github.com/diegoembarcadero) for keyframe at-rule parsing. |
| 833 |
* [goetas](https://github.com/goetas) for `@namespace` at-rule support. |
| 834 |
* [ziegenberg](https://github.com/ziegenberg) for general housekeeping and cleanup. |
| 835 |
* [View full list](https://github.com/sabberworm/PHP-CSS-Parser/contributors) |
| 836 |
|
| 837 |
## Misc |
| 838 |
|
| 839 |
### Legacy Support |
| 840 |
|
| 841 |
The latest pre-PSR-0 version of this project can be checked with the `0.9.0` tag. |