Skip to content

Commit 6a5c770

Browse files
committed
Sync the code documentation from the README to the source files
This should help readers understand the code. Also drop outdated documentation and polish the existing prose. Fixes #300
1 parent e4191b2 commit 6a5c770

18 files changed

+107
-37
lines changed

README.md

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ The resulting CSS document structure can be manipulated prior to being output.
3333

3434
#### Charset
3535

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.
36+
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.
3737

3838
```php
3939
$settings = \Sabberworm\CSS\Settings::create()
@@ -69,29 +69,26 @@ The resulting data structure consists mainly of five basic types: `CSSList`, `Ru
6969

7070
#### CSSList
7171

72-
`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:
72+
`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.
7373

74-
* `Document` – representing the root of a CSS file.
75-
* `MediaQuery` – represents a subsection of a `CSSList` that only applies to an output device matching the contained media query.
76-
77-
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`.
74+
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`.
7875

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

8178
#### RuleSet
8279

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

85-
* `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`.
82+
* `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`.
8683
* `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.
8784

8885
Note: A `CSSList` can contain other `CSSList`s (and `Import`s as well as a `Charset`), while a `RuleSet` can only contain `Rule`s.
8986

90-
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).
87+
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).
9188

9289
#### Rule
9390

94-
`Rule`s just have a key (the rule) and a value. These values are all instances of a `Value`.
91+
`Rule`s just have a string key (the rule) and a `Value`.
9592

9693
#### Value
9794

@@ -100,21 +97,22 @@ If you want to manipulate a `RuleSet`, use the methods `addRule(Rule $rule)`, `g
10097
* `Size` – consists of a numeric `size` value and a unit.
10198
* `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.
10299
* `CSSString` – this is just a wrapper for quoted strings to distinguish them from keywords; always output with double quotes.
103-
* `URL` – URLs in CSS; always output in URL("") notation.
100+
* `URL` – URLs in CSS; always output in `URL("")` notation.
101+
102+
There is another abstract subclass of `Value`, `ValueList`: A `ValueList` represents a lists of `Value`s, separated by some separation character (mostly `,`, whitespace, or `/`).
104103

105-
There is another abstract subclass of `Value`, `ValueList`. A `ValueList` represents a lists of `Value`s, separated by some separation character (mostly `,`, whitespace, or `/`). There are two types of `ValueList`s:
104+
There are two types of `ValueList`s:
106105

107-
* `RuleValueList` – The default type, used to represent all multi-valued 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).
106+
* `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).
108107
* `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);`.
109108

110109
#### Convenience methods
111110

112-
There are a few convenience methods on Document to ease finding, manipulating and deleting rules:
111+
There are a few convenience methods on `Document` to ease finding, manipulating and deleting rules:
113112

114-
* `getAllDeclarationBlocks()` – does what it says; no matter how deeply nested your selectors are. Aliased as `getAllSelectors()`.
115-
* `getAllRuleSets()` – does what it says; no matter how deeply nested your rule sets are.
113+
* `getAllDeclarationBlocks()` – does what it says; no matter how deeply nested the selectors are. Aliased as `getAllSelectors()`.
114+
* `getAllRuleSets()` – does what it says; no matter how deeply nested the rule sets are.
116115
* `getAllValues()` – finds all `Value` objects inside `Rule`s.
117-
* `getSelectorsBySpecificity(int: $specificity)` - finds all selectors with the requested specificity. `$specificity` is an integer with `1` being the highest specificity value. The method behaves just like `getAllDeclarationBlocks()` if no parameter is passed.
118116

119117
## To-Do
120118

src/CSSList/CSSList.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@
2424
use Sabberworm\CSS\Value\Value;
2525

2626
/**
27-
* A `CSSList` is the most generic container available. Its contents include `RuleSet` as well as other `CSSList`
28-
* objects.
27+
* This is the most generic container available. It can contain `DeclarationBlock`s (rule sets with a selector),
28+
* `RuleSet`s as well as other `CSSList` objects.
2929
*
30-
* Also, it may contain `Import` and `Charset` objects stemming from at-rules.
30+
* It can also contain `Import` and `Charset` objects stemming from at-rules.
3131
*/
3232
abstract class CSSList implements Renderable, Commentable
3333
{
@@ -444,6 +444,8 @@ protected function renderListContents(OutputFormat $oOutputFormat)
444444
abstract public function isRootList();
445445

446446
/**
447+
* Returns the stored items.
448+
*
447449
* @return array<int, RuleSet|Import|Charset|CSSList>
448450
*/
449451
public function getContents()

src/CSSList/Document.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
use Sabberworm\CSS\Value\Value;
1212

1313
/**
14-
* The root `CSSList` of a parsed file. Contains all top-level CSS contents, mostly declaration blocks,
15-
* but also any at-rules encountered.
14+
* This class represents the root of a parsed CSS file. It contains all top-level CSS contents: mostly declaration
15+
* blocks, but also any at-rules encountered (`Import` and `Charset`).
1616
*/
1717
class Document extends CSSBlockList
1818
{
@@ -37,7 +37,8 @@ public static function parse(ParserState $oParserState)
3737
}
3838

3939
/**
40-
* Gets all `DeclarationBlock` objects recursively.
40+
* Gets all `DeclarationBlock` objects recursively, no matter how deeply nested the selectors are.
41+
* Aliased as `getAllSelectors()`.
4142
*
4243
* @return array<int, DeclarationBlock>
4344
*/
@@ -62,7 +63,7 @@ public function getAllSelectors()
6263
}
6364

6465
/**
65-
* Returns all `RuleSet` objects found recursively in the tree.
66+
* Returns all `RuleSet` objects recursively found in the tree, no matter how deeply nested the rule sets are.
6667
*
6768
* @return array<int, RuleSet>
6869
*/
@@ -75,7 +76,7 @@ public function getAllRuleSets()
7576
}
7677

7778
/**
78-
* Returns all `Value` objects found recursively in the tree.
79+
* Returns all `Value` objects found recursively in `Rule`s in the tree.
7980
*
8081
* @param CSSList|RuleSet|string $mElement
8182
* the `CSSList` or `RuleSet` to start the search from (defaults to the whole document).
@@ -102,7 +103,7 @@ public function getAllValues($mElement = null, $bSearchInFunctionArguments = fal
102103
}
103104

104105
/**
105-
* Returns all `Selector` objects found recursively in the tree.
106+
* Returns all `Selector` objects with the requested specificity found recursively in the tree.
106107
*
107108
* Note that this does not yield the full `DeclarationBlock` that the selector belongs to
108109
* (and, currently, there is no way to get to that).

src/Parser.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class Parser
1717
private $oParserState;
1818

1919
/**
20-
* @param string $sText
20+
* @param string $sText the complete CSS as text (i.e., usually the contents of a CSS file)
2121
* @param Settings|null $oParserSettings
2222
* @param int $iLineNo the line number (starting from 1, not from 0)
2323
*/
@@ -30,6 +30,8 @@ public function __construct($sText, Settings $oParserSettings = null, $iLineNo =
3030
}
3131

3232
/**
33+
* Sets the charset to be used if the CSS does not contain an `@charset` declaration.
34+
*
3335
* @param string $sCharset
3436
*
3537
* @return void
@@ -40,6 +42,8 @@ public function setCharset($sCharset)
4042
}
4143

4244
/**
45+
* Returns the charset that is used if the CSS does not contain an `@charset` declaration.
46+
*
4347
* @return void
4448
*/
4549
public function getCharset()
@@ -49,6 +53,8 @@ public function getCharset()
4953
}
5054

5155
/**
56+
* Parses the CSS provided to the constructor and creates a `Document` from it.
57+
*
5258
* @return Document
5359
*
5460
* @throws SourceException

src/Parsing/ParserState.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ class ParserState
3333
private $iCurrentPosition;
3434

3535
/**
36+
* will only be used if the CSS does not contain an `@charset` declaration
37+
*
3638
* @var string
3739
*/
3840
private $sCharset;
@@ -48,7 +50,7 @@ class ParserState
4850
private $iLineNo;
4951

5052
/**
51-
* @param string $sText
53+
* @param string $sText the complete CSS as text (i.e., usually the contents of a CSS file)
5254
* @param int $iLineNo
5355
*/
5456
public function __construct($sText, Settings $oParserSettings, $iLineNo = 1)
@@ -61,6 +63,8 @@ public function __construct($sText, Settings $oParserSettings, $iLineNo = 1)
6163
}
6264

6365
/**
66+
* Sets the charset to be used if the CSS does not contain an `@charset` declaration.
67+
*
6468
* @param string $sCharset
6569
*
6670
* @return void
@@ -75,6 +79,8 @@ public function setCharset($sCharset)
7579
}
7680

7781
/**
82+
* Returns the charset that is used if the CSS does not contain an `@charset` declaration.
83+
*
7884
* @return string
7985
*/
8086
public function getCharset()

src/Rule/Rule.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@
1313
use Sabberworm\CSS\Value\Value;
1414

1515
/**
16-
* RuleSets contains Rule objects which always have a key and a value.
17-
* In CSS, Rules are expressed as follows: “key: value[0][0] value[0][1], value[1][0] value[1][1];”
16+
* `Rule`s just have a string key (the rule) and a 'Value'.
17+
*
18+
* In CSS, `Rule`s are expressed as follows: “key: value[0][0] value[0][1], value[1][0] value[1][1];”
1819
*/
1920
class Rule implements Renderable, Commentable
2021
{

src/RuleSet/AtRuleSet.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
use Sabberworm\CSS\Property\AtRule;
77

88
/**
9-
* A RuleSet constructed by an unknown at-rule. `@font-face` rules are rendered into AtRuleSet objects.
9+
* This class represents rule sets for generic at-rules which are not covered by specific classes, i.e., not
10+
* `@import`, `@charset` or `@media`.
11+
*
12+
* A common example for this is `@font-face`.
1013
*/
1114
class AtRuleSet extends RuleSet implements AtRule
1215
{

src/RuleSet/DeclarationBlock.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@
1919
use Sabberworm\CSS\Value\Value;
2020

2121
/**
22-
* Declaration blocks are the parts of a CSS file which denote the rules belonging to a selector.
22+
* This class represents a `RuleSet` constrained by a `Selector`.
23+
*
24+
* It contains an array of selector objects (comma-separated in the CSS) as well as the rules to be applied to the
25+
* matching elements.
2326
*
2427
* Declaration blocks usually appear directly inside a `Document` or another `CSSList` (mostly a `MediaQuery`).
2528
*/

src/RuleSet/RuleSet.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,13 @@
1212
use Sabberworm\CSS\Rule\Rule;
1313

1414
/**
15-
* RuleSet is a generic superclass denoting rules. The typical example for rule sets are declaration block.
16-
* However, unknown At-Rules (like `@font-face`) are also rule sets.
15+
* This class is a container for individual 'Rule's.
16+
*
17+
* The most common form of a rule set is one constrained by a selector, i.e., a `DeclarationBlock`.
18+
* However, unknown `AtRule`s (like `@font-face`) are rule sets as well.
19+
*
20+
* If you want to manipulate a `RuleSet`, use the methods `addRule(Rule $rule)`, `getRules()` and `removeRule($rule)`
21+
* (which accepts either a `Rule` or a rule name; optionally suffixed by a dash to remove all related rules).
1722
*/
1823
abstract class RuleSet implements Renderable, Commentable
1924
{

src/Settings.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,23 @@ class Settings
1111
{
1212
/**
1313
* Multi-byte string support.
14-
* If true (mbstring extension must be enabled), will use (slower) `mb_strlen`, `mb_convert_case`, `mb_substr`
14+
*
15+
* If `true` (`mbstring` extension must be enabled), will use (slower) `mb_strlen`, `mb_convert_case`, `mb_substr`
1516
* and `mb_strpos` functions. Otherwise, the normal (ASCII-Only) functions will be used.
1617
*
1718
* @var bool
1819
*/
1920
public $bMultibyteSupport;
2021

2122
/**
22-
* The default charset for the CSS if no `@charset` rule is found. Defaults to utf-8.
23+
* The default charset for the CSS if no `@charset` declaration is found. Defaults to utf-8.
2324
*
2425
* @var string
2526
*/
2627
public $sDefaultCharset = 'utf-8';
2728

2829
/**
29-
* Lenient parsing. When used (which is true by default), the parser will not choke
30-
* on unexpected tokens but simply ignore them.
30+
* Whether the parser silently ignore invalid rules instead of choking on them.
3131
*
3232
* @var bool
3333
*/
@@ -47,6 +47,11 @@ public static function create()
4747
}
4848

4949
/**
50+
* Enables/disables multi-byte string support.
51+
*
52+
* If `true` (`mbstring` extension must be enabled), will use (slower) `mb_strlen`, `mb_convert_case`, `mb_substr`
53+
* and `mb_strpos` functions. Otherwise, the normal (ASCII-Only) functions will be used.
54+
*
5055
* @param bool $bMultibyteSupport
5156
*
5257
* @return self fluent interface
@@ -58,6 +63,8 @@ public function withMultibyteSupport($bMultibyteSupport = true)
5863
}
5964

6065
/**
66+
* Sets the charset to be used if the CSS does not contain an `@charset` declaration.
67+
*
6168
* @param string $sDefaultCharset
6269
*
6370
* @return self fluent interface
@@ -69,6 +76,8 @@ public function withDefaultCharset($sDefaultCharset)
6976
}
7077

7178
/**
79+
* Configures whether the parser should silently ignore invalid rules.
80+
*
7281
* @param bool $bLenientParsing
7382
*
7483
* @return self fluent interface
@@ -80,6 +89,8 @@ public function withLenientParsing($bLenientParsing = true)
8089
}
8190

8291
/**
92+
* Configures the parser to choke on invalid rules.
93+
*
8394
* @return self fluent interface
8495
*/
8596
public function beStrict()

src/Value/CSSFunction.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
use Sabberworm\CSS\OutputFormat;
66

7+
/**
8+
* A `CSSFunction` represents a special kind of value that also contains a function name and where the values are the
9+
* function’s arguments. It also handles equals-sign-separated argument lists like `filter: alpha(opacity=90);`.
10+
*/
711
class CSSFunction extends ValueList
812
{
913
/**

src/Value/CSSString.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88
use Sabberworm\CSS\Parsing\UnexpectedEOFException;
99
use Sabberworm\CSS\Parsing\UnexpectedTokenException;
1010

11+
/**
12+
* This class is a wrapper for quoted strings to distinguish them from keywords.
13+
*
14+
* `CSSString`s always output with double quotes.
15+
*/
1116
class CSSString extends PrimitiveValue
1217
{
1318
/**

src/Value/Color.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
use Sabberworm\CSS\Parsing\UnexpectedEOFException;
88
use Sabberworm\CSS\Parsing\UnexpectedTokenException;
99

10+
/**
11+
* `Color's can be input in the form #rrggbb, #rgb or schema(val1, val2, …) but are always stored as an array of
12+
* ('s' => val1, 'c' => val2, 'h' => val3, …) and output in the second form.
13+
*/
1014
class Color extends CSSFunction
1115
{
1216
/**

src/Value/RuleValueList.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
namespace Sabberworm\CSS\Value;
44

5+
/**
6+
* This class is used to represent all multivalued rules like `font: bold 12px/3 Helvetica, Verdana, sans-serif;`
7+
* (where the value would be a whitespace-separated list of the primitive value `bold`, a slash-separated list
8+
* and a comma-separated list).
9+
*/
510
class RuleValueList extends ValueList
611
{
712
/**

src/Value/Size.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
use Sabberworm\CSS\Parsing\UnexpectedEOFException;
88
use Sabberworm\CSS\Parsing\UnexpectedTokenException;
99

10+
/**
11+
* A `Size` consists of a numeric `size` value and a unit.
12+
*/
1013
class Size extends PrimitiveValue
1114
{
1215
/**

0 commit comments

Comments
 (0)