Skip to content

Commit 5cd518e

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 9c89b95 commit 5cd518e

18 files changed

+96
-31
lines changed

README.md

Lines changed: 11 additions & 11 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,27 @@ 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

85+
///////////////// continue here
8886
Note: A `CSSList` can contain other `CSSList`s (and `Import`s as well as a `Charset`), while a `RuleSet` can only contain `Rule`s.
8987

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).
88+
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).
9189

9290
#### Rule
9391

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

9694
#### Value
9795

@@ -102,14 +100,16 @@ If you want to manipulate a `RuleSet`, use the methods `addRule(Rule $rule)`, `g
102100
* `CSSString` – this is just a wrapper for quoted strings to distinguish them from keywords; always output with double quotes.
103101
* `URL` – URLs in CSS; always output in URL("") notation.
104102

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:
103+
There is another abstract subclass of `Value`, `ValueList`: A `ValueList` represents a lists of `Value`s, separated by some separation character (mostly `,`, whitespace, or `/`).
104+
105+
There are two types of `ValueList`s:
106106

107107
* `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).
108108
* `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);`.
109109

110110
#### Convenience methods
111111

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

114114
* `getAllDeclarationBlocks()` – does what it says; no matter how deeply nested your selectors are. Aliased as `getAllSelectors()`.
115115
* `getAllRuleSets()` – does what it says; no matter how deeply nested your rule sets are.

src/CSSList/CSSList.php

Lines changed: 6 additions & 4 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 contains `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 may also contain `Import` and `Charset` objects stemming from at-rules.
3131
*/
3232
abstract class CSSList implements Renderable, Commentable
3333
{
@@ -274,7 +274,7 @@ public function prepend($oItem)
274274
}
275275

276276
/**
277-
* Appends an item to tje list of contents.
277+
* Appends an item to the list of contents.
278278
*
279279
* @param RuleSet|CSSList|Import|Charset $oItem
280280
*
@@ -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: 4 additions & 4 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
{
@@ -62,7 +62,7 @@ public function getAllSelectors()
6262
}
6363

6464
/**
65-
* Returns all `RuleSet` objects found recursively in the tree.
65+
* Returns all `RuleSet` objects recursively found in the tree.
6666
*
6767
* @return array<int, RuleSet>
6868
*/
@@ -75,7 +75,7 @@ public function getAllRuleSets()
7575
}
7676

7777
/**
78-
* Returns all `Value` objects found recursively in the tree.
78+
* Returns all `Value` objects found recursively in `Rule`s the tree.
7979
*
8080
* @param CSSList|RuleSet|string $mElement
8181
* the `CSSList` or `RuleSet` to start the search from (defaults to the whole document).

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 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: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@
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 rules.
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.
1719
*/
1820
abstract class RuleSet implements Renderable, Commentable
1921
{

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 multi-valued 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
/**

src/Value/URL.php

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

11+
/**
12+
* This class represents URLs in CSS. `URL`s always output in URL("") notation.
13+
*/
1114
class URL extends PrimitiveValue
1215
{
1316
/**

src/Value/Value.php

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

11+
/**
12+
* Abstract base class for specific classes of CSS values: `Size`, `Color`, `CSSString` and `URL`
13+
*/
1114
abstract class Value implements Renderable
1215
{
1316
/**

src/Value/ValueList.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 `ValueList` represents a lists of `Value`s, separated by some separation character
9+
* (mostly `,`, whitespace, or `/`).
10+
*/
711
abstract class ValueList extends Value
812
{
913
/**

0 commit comments

Comments
 (0)