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
Copy file name to clipboardExpand all lines: README.md
+36-30Lines changed: 36 additions & 30 deletions
Original file line number
Diff line number
Diff line change
@@ -5,78 +5,84 @@ A Parser for CSS Files written in PHP. Allows extraction of CSS files into a dat
5
5
6
6
## Usage
7
7
8
-
### Installation
8
+
### Installation using composer
9
9
10
-
Include the `CSSParser.php` file somewhere in your code using `require_once` (or `include_once`, if you prefer), the given `lib` folder needs to exist next to the file.
10
+
Add php-css-parser to your composer.json
11
+
12
+
{
13
+
"require": {
14
+
"sabberworm/php-css-parser": "*"
15
+
}
16
+
}
11
17
12
18
### Extraction
13
19
14
20
To use the CSS Parser, create a new instance. The constructor takes the following form:
15
21
16
-
new CSSParser($sCssContents, $sCharset = 'utf-8');
22
+
new Sabberworm\CSS\Parser($sText, $sDefaultCharset = 'utf-8');
17
23
18
24
The charset is used only if no @charset declaration is found in the CSS file.
19
25
20
26
To read a file, for example, you’d do the following:
21
27
22
-
$oCssParser = new CSSParser(file_get_contents('somefile.css'));
28
+
$oCssParser = new Sabberworm\CSS\Parser(file_get_contents('somefile.css'));
23
29
$oCssDocument = $oCssParser->parse();
24
30
25
31
The resulting CSS document structure can be manipulated prior to being output.
26
32
27
33
### Manipulation
28
34
29
-
The resulting data structure consists mainly of five basic types: `CSSList`, `CSSRuleSet`, `CSSRule`, `CSSSelector` and `CSSValue`. There are two additional types used: `CSSImport` and `CSSCharset` which you won’t use often.
35
+
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.
30
36
31
37
#### CSSList
32
38
33
39
`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:
34
40
35
-
*`CSSDocument` – representing the root of a CSS file.
36
-
*`CSSMediaQuery` – represents a subsection of a CSSList that only applies to a output device matching the contained media query.
41
+
*`Document` – representing the root of a CSS file.
42
+
*`MediaQuery` – represents a subsection of a CSSList that only applies to a output device matching the contained media query.
37
43
38
-
#### CSSRuleSet
44
+
#### RuleSet
39
45
40
-
`CSSRuleSet` 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:
46
+
`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:
41
47
42
-
*`CSSAtRule` – 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.
43
-
*`CSSDeclarationBlock` – a RuleSet constrained by a `CSSSelector; contains an array of selector objects (comma-separated in the CSS) as well as the rules to be applied to the matching elements.
48
+
*`AtRule` – 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.
49
+
*`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.
44
50
45
-
Note: A `CSSList` can contain other `CSSList`s (and `CSSImport`s as well as a `CSSCharset`) while a `CSSRuleSet` can only contain `CSSRule`s.
51
+
Note: A `CSSList` can contain other `CSSList`s (and `Import`s as well as a `Charset`) while a `RuleSet` can only contain `Rule`s.
46
52
47
-
#### CSSRule
53
+
#### Rule
48
54
49
-
`CSSRule`s just have a key (the rule) and multiple values (the part after the colon in the CSS file). This means the `values` attribute is an array consisting of arrays. The inner level of arrays is comma-separated in the CSS file while the outer level is whitespace-separated.
55
+
`Rule`s just have a key (the rule) and multiple values (the part after the colon in the CSS file). This means the `values` attribute is an array consisting of arrays. The inner level of arrays is comma-separated in the CSS file while the outer level is whitespace-separated.
50
56
51
-
#### CSSValue
57
+
#### Value
52
58
53
-
`CSSValue` is an abstract class that only defines the `__toString` method. The concrete subclasses are:
59
+
`Value` is an abstract class that only defines the `__toString` method. The concrete subclasses are:
54
60
55
-
*`CSSSize` – consists of a numeric `size` value and a unit.
56
-
*`CSSColor` – colors can be input in the form #rrggbb, #rgb or schema(val1, val2, …) but are alwas stored as an array of ('s' => val1, 'c' => val2, 'h' => val3, …) and output in the second form.
57
-
*`CSSString` – this is just a wrapper for quoted strings to distinguish them from keywords; always output with double quotes.
58
-
*`CSSURL` – URLs in CSS; always output in URL("") notation.
61
+
*`Size` – consists of a numeric `size` value and a unit.
62
+
*`Color` – colors can be input in the form #rrggbb, #rgb or schema(val1, val2, …) but are alwas stored as an array of ('s' => val1, 'c' => val2, 'h' => val3, …) and output in the second form.
63
+
*`String` – this is just a wrapper for quoted strings to distinguish them from keywords; always output with double quotes.
64
+
*`URL` – URLs in CSS; always output in URL("") notation.
59
65
60
-
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 `CSSRuleSet`, a `CSSImport` or a `CSSCharset`.
66
+
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`.
61
67
62
68
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.
63
69
64
-
If you want to manipulate a `CSSRuleSet`, use the methods `addRule(CSSRule $oRule)`, `getRules()` and `removeRule($mRule)` (which accepts either a CSSRule instance or a rule name; optionally suffixed by a dash to remove all related rules).
70
+
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).
65
71
66
72
#### Convenience methods
67
73
68
-
There are a few convenience methods on CSSDocument to ease finding, manipulating and deleting rules:
74
+
There are a few convenience methods on Document to ease finding, manipulating and deleting rules:
69
75
70
76
*`getAllDeclarationBlocks()` – does what it says; no matter how deeply nested your selectors are. Aliased as `getAllSelectors()`.
71
77
*`getAllRuleSets()` – does what it says; no matter how deeply nested your rule sets are.
72
-
*`getAllValues()` – finds all `CSSValue` objects inside `CSSRule`s.
78
+
*`getAllValues()` – finds all `Value` objects inside `Rule`s.
73
79
74
80
### Use cases
75
81
76
-
#### Use `CSSParser` to prepend an id to all selectors
82
+
#### Use `Parser` to prepend an id to all selectors
77
83
78
84
$sMyId = "#my_id";
79
-
$oParser = new CSSParser($sCssContents);
85
+
$oParser = new Sabberworm\CSS\Parser($sText);
80
86
$oCss = $oParser->parse();
81
87
foreach($oCss->getAllDeclarationBlocks() as $oBlock) {
82
88
foreach($oBlock->getSelectors() as $oSelector) {
@@ -87,7 +93,7 @@ There are a few convenience methods on CSSDocument to ease finding, manipulating
@@ -97,7 +103,7 @@ There are a few convenience methods on CSSDocument to ease finding, manipulating
97
103
98
104
#### Remove unwanted rules
99
105
100
-
$oParser = new CSSParser($sCssContents);
106
+
$oParser = new Sabberworm\CSS\Parser($sText);
101
107
$oCss = $oParser->parse();
102
108
foreach($oCss->getAllRuleSets() as $oRuleSet) {
103
109
$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
@@ -108,7 +114,7 @@ There are a few convenience methods on CSSDocument to ease finding, manipulating
108
114
109
115
To output the entire CSS document into a variable, just use `->__toString()`:
110
116
111
-
$oCssParser = new CSSParser(file_get_contents('somefile.css'));
117
+
$oCssParser = new Sabberworm\CSS\Parser(file_get_contents('somefile.css'));
112
118
$oCssDocument = $oCssParser->parse();
113
119
print $oCssDocument->__toString();
114
120
@@ -352,7 +358,7 @@ To output the entire CSS document into a variable, just use `->__toString()`:
352
358
* More convenience methods [like `selectorsWithElement($sId/Class/TagName)`, `removeSelector($oSelector)`, `attributesOfType($sType)`, `removeAttributesOfType($sType)`]
353
359
* Options for output (compact, verbose, etc.)
354
360
* Support for @namespace
355
-
* Named color support (using `CSSColor` instead of an anonymous string literal)
361
+
* Named color support (using `Color` instead of an anonymous string literal)
356
362
* Test suite
357
363
* Adopt lenient parsing rules
358
364
* Support for @-rules (other than @media) that are CSSLists (to support @-webkit-keyframes)
0 commit comments