Skip to content

Keep more lines under 120 characters #226

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
May 28, 2021
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
42 changes: 34 additions & 8 deletions lib/Sabberworm/CSS/CSSList/CSSList.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,20 @@ private static function parseListItem(ParserState $oParserState, CSSList $oList)
$oAtRule = self::parseAtRule($oParserState);
if ($oAtRule instanceof Charset) {
if (!$bIsRoot) {
throw new UnexpectedTokenException('@charset may only occur in root document', '', 'custom', $oParserState->currentLine());
throw new UnexpectedTokenException(
'@charset may only occur in root document',
'',
'custom',
$oParserState->currentLine()
);
}
if (count($oList->getContents()) > 0) {
throw new UnexpectedTokenException('@charset must be the first parseable token in a document', '', 'custom', $oParserState->currentLine());
throw new UnexpectedTokenException(
'@charset must be the first parseable token in a document',
'',
'custom',
$oParserState->currentLine()
);
}
$oParserState->setCharset($oAtRule->getCharset()->getString());
}
Expand Down Expand Up @@ -160,7 +170,12 @@ private static function parseAtRule(ParserState $oParserState)
throw new UnexpectedTokenException('Wrong namespace prefix', $sPrefix, 'custom', $iIdentifierLineNum);
}
if (!($mUrl instanceof CSSString || $mUrl instanceof URL)) {
throw new UnexpectedTokenException('Wrong namespace url of invalid type', $mUrl, 'custom', $iIdentifierLineNum);
throw new UnexpectedTokenException(
'Wrong namespace url of invalid type',
$mUrl,
'custom',
$iIdentifierLineNum
);
}
return new CSSNamespace($mUrl, $sPrefix, $iIdentifierLineNum);
} else {
Expand Down Expand Up @@ -194,8 +209,9 @@ private static function parseAtRule(ParserState $oParserState)
}
}

/**
* Tests an identifier for a given value. Since identifiers are all keywords, they can be vendor-prefixed. We need to check for these versions too.
/**
* Tests an identifier for a given value. Since identifiers are all keywords, they can be vendor-prefixed.
* We need to check for these versions too.
*/
private static function identifierIs($sIdentifier, $sMatch)
{
Expand Down Expand Up @@ -246,7 +262,11 @@ public function splice($iOffset, $iLength = null, $mReplacement = null)

/**
* Removes an item from the CSS list.
* @param RuleSet|Import|Charset|CSSList $oItemToRemove May be a RuleSet (most likely a DeclarationBlock), a Import, a Charset or another CSSList (most likely a MediaQuery)
*
* @param RuleSet|Import|Charset|CSSList $oItemToRemove
* May be a RuleSet (most likely a DeclarationBlock), a Import,
* a Charset or another CSSList (most likely a MediaQuery)
*
* @return bool Whether the item was removed.
*/
public function remove($oItemToRemove)
Expand All @@ -262,7 +282,9 @@ public function remove($oItemToRemove)
/**
* Replaces an item from the CSS list.
*
* @param RuleSet|Import|Charset|CSSList $oItemToRemove May be a RuleSet (most likely a DeclarationBlock), a Import, a Charset or another CSSList (most likely a MediaQuery)
* @param RuleSet|Import|Charset|CSSList $oItemToRemove
* May be a RuleSet (most likely a DeclarationBlock), a Import, a Charset
* or another CSSList (most likely a MediaQuery)
*/
public function replace($oOldItem, $mNewItem)
{
Expand Down Expand Up @@ -306,7 +328,11 @@ public function removeDeclarationBlockBySelector($mSelector, $bRemoveAll = false
foreach ($mSelector as $iKey => &$mSel) {
if (!($mSel instanceof Selector)) {
if (!Selector::isValid($mSel)) {
throw new UnexpectedTokenException("Selector did not match '" . Selector::SELECTOR_VALIDATION_RX . "'.", $mSel, "custom");
throw new UnexpectedTokenException(
"Selector did not match '" . Selector::SELECTOR_VALIDATION_RX . "'.",
$mSel,
"custom"
);
}
$mSel = new Selector($mSel);
}
Expand Down
15 changes: 11 additions & 4 deletions lib/Sabberworm/CSS/CSSList/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
use Sabberworm\CSS\Parsing\ParserState;

/**
* The root CSSList of a parsed file. Contains all top-level css contents, mostly declaration blocks, but also any @-rules encountered.
* The root CSSList of a parsed file. Contains all top-level css contents, mostly declaration blocks,
* but also any @-rules encountered.
*/
class Document extends CSSBlockList
{
Expand Down Expand Up @@ -62,7 +63,9 @@ public function getAllRuleSets()

/**
* Returns all Value objects found recursively in the tree.
* @param (object|string) $mElement the CSSList or RuleSet to start the search from (defaults to the whole document). If a string is given, it is used as rule name filter (@see{RuleSet->getRules()}).
* @param (object|string) $mElement
* the CSSList or RuleSet to start the search from (defaults to the whole document).
* If a string is given, it is used as rule name filter (@see{RuleSet->getRules()}).
* @param (bool) $bSearchInFunctionArguments whether to also return Value objects used as Function arguments.
*/
public function getAllValues($mElement = null, $bSearchInFunctionArguments = false)
Expand All @@ -81,8 +84,12 @@ public function getAllValues($mElement = null, $bSearchInFunctionArguments = fal

/**
* Returns all Selector objects 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).
* @param $sSpecificitySearch An optional filter by specificity. May contain a comparison operator and a number or just a number (defaults to "==").
* Note that this does not yield the full DeclarationBlock that the selector belongs to
* (and, currently, there is no way to get to that).
*
* @param string $sSpecificitySearch
* An optional filter by specificity.
* May contain a comparison operator and a number or just a number (defaults to "==").
* @example getSelectorsBySpecificity('>= 100')
*/
public function getSelectorsBySpecificity($sSpecificitySearch = null)
Expand Down
9 changes: 6 additions & 3 deletions lib/Sabberworm/CSS/OutputFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,8 @@ public static function create()
public static function createCompact()
{
$format = self::create();
$format->set('Space*Rules', "")->set('Space*Blocks', "")->setSpaceAfterRuleName('')->setSpaceBeforeOpeningBrace('')->setSpaceAfterSelectorSeparator('');
$format->set('Space*Rules', "")->set('Space*Blocks', "")->setSpaceAfterRuleName('')
->setSpaceBeforeOpeningBrace('')->setSpaceAfterSelectorSeparator('');
return $format;
}

Expand All @@ -198,7 +199,8 @@ public static function createCompact()
public static function createPretty()
{
$format = self::create();
$format->set('Space*Rules', "\n")->set('Space*Blocks', "\n")->setSpaceBetweenBlocks("\n\n")->set('SpaceAfterListArgumentSeparator', ['default' => '', ',' => ' ']);
$format->set('Space*Rules', "\n")->set('Space*Blocks', "\n")
->setSpaceBetweenBlocks("\n\n")->set('SpaceAfterListArgumentSeparator', ['default' => '', ',' => ' ']);
return $format;
}
}
Expand All @@ -215,7 +217,8 @@ public function __construct(OutputFormat $oFormat)
public function space($sName, $sType = null)
{
$sSpaceString = $this->oFormat->get("Space$sName");
// If $sSpaceString is an array, we have multple values configured depending on the type of object the space applies to
// If $sSpaceString is an array, we have multple values configured
// depending on the type of object the space applies to
if (is_array($sSpaceString)) {
if ($sType !== null && isset($sSpaceString[$sType])) {
$sSpaceString = $sSpaceString[$sType];
Expand Down
12 changes: 10 additions & 2 deletions lib/Sabberworm/CSS/Parsing/ParserState.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,10 @@ public function parseIdentifier($bIgnoreCase = true)
public function parseCharacter($bIsForIdentifier)
{
if ($this->peek() === '\\') {
if ($bIsForIdentifier && $this->oParserSettings->bLenientParsing && ($this->comes('\0') || $this->comes('\9'))) {
if (
$bIsForIdentifier && $this->oParserSettings->bLenientParsing
&& ($this->comes('\0') || $this->comes('\9'))
) {
// Non-strings can contain \0 or \9 which is an IE hack supported in lenient parsing.
return null;
}
Expand Down Expand Up @@ -265,7 +268,12 @@ public function consumeUntil($aEnd, $bIncludeEnd = false, $consumeEnd = false, a
}

$this->iCurrentPosition = $start;
throw new UnexpectedEOFException('One of ("' . implode('","', $aEnd) . '")', $this->peek(5), 'search', $this->iLineNo);
throw new UnexpectedEOFException(
'One of ("' . implode('","', $aEnd) . '")',
$this->peek(5),
'search',
$this->iLineNo
);
}

private function inputLeft()
Expand Down
3 changes: 2 additions & 1 deletion lib/Sabberworm/CSS/Property/AtRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

interface AtRule extends Renderable, Commentable
{
// Since there are more set rules than block rules, we’re whitelisting the block rules and have anything else be treated as a set rule.
// Since there are more set rules than block rules,
// we’re whitelisting the block rules and have anything else be treated as a set rule.
const BLOCK_RULES = 'media/document/supports/region-style/font-feature-values';
// …and more font-specific ones (to be used inside font-feature-values)
const SET_RULES = 'font-face/counter-style/page/swash/styleset/annotation';
Expand Down
3 changes: 2 additions & 1 deletion lib/Sabberworm/CSS/Property/Import.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ public function __toString()
*/
public function render(\Sabberworm\CSS\OutputFormat $oOutputFormat)
{
return "@import " . $this->oLocation->render($oOutputFormat) . ($this->sMediaQuery === null ? '' : ' ' . $this->sMediaQuery) . ';';
return "@import " . $this->oLocation->render($oOutputFormat)
. ($this->sMediaQuery === null ? '' : ' ' . $this->sMediaQuery) . ';';
}

/**
Expand Down
15 changes: 11 additions & 4 deletions lib/Sabberworm/CSS/Rule/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ public function __construct($sRule, $iLineNo = 0, $iColNo = 0)
public static function parse(ParserState $oParserState)
{
$aComments = $oParserState->consumeWhiteSpace();
$oRule = new Rule($oParserState->parseIdentifier(!$oParserState->comes("--")), $oParserState->currentLine(), $oParserState->currentColumn());
$oRule = new Rule(
$oParserState->parseIdentifier(!$oParserState->comes("--")),
$oParserState->currentLine(),
$oParserState->currentColumn()
);
$oRule->setComments($aComments);
$oRule->addComments($oParserState->consumeWhiteSpace());
$oParserState->consume(':');
Expand Down Expand Up @@ -117,7 +121,8 @@ public function setValue($mValue)
}

/**
* @deprecated Old-Style 2-dimensional array given. Retained for (some) backwards-compatibility. Use setValue() instead and wrapp the value inside a RuleValueList if necessary.
* @deprecated Old-Style 2-dimensional array given. Retained for (some) backwards-compatibility.
* Use setValue() instead and wrap the value inside a RuleValueList if necessary.
*/
public function setValues($aSpaceSeparatedValues)
{
Expand Down Expand Up @@ -153,7 +158,8 @@ public function setValues($aSpaceSeparatedValues)
}

/**
* @deprecated Old-Style 2-dimensional array returned. Retained for (some) backwards-compatibility. Use getValue() instead and check for the existance of a (nested set of) ValueList object(s).
* @deprecated Old-Style 2-dimensional array returned. Retained for (some) backwards-compatibility.
* Use getValue() instead and check for the existance of a (nested set of) ValueList object(s).
*/
public function getValues()
{
Expand All @@ -180,7 +186,8 @@ public function getValues()
}

/**
* Adds a value to the existing value. Value will be appended if a RuleValueList exists of the given type. Otherwise, the existing value will be wrapped by one.
* Adds a value to the existing value. Value will be appended if a RuleValueList exists of the given type.
* Otherwise, the existing value will be wrapped by one.
*/
public function addValue($mValue, $sType = ' ')
{
Expand Down
24 changes: 19 additions & 5 deletions lib/Sabberworm/CSS/RuleSet/DeclarationBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ public static function parse(ParserState $oParserState, $oList = null)
$aSelectorParts = [];
$sStringWrapperChar = false;
do {
$aSelectorParts[] = $oParserState->consume(1) . $oParserState->consumeUntil(['{', '}', '\'', '"'], false, false, $aComments);
$aSelectorParts[] = $oParserState->consume(1)
. $oParserState->consumeUntil(['{', '}', '\'', '"'], false, false, $aComments);
if (in_array($oParserState->peek(), ['\'', '"']) && substr(end($aSelectorParts), -1) != "\\") {
if ($sStringWrapperChar === false) {
$sStringWrapperChar = $oParserState->peek();
Expand Down Expand Up @@ -80,12 +81,20 @@ public function setSelectors($mSelector, $oList = null)
if (!($mSelector instanceof Selector)) {
if ($oList === null || !($oList instanceof KeyFrame)) {
if (!Selector::isValid($mSelector)) {
throw new UnexpectedTokenException("Selector did not match '" . Selector::SELECTOR_VALIDATION_RX . "'.", $mSelector, "custom");
throw new UnexpectedTokenException(
"Selector did not match '" . Selector::SELECTOR_VALIDATION_RX . "'.",
$mSelector,
"custom"
);
}
$this->aSelectors[$iKey] = new Selector($mSelector);
} else {
if (!KeyframeSelector::isValid($mSelector)) {
throw new UnexpectedTokenException("Selector did not match '" . KeyframeSelector::SELECTOR_VALIDATION_RX . "'.", $mSelector, "custom");
throw new UnexpectedTokenException(
"Selector did not match '" . KeyframeSelector::SELECTOR_VALIDATION_RX . "'.",
$mSelector,
"custom"
);
}
$this->aSelectors[$iKey] = new KeyframeSelector($mSelector);
}
Expand Down Expand Up @@ -353,7 +362,9 @@ public function expandBackgroundShorthand()
$aBgProperties = [
'background-color' => ['transparent'], 'background-image' => ['none'],
'background-repeat' => ['repeat'], 'background-attachment' => ['scroll'],
'background-position' => [new Size(0, '%', null, false, $this->iLineNo), new Size(0, '%', null, false, $this->iLineNo)]
'background-position' => [
new Size(0, '%', null, false, $this->iLineNo), new Size(0, '%', null, false, $this->iLineNo)
]
];
$mRuleValue = $oRule->getValue();
$aValues = [];
Expand Down Expand Up @@ -696,7 +707,10 @@ public function render(\Sabberworm\CSS\OutputFormat $oOutputFormat)
throw new OutputException("Attempt to print declaration block with missing selector", $this->iLineNo);
}
$sResult = $oOutputFormat->sBeforeDeclarationBlock;
$sResult .= $oOutputFormat->implode($oOutputFormat->spaceBeforeSelectorSeparator() . ',' . $oOutputFormat->spaceAfterSelectorSeparator(), $this->aSelectors);
$sResult .= $oOutputFormat->implode(
$oOutputFormat->spaceBeforeSelectorSeparator() . ',' . $oOutputFormat->spaceAfterSelectorSeparator(),
$this->aSelectors
);
$sResult .= $oOutputFormat->sAfterDeclarationBlockSelectors;
$sResult .= $oOutputFormat->spaceBeforeOpeningBrace() . '{';
$sResult .= parent::render($oOutputFormat);
Expand Down
Loading