Skip to content

Commit 97149f3

Browse files
README modified
1 parent c9470ca commit 97149f3

File tree

3 files changed

+36
-46
lines changed

3 files changed

+36
-46
lines changed

README.md

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,78 +5,84 @@ A Parser for CSS Files written in PHP. Allows extraction of CSS files into a dat
55

66
## Usage
77

8-
### Installation
8+
### Installation using composer
99

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+
}
1117

1218
### Extraction
1319

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

16-
new CSSParser($sCssContents, $sCharset = 'utf-8');
22+
new Sabberworm\CSS\Parser($sText, $sDefaultCharset = 'utf-8');
1723

1824
The charset is used only if no @charset declaration is found in the CSS file.
1925

2026
To read a file, for example, you’d do the following:
2127

22-
$oCssParser = new CSSParser(file_get_contents('somefile.css'));
28+
$oCssParser = new Sabberworm\CSS\Parser(file_get_contents('somefile.css'));
2329
$oCssDocument = $oCssParser->parse();
2430

2531
The resulting CSS document structure can be manipulated prior to being output.
2632

2733
### Manipulation
2834

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

3137
#### CSSList
3238

3339
`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:
3440

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

38-
#### CSSRuleSet
44+
#### RuleSet
3945

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

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

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

47-
#### CSSRule
53+
#### Rule
4854

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

51-
#### CSSValue
57+
#### Value
5258

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

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

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`.
6167

6268
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.
6369

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).
6571

6672
#### Convenience methods
6773

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

7076
* `getAllDeclarationBlocks()` – does what it says; no matter how deeply nested your selectors are. Aliased as `getAllSelectors()`.
7177
* `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.
7379

7480
### Use cases
7581

76-
#### Use `CSSParser` to prepend an id to all selectors
82+
#### Use `Parser` to prepend an id to all selectors
7783

7884
$sMyId = "#my_id";
79-
$oParser = new CSSParser($sCssContents);
85+
$oParser = new Sabberworm\CSS\Parser($sText);
8086
$oCss = $oParser->parse();
8187
foreach($oCss->getAllDeclarationBlocks() as $oBlock) {
8288
foreach($oBlock->getSelectors() as $oSelector) {
@@ -87,7 +93,7 @@ There are a few convenience methods on CSSDocument to ease finding, manipulating
8793

8894
#### Shrink all absolute sizes to half
8995

90-
$oParser = new CSSParser($sCssContents);
96+
$oParser = new Sabberworm\CSS\Parser($sText);
9197
$oCss = $oParser->parse();
9298
foreach($oCss->getAllValues() as $mValue) {
9399
if($mValue instanceof CSSSize && !$mValue->isRelative()) {
@@ -97,7 +103,7 @@ There are a few convenience methods on CSSDocument to ease finding, manipulating
97103

98104
#### Remove unwanted rules
99105

100-
$oParser = new CSSParser($sCssContents);
106+
$oParser = new Sabberworm\CSS\Parser($sText);
101107
$oCss = $oParser->parse();
102108
foreach($oCss->getAllRuleSets() as $oRuleSet) {
103109
$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
108114

109115
To output the entire CSS document into a variable, just use `->__toString()`:
110116

111-
$oCssParser = new CSSParser(file_get_contents('somefile.css'));
117+
$oCssParser = new Sabberworm\CSS\Parser(file_get_contents('somefile.css'));
112118
$oCssDocument = $oCssParser->parse();
113119
print $oCssDocument->__toString();
114120

@@ -352,7 +358,7 @@ To output the entire CSS document into a variable, just use `->__toString()`:
352358
* More convenience methods [like `selectorsWithElement($sId/Class/TagName)`, `removeSelector($oSelector)`, `attributesOfType($sType)`, `removeAttributesOfType($sType)`]
353359
* Options for output (compact, verbose, etc.)
354360
* 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)
356362
* Test suite
357363
* Adopt lenient parsing rules
358364
* Support for @-rules (other than @media) that are CSSLists (to support @-webkit-keyframes)

nbproject/project.properties

Lines changed: 0 additions & 7 deletions
This file was deleted.

nbproject/project.xml

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)