Skip to content

Sync the code documentation from the README to the source files #354

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 14 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ The resulting CSS document structure can be manipulated prior to being output.

#### Charset

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

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

#### CSSList

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

* `Document` – representing the root of a CSS file.
* `MediaQuery` – represents a subsection of a `CSSList` that only applies to an output device matching the contained media query.

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

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.

#### RuleSet

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

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

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

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

#### Rule

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

#### Value

Expand All @@ -100,21 +97,22 @@ If you want to manipulate a `RuleSet`, use the methods `addRule(Rule $rule)`, `g
* `Size` – consists of a numeric `size` value and a unit.
* `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.
* `CSSString` – this is just a wrapper for quoted strings to distinguish them from keywords; always output with double quotes.
* `URL` – URLs in CSS; always output in URL("") notation.
* `URL` – URLs in CSS; always output in `URL("")` notation.

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 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:
There are two types of `ValueList`s:

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

#### Convenience methods

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

* `getAllDeclarationBlocks()` – does what it says; no matter how deeply nested your selectors are. Aliased as `getAllSelectors()`.
* `getAllRuleSets()` – does what it says; no matter how deeply nested your rule sets are.
* `getAllDeclarationBlocks()` – does what it says; no matter how deeply nested the selectors are. Aliased as `getAllSelectors()`.
* `getAllRuleSets()` – does what it says; no matter how deeply nested the rule sets are.
* `getAllValues()` – finds all `Value` objects inside `Rule`s.
* `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.

## To-Do

Expand Down
8 changes: 5 additions & 3 deletions src/CSSList/CSSList.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
use Sabberworm\CSS\Value\Value;

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

/**
* Returns the stored items.
*
* @return array<int, RuleSet|Import|Charset|CSSList>
*/
public function getContents()
Expand Down
13 changes: 7 additions & 6 deletions src/CSSList/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
use Sabberworm\CSS\Value\Value;

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

/**
* Gets all `DeclarationBlock` objects recursively.
* Gets all `DeclarationBlock` objects recursively, no matter how deeply nested the selectors are.
* Aliased as `getAllSelectors()`.
*
* @return array<int, DeclarationBlock>
*/
Expand All @@ -62,7 +63,7 @@ public function getAllSelectors()
}

/**
* Returns all `RuleSet` objects found recursively in the tree.
* Returns all `RuleSet` objects recursively found in the tree, no matter how deeply nested the rule sets are.
*
* @return array<int, RuleSet>
*/
Expand All @@ -75,7 +76,7 @@ public function getAllRuleSets()
}

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

/**
* Returns all `Selector` objects found recursively in the tree.
* Returns all `Selector` objects with the requested specificity found recursively in the tree.
*
* Note that this does not yield the full `DeclarationBlock` that the selector belongs to
* (and, currently, there is no way to get to that).
Expand Down
8 changes: 7 additions & 1 deletion src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Parser
private $oParserState;

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

/**
* Sets the charset to be used if the CSS does not contain an `@charset` declaration.
*
* @param string $sCharset
*
* @return void
Expand All @@ -40,6 +42,8 @@ public function setCharset($sCharset)
}

/**
* Returns the charset that is used if the CSS does not contain an `@charset` declaration.
*
* @return void
*/
public function getCharset()
Expand All @@ -49,6 +53,8 @@ public function getCharset()
}

/**
* Parses the CSS provided to the constructor and creates a `Document` from it.
*
* @return Document
*
* @throws SourceException
Expand Down
8 changes: 7 additions & 1 deletion src/Parsing/ParserState.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class ParserState
private $iCurrentPosition;

/**
* will only be used if the CSS does not contain an `@charset` declaration
*
* @var string
*/
private $sCharset;
Expand All @@ -48,7 +50,7 @@ class ParserState
private $iLineNo;

/**
* @param string $sText
* @param string $sText the complete CSS as text (i.e., usually the contents of a CSS file)
* @param int $iLineNo
*/
public function __construct($sText, Settings $oParserSettings, $iLineNo = 1)
Expand All @@ -61,6 +63,8 @@ public function __construct($sText, Settings $oParserSettings, $iLineNo = 1)
}

/**
* Sets the charset to be used if the CSS does not contain an `@charset` declaration.
*
* @param string $sCharset
*
* @return void
Expand All @@ -75,6 +79,8 @@ public function setCharset($sCharset)
}

/**
* Returns the charset that is used if the CSS does not contain an `@charset` declaration.
*
* @return string
*/
public function getCharset()
Expand Down
5 changes: 3 additions & 2 deletions src/Rule/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
use Sabberworm\CSS\Value\Value;

/**
* RuleSets contains Rule objects which always have a key and a value.
* In CSS, Rules are expressed as follows: “key: value[0][0] value[0][1], value[1][0] value[1][1];”
* `Rule`s just have a string key (the rule) and a 'Value'.
*
* In CSS, `Rule`s are expressed as follows: “key: value[0][0] value[0][1], value[1][0] value[1][1];”
*/
class Rule implements Renderable, Commentable
{
Expand Down
5 changes: 4 additions & 1 deletion src/RuleSet/AtRuleSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
use Sabberworm\CSS\Property\AtRule;

/**
* A RuleSet constructed by an unknown at-rule. `@font-face` rules are rendered into AtRuleSet objects.
* This class represents rule sets 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`.
*/
class AtRuleSet extends RuleSet implements AtRule
{
Expand Down
5 changes: 4 additions & 1 deletion src/RuleSet/DeclarationBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
use Sabberworm\CSS\Value\Value;

/**
* Declaration blocks are the parts of a CSS file which denote the rules belonging to a selector.
* This class represents a `RuleSet` constrained by a `Selector`.
*
* It contains an array of selector objects (comma-separated in the CSS) as well as the rules to be applied to the
* matching elements.
*
* Declaration blocks usually appear directly inside a `Document` or another `CSSList` (mostly a `MediaQuery`).
*/
Expand Down
9 changes: 7 additions & 2 deletions src/RuleSet/RuleSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,13 @@
use Sabberworm\CSS\Rule\Rule;

/**
* RuleSet is a generic superclass denoting rules. The typical example for rule sets are declaration block.
* However, unknown At-Rules (like `@font-face`) are also rule sets.
* This class is a container for individual 'Rule's.
*
* The most common form of a rule set is one constrained by a selector, i.e., a `DeclarationBlock`.
* However, unknown `AtRule`s (like `@font-face`) are rule sets as well.
*
* 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).
*/
abstract class RuleSet implements Renderable, Commentable
{
Expand Down
19 changes: 15 additions & 4 deletions src/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,23 @@ class Settings
{
/**
* Multi-byte string support.
* If true (mbstring extension must be enabled), will use (slower) `mb_strlen`, `mb_convert_case`, `mb_substr`
*
* If `true` (`mbstring` extension must be enabled), will use (slower) `mb_strlen`, `mb_convert_case`, `mb_substr`
* and `mb_strpos` functions. Otherwise, the normal (ASCII-Only) functions will be used.
*
* @var bool
*/
public $bMultibyteSupport;

/**
* The default charset for the CSS if no `@charset` rule is found. Defaults to utf-8.
* The default charset for the CSS if no `@charset` declaration is found. Defaults to utf-8.
*
* @var string
*/
public $sDefaultCharset = 'utf-8';

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

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

/**
* Sets the charset to be used if the CSS does not contain an `@charset` declaration.
*
* @param string $sDefaultCharset
*
* @return self fluent interface
Expand All @@ -69,6 +76,8 @@ public function withDefaultCharset($sDefaultCharset)
}

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

/**
* Configures the parser to choke on invalid rules.
*
* @return self fluent interface
*/
public function beStrict()
Expand Down
4 changes: 4 additions & 0 deletions src/Value/CSSFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

use Sabberworm\CSS\OutputFormat;

/**
* A `CSSFunction` represents a special kind of value that also contains a function name and where the values are the
* function’s arguments. It also handles equals-sign-separated argument lists like `filter: alpha(opacity=90);`.
*/
class CSSFunction extends ValueList
{
/**
Expand Down
5 changes: 5 additions & 0 deletions src/Value/CSSString.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
use Sabberworm\CSS\Parsing\UnexpectedEOFException;
use Sabberworm\CSS\Parsing\UnexpectedTokenException;

/**
* This class is a wrapper for quoted strings to distinguish them from keywords.
*
* `CSSString`s always output with double quotes.
*/
class CSSString extends PrimitiveValue
{
/**
Expand Down
4 changes: 4 additions & 0 deletions src/Value/Color.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
use Sabberworm\CSS\Parsing\UnexpectedEOFException;
use Sabberworm\CSS\Parsing\UnexpectedTokenException;

/**
* `Color's 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.
*/
class Color extends CSSFunction
{
/**
Expand Down
5 changes: 5 additions & 0 deletions src/Value/RuleValueList.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

namespace Sabberworm\CSS\Value;

/**
* This class is 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).
*/
class RuleValueList extends ValueList
{
/**
Expand Down
3 changes: 3 additions & 0 deletions src/Value/Size.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
use Sabberworm\CSS\Parsing\UnexpectedEOFException;
use Sabberworm\CSS\Parsing\UnexpectedTokenException;

/**
* A `Size` consists of a numeric `size` value and a unit.
*/
class Size extends PrimitiveValue
{
/**
Expand Down
Loading