You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
6
8
7
## Usage
9
8
10
-
### Installation using composer
9
+
### Installation using Composer
11
10
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
20
13
```
21
14
22
15
### Extraction
23
16
24
17
To use the CSS Parser, create a new instance. The constructor takes the following form:
25
18
26
19
```php
27
-
new Sabberworm\CSS\Parser($sText);
20
+
new \Sabberworm\CSS\Parser($css);
28
21
```
29
22
30
23
To read a file, for example, you’d do the following:
31
24
32
25
```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();
35
28
```
36
29
37
30
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.
40
33
41
34
#### Charset
42
35
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.
$parser = new \Sabberworm\CSS\Parser($css, $settings);
48
42
```
49
43
50
44
#### Strict parsing
51
45
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:
53
47
54
48
```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
+
);
56
53
```
57
54
58
55
#### Disable multibyte functions
59
56
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.
$parser = new \Sabberworm\CSS\Parser($css, $settings);
65
62
```
66
63
67
64
### Manipulation
68
65
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.
70
67
71
68
#### CSSList
72
69
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:
74
71
75
72
*`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.
77
74
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`.
79
76
80
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.
81
78
82
79
#### RuleSet
83
80
84
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:
85
82
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.
88
85
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.
90
87
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).
92
89
93
90
#### Rule
94
91
@@ -118,46 +115,50 @@ There are a few convenience methods on Document to ease finding, manipulating an
118
115
119
116
## To-Do
120
117
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).
123
120
* Named color support (using `Color` instead of an anonymous string literal)
124
121
125
122
## Use cases
126
123
127
-
### Use `Parser` to prepend an id to all selectors
124
+
### Use `Parser` to prepend an ID to all selectors
128
125
129
126
```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
foreach ($cssDocument->getAllValues() as $value) {
145
+
if ($value instanceof CSSSize && !$value->isRelative()) {
146
+
$value->setSize($value->getSize() / 2);
147
+
}
150
148
}
151
149
```
152
150
153
151
### Remove unwanted rules
154
152
155
153
```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');
161
162
}
162
163
```
163
164
@@ -166,26 +167,27 @@ foreach($oCss->getAllRuleSets() as $oRuleSet) {
166
167
To output the entire CSS document into a variable, just use `->render()`:
167
168
168
169
```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();
172
173
```
173
174
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`:
0 commit comments