Skip to content

Commit 8c4e4c5

Browse files
committed
Polish the README
- fix some typos - clean up the CSS and PHP examples - clean up the Markdown formatting - recommend `composer install` for installing
1 parent 67578e6 commit 8c4e4c5

File tree

1 file changed

+73
-71
lines changed

1 file changed

+73
-71
lines changed

README.md

Lines changed: 73 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,30 @@
1-
PHP CSS Parser
2-
--------------
1+
# PHP CSS Parser
32

43
[![Build Status](https://github.com/sabberworm/PHP-CSS-Parser/workflows/CI/badge.svg?branch=master)](https://github.com/sabberworm/PHP-CSS-Parser/actions/)
54

65
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.
76

87
## Usage
98

10-
### Installation using composer
9+
### Installation using Composer
1110

12-
Add php-css-parser to your composer.json
13-
14-
```json
15-
{
16-
"require": {
17-
"sabberworm/php-css-parser": "*"
18-
}
19-
}
11+
```bash
12+
composer require sabberworm/php-css-parser
2013
```
2114

2215
### Extraction
2316

2417
To use the CSS Parser, create a new instance. The constructor takes the following form:
2518

2619
```php
27-
new Sabberworm\CSS\Parser($sText);
20+
new \Sabberworm\CSS\Parser($css);
2821
```
2922

3023
To read a file, for example, you’d do the following:
3124

3225
```php
33-
$oCssParser = new Sabberworm\CSS\Parser(file_get_contents('somefile.css'));
34-
$oCssDocument = $oCssParser->parse();
26+
$parser = new \Sabberworm\CSS\Parser(file_get_contents('somefile.css'));
27+
$cssDocument = $parser->parse();
3528
```
3629

3730
The resulting CSS document structure can be manipulated prior to being output.
@@ -40,55 +33,59 @@ The resulting CSS document structure can be manipulated prior to being output.
4033

4134
#### Charset
4235

43-
The charset option is used only if no @charset declaration is found in the CSS file. 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.
36+
The charset option is used only if no `@charset` declaration is found in the CSS file. 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.
4437

4538
```php
46-
$oSettings = Sabberworm\CSS\Settings::create()->withDefaultCharset('windows-1252');
47-
new Sabberworm\CSS\Parser($sText, $oSettings);
39+
$settings = \Sabberworm\CSS\Settings::create()
40+
->withDefaultCharset('windows-1252');
41+
$parser = new \Sabberworm\CSS\Parser($css, $settings);
4842
```
4943

5044
#### Strict parsing
5145

52-
To have the parser choke on invalid rules, supply a thusly configured Sabberworm\CSS\Settings object:
46+
To have the parser choke on invalid rules, supply a thusly configured `\Sabberworm\CSS\Settings` object:
5347

5448
```php
55-
$oCssParser = new Sabberworm\CSS\Parser(file_get_contents('somefile.css'), Sabberworm\CSS\Settings::create()->beStrict());
49+
$parser = new \Sabberworm\CSS\Parser(
50+
file_get_contents('somefile.css'),
51+
\Sabberworm\CSS\Settings::create()->beStrict()
52+
);
5653
```
5754

5855
#### Disable multibyte functions
5956

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 to use this with input you have no control over as it’s not thoroughly covered by test cases.
57+
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.
6158

6259
```php
63-
$oSettings = Sabberworm\CSS\Settings::create()->withMultibyteSupport(false);
64-
new Sabberworm\CSS\Parser($sText, $oSettings);
60+
$settings = \Sabberworm\CSS\Settings::create()->withMultibyteSupport(false);
61+
$parser = new \Sabberworm\CSS\Parser($css, $settings);
6562
```
6663

6764
### Manipulation
6865

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.
66+
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.
7067

7168
#### CSSList
7269

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. `CSSList` has the following concrete subtypes:
70+
`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. `CSSList` has the following concrete subtypes:
7471

7572
* `Document` – representing the root of a CSS file.
76-
* `MediaQuery` – represents a subsection of a CSSList that only applies to a output device matching the contained media query.
73+
* `MediaQuery` – represents a subsection of a `CSSList` that only applies to an output device matching the contained media query.
7774

78-
To access the items stored in a `CSSList` – like the document you got back when calling `$oCssParser->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`.
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`.
7976

8077
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.
8178

8279
#### RuleSet
8380

8481
`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:
8582

86-
* `AtRuleSet` – for generic at-rules which do not match the ones specifically mentioned like @import, @charset or @media. A common example for this is @font-face.
87-
* `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.
83+
* `AtRuleSet` – for generic at-rules which do not match the ones specifically mentioned like `@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.
8885

89-
Note: A `CSSList` can contain other `CSSList`s (and `Import`s as well as a `Charset`) while a `RuleSet` can only contain `Rule`s.
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.
9087

91-
If you want to manipulate a `RuleSet`, use the methods `addRule(Rule $oRule)`, `getRules()` and `removeRule($mRule)` (which accepts either a Rule instance or a rule name; optionally suffixed by a dash to remove all related rules).
88+
If you want to manipulate a `RuleSet`, use the methods `addRule(Rule $rule)`, `getRules()` and `removeRule($rule)` (which accepts either a `Rule` instance or a rule name; optionally suffixed by a dash to remove all related rules).
9289

9390
#### Rule
9491

@@ -118,46 +115,50 @@ There are a few convenience methods on Document to ease finding, manipulating an
118115

119116
## To-Do
120117

121-
* More convenience methods [like `selectorsWithElement($sId/Class/TagName)`, `attributesOfType($sType)`, `removeAttributesOfType($sType)`]
122-
* 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).
118+
* More convenience methods (like `selectorsWithElement($sId/Class/TagName)`, `attributesOfType($type)`, `removeAttributesOfType($type)`)
119+
* 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).
123120
* Named color support (using `Color` instead of an anonymous string literal)
124121

125122
## Use cases
126123

127-
### Use `Parser` to prepend an id to all selectors
124+
### Use `Parser` to prepend an ID to all selectors
128125

129126
```php
130-
$sMyId = "#my_id";
131-
$oParser = new Sabberworm\CSS\Parser($sText);
132-
$oCss = $oParser->parse();
133-
foreach($oCss->getAllDeclarationBlocks() as $oBlock) {
134-
foreach($oBlock->getSelectors() as $oSelector) {
135-
//Loop over all selector parts (the comma-separated strings in a selector) and prepend the id
136-
$oSelector->setSelector($sMyId.' '.$oSelector->getSelector());
137-
}
127+
$myId = "#my_id";
128+
$parser = new \Sabberworm\CSS\Parser($css);
129+
$cssDocument = $parser->parse();
130+
foreach ($cssDocument->getAllDeclarationBlocks() as $block) {
131+
foreach ($block->getSelectors() as $selector) {
132+
// Loop over all selector parts (the comma-separated strings in a
133+
// selector) and prepend the ID.
134+
$selector->setSelector($myId.' '.$selector->getSelector());
135+
}
138136
}
139137
```
140138

141139
### Shrink all absolute sizes to half
142140

143141
```php
144-
$oParser = new Sabberworm\CSS\Parser($sText);
145-
$oCss = $oParser->parse();
146-
foreach($oCss->getAllValues() as $mValue) {
147-
if($mValue instanceof CSSSize && !$mValue->isRelative()) {
148-
$mValue->setSize($mValue->getSize()/2);
149-
}
142+
$parser = new \Sabberworm\CSS\Parser($css);
143+
$cssDocument = $parser->parse();
144+
foreach ($cssDocument->getAllValues() as $value) {
145+
if ($value instanceof CSSSize && !$value->isRelative()) {
146+
$value->setSize($value->getSize() / 2);
147+
}
150148
}
151149
```
152150

153151
### Remove unwanted rules
154152

155153
```php
156-
$oParser = new Sabberworm\CSS\Parser($sText);
157-
$oCss = $oParser->parse();
158-
foreach($oCss->getAllRuleSets() as $oRuleSet) {
159-
$oRuleSet->removeRule('font-'); //Note that the added dash will make this remove all rules starting with font- (like font-size, font-weight, etc.) as well as a potential font-rule
160-
$oRuleSet->removeRule('cursor');
154+
$parser = new \Sabberworm\CSS\Parser($css);
155+
$cssDocument = $parser->parse();
156+
foreach($cssDocument->getAllRuleSets() as $oRuleSet) {
157+
// Note that the added dash will make this remove all rules starting with
158+
// `font-` (like `font-size`, `font-weight`, etc.) as well as a potential
159+
// `font-rule`.
160+
$oRuleSet->removeRule('font-');
161+
$oRuleSet->removeRule('cursor');
161162
}
162163
```
163164

@@ -166,26 +167,27 @@ foreach($oCss->getAllRuleSets() as $oRuleSet) {
166167
To output the entire CSS document into a variable, just use `->render()`:
167168

168169
```php
169-
$oCssParser = new Sabberworm\CSS\Parser(file_get_contents('somefile.css'));
170-
$oCssDocument = $oCssParser->parse();
171-
print $oCssDocument->render();
170+
$parser = new \Sabberworm\CSS\Parser(file_get_contents('somefile.css'));
171+
$cssDocument = $parser->parse();
172+
print $cssDocument->render();
172173
```
173174

174-
If you want to format the output, pass an instance of type `Sabberworm\CSS\OutputFormat`:
175+
If you want to format the output, pass an instance of type `\Sabberworm\CSS\OutputFormat`:
175176

176177
```php
177-
$oFormat = Sabberworm\CSS\OutputFormat::create()->indentWithSpaces(4)->setSpaceBetweenRules("\n");
178-
print $oCssDocument->render($oFormat);
178+
$format = \Sabberworm\CSS\OutputFormat::create()
179+
->indentWithSpaces(4)->setSpaceBetweenRules("\n");
180+
print $cssDocument->render($format);
179181
```
180182

181183
Or use one of the predefined formats:
182184

183185
```php
184-
print $oCssDocument->render(Sabberworm\CSS\OutputFormat::createPretty());
185-
print $oCssDocument->render(Sabberworm\CSS\OutputFormat::createCompact());
186+
print $cssDocument->render(Sabberworm\CSS\OutputFormat::createPretty());
187+
print $cssDocument->render(Sabberworm\CSS\OutputFormat::createCompact());
186188
```
187189

188-
To see what you can do with output formatting, look at the tests in `tests/Sabberworm/CSS/OutputFormatTest.php`.
190+
To see what you can do with output formatting, look at the tests in `tests/OutputFormatTest.php`.
189191

190192
## Examples
191193

@@ -198,16 +200,16 @@ To see what you can do with output formatting, look at the tests in `tests/Sabbe
198200

199201
@font-face {
200202
font-family: "CrassRoots";
201-
src: url("../media/cr.ttf")
203+
src: url("../media/cr.ttf");
202204
}
203205

204206
html, body {
205-
font-size: 1.6em
207+
font-size: 1.6em;
206208
}
207209

208210
@keyframes mymove {
209-
from { top: 0px; }
210-
to { top: 200px; }
211+
from { top: 0px; }
212+
to { top: 200px; }
211213
}
212214

213215
```
@@ -440,8 +442,7 @@ class Sabberworm\CSS\CSSList\Document#4 (2) {
440442
@charset "utf-8";
441443
@font-face {font-family: "CrassRoots";src: url("../media/cr.ttf");}
442444
html, body {font-size: 1.6em;}
443-
@keyframes mymove {from {top: 0px;}
444-
to {top: 200px;}}
445+
@keyframes mymove {from {top: 0px;} to {top: 200px;}}
445446
```
446447

447448
### Example 2 (Values)
@@ -450,9 +451,9 @@ html, body {font-size: 1.6em;}
450451

451452
```css
452453
#header {
453-
margin: 10px 2em 1cm 2%;
454-
font-family: Verdana, Helvetica, "Gill Sans", sans-serif;
455-
color: red !important;
454+
margin: 10px 2em 1cm 2%;
455+
font-family: Verdana, Helvetica, "Gill Sans", sans-serif;
456+
color: red !important;
456457
}
457458

458459
```
@@ -611,6 +612,7 @@ class Sabberworm\CSS\CSSList\Document#4 (2) {
611612

612613
## Contributors/Thanks to
613614

615+
* [oliverklee](https://github.com/oliverklee) for lots of refactorings, code modernizations and CI integrations
614616
* [raxbg](https://github.com/raxbg) for contributions to parse `calc`, grid lines, and various bugfixes.
615617
* [westonruter](https://github.com/westonruter) for bugfixes and improvements.
616618
* [FMCorz](https://github.com/FMCorz) for many patches and suggestions, for being able to parse comments and IE hacks (in lenient mode).
@@ -627,4 +629,4 @@ class Sabberworm\CSS\CSSList\Document#4 (2) {
627629
## Misc
628630

629631
* Legacy Support: The latest pre-PSR-0 version of this project can be checked with the `0.9.0` tag.
630-
* Running Tests: To run all unit tests for this project, run `composer install` to install phpunit and use `./vendor/phpunit/phpunit/phpunit`.
632+
* Running Tests: To run all unit tests for this project, run `composer install` to install phpunit and use `./vendor/bin/phpunit`.

0 commit comments

Comments
 (0)