diff --git a/lib/Sabberworm/CSS/CSSList/CSSList.php b/lib/Sabberworm/CSS/CSSList/CSSList.php index 7a4ef22c..79aca07d 100644 --- a/lib/Sabberworm/CSS/CSSList/CSSList.php +++ b/lib/Sabberworm/CSS/CSSList/CSSList.php @@ -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()); } @@ -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 { @@ -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) { @@ -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) @@ -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) { @@ -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); } diff --git a/lib/Sabberworm/CSS/CSSList/Document.php b/lib/Sabberworm/CSS/CSSList/Document.php index 0b07f1ff..d5125ac1 100644 --- a/lib/Sabberworm/CSS/CSSList/Document.php +++ b/lib/Sabberworm/CSS/CSSList/Document.php @@ -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 { @@ -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) @@ -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) diff --git a/lib/Sabberworm/CSS/OutputFormat.php b/lib/Sabberworm/CSS/OutputFormat.php index a8d05992..ff927fbd 100644 --- a/lib/Sabberworm/CSS/OutputFormat.php +++ b/lib/Sabberworm/CSS/OutputFormat.php @@ -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; } @@ -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; } } @@ -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]; diff --git a/lib/Sabberworm/CSS/Parsing/ParserState.php b/lib/Sabberworm/CSS/Parsing/ParserState.php index 60d0b908..1a47f90f 100644 --- a/lib/Sabberworm/CSS/Parsing/ParserState.php +++ b/lib/Sabberworm/CSS/Parsing/ParserState.php @@ -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; } @@ -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() diff --git a/lib/Sabberworm/CSS/Property/AtRule.php b/lib/Sabberworm/CSS/Property/AtRule.php index 291c388b..6de34231 100644 --- a/lib/Sabberworm/CSS/Property/AtRule.php +++ b/lib/Sabberworm/CSS/Property/AtRule.php @@ -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'; diff --git a/lib/Sabberworm/CSS/Property/Import.php b/lib/Sabberworm/CSS/Property/Import.php index 4cd5a10b..b4b64321 100644 --- a/lib/Sabberworm/CSS/Property/Import.php +++ b/lib/Sabberworm/CSS/Property/Import.php @@ -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) . ';'; } /** diff --git a/lib/Sabberworm/CSS/Rule/Rule.php b/lib/Sabberworm/CSS/Rule/Rule.php index f3d97fcc..9f4de610 100644 --- a/lib/Sabberworm/CSS/Rule/Rule.php +++ b/lib/Sabberworm/CSS/Rule/Rule.php @@ -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(':'); @@ -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) { @@ -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() { @@ -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 = ' ') { diff --git a/lib/Sabberworm/CSS/RuleSet/DeclarationBlock.php b/lib/Sabberworm/CSS/RuleSet/DeclarationBlock.php index 9d395c9e..49298d08 100644 --- a/lib/Sabberworm/CSS/RuleSet/DeclarationBlock.php +++ b/lib/Sabberworm/CSS/RuleSet/DeclarationBlock.php @@ -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(); @@ -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); } @@ -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 = []; @@ -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); diff --git a/lib/Sabberworm/CSS/RuleSet/RuleSet.php b/lib/Sabberworm/CSS/RuleSet/RuleSet.php index 8cec47b2..39eb9bef 100644 --- a/lib/Sabberworm/CSS/RuleSet/RuleSet.php +++ b/lib/Sabberworm/CSS/RuleSet/RuleSet.php @@ -102,9 +102,14 @@ public function addRule(Rule $oRule, Rule $oSibling = null) /** * Returns all rules matching the given rule name * - * @param null|string|Rule $mRule pattern to search for. If null, returns all rules. if the pattern ends with a dash, all rules starting with the pattern are returned as well as one matching the pattern with the dash excluded. passing a Rule behaves like calling getRules($mRule->getRule()). + * @param null|string|Rule $mRule + * pattern to search for. If null, returns all rules. + * if the pattern ends with a dash, all rules starting with the pattern are returned + * as well as one matching the pattern with the dash excluded. + * passing a Rule behaves like calling getRules($mRule->getRule()). * - * @example $oRuleSet->getRules('font-') //returns an array of all rules either beginning with font- or matching font. + * @example $oRuleSet->getRules('font-') + * //returns an array of all rules either beginning with font- or matching font. * @example $oRuleSet->getRules('font') //returns array(0 => $oRule, …) or array(). * * @return array Rules. @@ -116,8 +121,14 @@ public function getRules($mRule = null) } $aResult = []; foreach ($this->aRules as $sName => $aRules) { - // Either no search rule is given or the search rule matches the found rule exactly or the search rule ends in “-” and the found rule starts with the search rule. - if (!$mRule || $sName === $mRule || (strrpos($mRule, '-') === strlen($mRule) - strlen('-') && (strpos($sName, $mRule) === 0 || $sName === substr($mRule, 0, -1)))) { + // Either no search rule is given or the search rule matches the found rule exactly + // or the search rule ends in “-” and the found rule starts with the search rule. + if ( + !$mRule || $sName === $mRule + || (strrpos($mRule, '-') === strlen($mRule) - strlen('-') + && (strpos($sName, $mRule) === 0 || $sName === substr($mRule, 0, -1)) + ) + ) { $aResult = array_merge($aResult, $aRules); } } @@ -143,9 +154,17 @@ public function setRules(array $aRules) } /** - * Returns all rules matching the given pattern and returns them in an associative array with the rule’s name as keys. This method exists mainly for backwards-compatibility and is really only partially useful. - * @param (string) $mRule pattern to search for. If null, returns all rules. if the pattern ends with a dash, all rules starting with the pattern are returned as well as one matching the pattern with the dash excluded. passing a Rule behaves like calling getRules($mRule->getRule()). - * Note: This method loses some information: Calling this (with an argument of 'background-') on a declaration block like { background-color: green; background-color; rgba(0, 127, 0, 0.7); } will only yield an associative array containing the rgba-valued rule while @link{getRules()} would yield an indexed array containing both. + * Returns all rules matching the given pattern and returns them in an associative array with the rule’s name + * as keys. This method exists mainly for backwards-compatibility and is really only partially useful. + * + * Note: This method loses some information: Calling this (with an argument of 'background-') on a declaration block + * like { background-color: green; background-color; rgba(0, 127, 0, 0.7); } will only yield an associative array + * containing the rgba-valued rule while @link{getRules()} would yield an indexed array containing both. + * + * @param (string) $mRule + * pattern to search for. If null, returns all rules. if the pattern ends with a dash, + * all rules starting with the pattern are returned as well as one matching the pattern with the dash + * excluded. passing a Rule behaves like calling getRules($mRule->getRule()). * @return Rule[] Rules. */ public function getRulesAssoc($mRule = null) @@ -158,8 +177,17 @@ public function getRulesAssoc($mRule = null) } /** - * Remove a rule from this RuleSet. This accepts all the possible values that @link{getRules()} accepts. If given a Rule, it will only remove this particular rule (by identity). If given a name, it will remove all rules by that name. Note: this is different from pre-v.2.0 behaviour of PHP-CSS-Parser, where passing a Rule instance would remove all rules with the same name. To get the old behvaiour, use removeRule($oRule->getRule()). - * @param (null|string|Rule) $mRule pattern to remove. If $mRule is null, all rules are removed. If the pattern ends in a dash, all rules starting with the pattern are removed as well as one matching the pattern with the dash excluded. Passing a Rule behaves matches by identity. + * Remove a rule from this RuleSet. This accepts all the possible values that @link{getRules()} accepts. + * If given a Rule, it will only remove this particular rule (by identity). + * If given a name, it will remove all rules by that name. + * + * Note: this is different from pre-v.2.0 behaviour of PHP-CSS-Parser, where passing a Rule instance would + * remove all rules with the same name. To get the old behvaiour, use removeRule($oRule->getRule()). + * + * @param (null|string|Rule) $mRule + * pattern to remove. If $mRule is null, all rules are removed. If the pattern ends in a dash, + * all rules starting with the pattern are removed as well as one matching the pattern with the dash + * excluded. Passing a Rule behaves matches by identity. */ public function removeRule($mRule) { @@ -175,8 +203,14 @@ public function removeRule($mRule) } } else { foreach ($this->aRules as $sName => $aRules) { - // Either no search rule is given or the search rule matches the found rule exactly or the search rule ends in “-” and the found rule starts with the search rule or equals it (without the trailing dash). - if (!$mRule || $sName === $mRule || (strrpos($mRule, '-') === strlen($mRule) - strlen('-') && (strpos($sName, $mRule) === 0 || $sName === substr($mRule, 0, -1)))) { + // Either no search rule is given or the search rule matches the found rule exactly + // or the search rule ends in “-” and the found rule starts with the search rule or equals it + // (without the trailing dash). + if ( + !$mRule || $sName === $mRule + || (strrpos($mRule, '-') === strlen($mRule) - strlen('-') + && (strpos($sName, $mRule) === 0 || $sName === substr($mRule, 0, -1))) + ) { unset($this->aRules[$sName]); } } diff --git a/lib/Sabberworm/CSS/Settings.php b/lib/Sabberworm/CSS/Settings.php index ad89d4eb..cb7fd27f 100644 --- a/lib/Sabberworm/CSS/Settings.php +++ b/lib/Sabberworm/CSS/Settings.php @@ -12,18 +12,21 @@ class Settings { /** - * 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. - */ + * 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. + */ 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` rule is found. Defaults to utf-8. + */ 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. - */ + * Lenient parsing. When used (which is true by default), the parser will not choke + * on unexpected tokens but simply ignore them. + */ public $bLenientParsing = true; private function __construct() diff --git a/lib/Sabberworm/CSS/Value/CSSString.php b/lib/Sabberworm/CSS/Value/CSSString.php index 3de9416a..6aedf2b0 100644 --- a/lib/Sabberworm/CSS/Value/CSSString.php +++ b/lib/Sabberworm/CSS/Value/CSSString.php @@ -39,7 +39,10 @@ public static function parse(ParserState $oParserState) while (!$oParserState->comes($sQuote)) { $sContent = $oParserState->parseCharacter(false); if ($sContent === null) { - throw new SourceException("Non-well-formed quoted string {$oParserState->peek(3)}", $oParserState->currentLine()); + throw new SourceException( + "Non-well-formed quoted string {$oParserState->peek(3)}", + $oParserState->currentLine() + ); } $sResult .= $sContent; } diff --git a/lib/Sabberworm/CSS/Value/CalcFunction.php b/lib/Sabberworm/CSS/Value/CalcFunction.php index e03fd45f..b17d4c3f 100644 --- a/lib/Sabberworm/CSS/Value/CalcFunction.php +++ b/lib/Sabberworm/CSS/Value/CalcFunction.php @@ -39,7 +39,12 @@ public static function parse(ParserState $oParserState) if (in_array($oParserState->peek(), $aOperators)) { if (($oParserState->comes('-') || $oParserState->comes('+'))) { if ($oParserState->peek(1, -1) != ' ' || !($oParserState->comes('- ') || $oParserState->comes('+ '))) { - throw new UnexpectedTokenException(" {$oParserState->peek()} ", $oParserState->peek(1, -1) . $oParserState->peek(2), 'literal', $oParserState->currentLine()); + throw new UnexpectedTokenException( + " {$oParserState->peek()} ", + $oParserState->peek(1, -1) . $oParserState->peek(2), + 'literal', + $oParserState->currentLine() + ); } } $oCalcList->addListComponent($oParserState->consume(1)); diff --git a/lib/Sabberworm/CSS/Value/Color.php b/lib/Sabberworm/CSS/Value/Color.php index 60637c13..a5d8f70d 100644 --- a/lib/Sabberworm/CSS/Value/Color.php +++ b/lib/Sabberworm/CSS/Value/Color.php @@ -21,7 +21,8 @@ public static function parse(ParserState $oParserState) if ($oParserState->strlen($sValue) === 3) { $sValue = $sValue[0] . $sValue[0] . $sValue[1] . $sValue[1] . $sValue[2] . $sValue[2]; } elseif ($oParserState->strlen($sValue) === 4) { - $sValue = $sValue[0] . $sValue[0] . $sValue[1] . $sValue[1] . $sValue[2] . $sValue[2] . $sValue[3] . $sValue[3]; + $sValue = $sValue[0] . $sValue[0] . $sValue[1] . $sValue[1] . $sValue[2] . $sValue[2] . $sValue[3] + . $sValue[3]; } if ($oParserState->strlen($sValue) === 8) { @@ -29,7 +30,12 @@ public static function parse(ParserState $oParserState) 'r' => new Size(intval($sValue[0] . $sValue[1], 16), null, true, $oParserState->currentLine()), 'g' => new Size(intval($sValue[2] . $sValue[3], 16), null, true, $oParserState->currentLine()), 'b' => new Size(intval($sValue[4] . $sValue[5], 16), null, true, $oParserState->currentLine()), - 'a' => new Size(round(self::mapRange(intval($sValue[6] . $sValue[7], 16), 0, 255, 0, 1), 2), null, true, $oParserState->currentLine()) + 'a' => new Size( + round(self::mapRange(intval($sValue[6] . $sValue[7], 16), 0, 255, 0, 1), 2), + null, + true, + $oParserState->currentLine() + ) ]; } else { $aColor = [ @@ -119,7 +125,8 @@ public function render(\Sabberworm\CSS\OutputFormat $oOutputFormat) $this->aComponents['g']->getSize(), $this->aComponents['b']->getSize() ); - return '#' . (($sResult[0] == $sResult[1]) && ($sResult[2] == $sResult[3]) && ($sResult[4] == $sResult[5]) ? "$sResult[0]$sResult[2]$sResult[4]" : $sResult); + return '#' . (($sResult[0] == $sResult[1]) && ($sResult[2] == $sResult[3]) && ($sResult[4] == $sResult[5]) + ? "$sResult[0]$sResult[2]$sResult[4]" : $sResult); } return parent::render($oOutputFormat); } diff --git a/lib/Sabberworm/CSS/Value/Size.php b/lib/Sabberworm/CSS/Value/Size.php index 55a08354..a672e745 100644 --- a/lib/Sabberworm/CSS/Value/Size.php +++ b/lib/Sabberworm/CSS/Value/Size.php @@ -7,7 +7,8 @@ class Size extends PrimitiveValue { - const ABSOLUTE_SIZE_UNITS = 'px/cm/mm/mozmm/in/pt/pc/vh/vw/vmin/vmax/rem'; //vh/vw/vm(ax)/vmin/rem are absolute insofar as they don’t scale to the immediate parent (only the viewport) + // vh/vw/vm(ax)/vmin/rem are absolute insofar as they don’t scale to the immediate parent (only the viewport) + const ABSOLUTE_SIZE_UNITS = 'px/cm/mm/mozmm/in/pt/pc/vh/vw/vmin/vmax/rem'; const RELATIVE_SIZE_UNITS = '%/em/ex/ch/fr'; const NON_SIZE_UNITS = 'deg/grad/rad/s/ms/turns/Hz/kHz'; @@ -57,7 +58,9 @@ private static function getSizeUnits() { if (self::$SIZE_UNITS === null) { self::$SIZE_UNITS = []; - foreach (explode('/', Size::ABSOLUTE_SIZE_UNITS . '/' . Size::RELATIVE_SIZE_UNITS . '/' . Size::NON_SIZE_UNITS) as $val) { + foreach ( + explode('/', Size::ABSOLUTE_SIZE_UNITS . '/' . Size::RELATIVE_SIZE_UNITS . '/' . Size::NON_SIZE_UNITS) as $val + ) { $iSize = strlen($val); if (!isset(self::$SIZE_UNITS[$iSize])) { self::$SIZE_UNITS[$iSize] = []; @@ -133,7 +136,9 @@ public function render(\Sabberworm\CSS\OutputFormat $oOutputFormat) { $l = localeconv(); $sPoint = preg_quote($l['decimal_point'], '/'); - $sSize = preg_match("/[\d\.]+e[+-]?\d+/i", (string)$this->fSize) ? preg_replace("/$sPoint?0+$/", "", sprintf("%f", $this->fSize)) : $this->fSize; - return preg_replace(["/$sPoint/", "/^(-?)0\./"], ['.', '$1.'], $sSize) . ($this->sUnit === null ? '' : $this->sUnit); + $sSize = preg_match("/[\d\.]+e[+-]?\d+/i", (string)$this->fSize) + ? preg_replace("/$sPoint?0+$/", "", sprintf("%f", $this->fSize)) : $this->fSize; + return preg_replace(["/$sPoint/", "/^(-?)0\./"], ['.', '$1.'], $sSize) + . ($this->sUnit === null ? '' : $this->sUnit); } } diff --git a/lib/Sabberworm/CSS/Value/Value.php b/lib/Sabberworm/CSS/Value/Value.php index ec339bfe..31c6afae 100644 --- a/lib/Sabberworm/CSS/Value/Value.php +++ b/lib/Sabberworm/CSS/Value/Value.php @@ -20,7 +20,10 @@ public static function parseValue(ParserState $oParserState, $aListDelimiters = $aStack = []; $oParserState->consumeWhiteSpace(); //Build a list of delimiters and parsed values - while (!($oParserState->comes('}') || $oParserState->comes(';') || $oParserState->comes('!') || $oParserState->comes(')') || $oParserState->comes('\\'))) { + while ( + !($oParserState->comes('}') || $oParserState->comes(';') || $oParserState->comes('!') + || $oParserState->comes(')') || $oParserState->comes('\\')) + ) { if (count($aStack) > 0) { $bFoundDelimiter = false; foreach ($aListDelimiters as $sDelimiter) { @@ -60,7 +63,12 @@ public static function parseValue(ParserState $oParserState, $aListDelimiters = } } if (!isset($aStack[0])) { - throw new UnexpectedTokenException(" {$oParserState->peek()} ", $oParserState->peek(1, -1) . $oParserState->peek(2), 'literal', $oParserState->currentLine()); + throw new UnexpectedTokenException( + " {$oParserState->peek()} ", + $oParserState->peek(1, -1) . $oParserState->peek(2), + 'literal', + $oParserState->currentLine() + ); } return $aStack[0]; } @@ -83,13 +91,19 @@ public static function parsePrimitiveValue(ParserState $oParserState) { $oValue = null; $oParserState->consumeWhiteSpace(); - if (is_numeric($oParserState->peek()) || ($oParserState->comes('-.') && is_numeric($oParserState->peek(1, 2))) || (($oParserState->comes('-') || $oParserState->comes('.')) && is_numeric($oParserState->peek(1, 1)))) { + if ( + is_numeric($oParserState->peek()) || ($oParserState->comes('-.') + && is_numeric($oParserState->peek(1, 2))) || (($oParserState->comes('-') || $oParserState->comes('.')) && is_numeric($oParserState->peek(1, 1))) + ) { $oValue = Size::parse($oParserState); } elseif ($oParserState->comes('#') || $oParserState->comes('rgb', true) || $oParserState->comes('hsl', true)) { $oValue = Color::parse($oParserState); } elseif ($oParserState->comes('url', true)) { $oValue = URL::parse($oParserState); - } elseif ($oParserState->comes('calc', true) || $oParserState->comes('-webkit-calc', true) || $oParserState->comes('-moz-calc', true)) { + } elseif ( + $oParserState->comes('calc', true) || $oParserState->comes('-webkit-calc', true) + || $oParserState->comes('-moz-calc', true) + ) { $oValue = CalcFunction::parse($oParserState); } elseif ($oParserState->comes("'") || $oParserState->comes('"')) { $oValue = CSSString::parse($oParserState); diff --git a/lib/Sabberworm/CSS/Value/ValueList.php b/lib/Sabberworm/CSS/Value/ValueList.php index 6eec4e11..003631cb 100644 --- a/lib/Sabberworm/CSS/Value/ValueList.php +++ b/lib/Sabberworm/CSS/Value/ValueList.php @@ -55,6 +55,9 @@ public function __toString() */ public function render(\Sabberworm\CSS\OutputFormat $oOutputFormat) { - return $oOutputFormat->implode($oOutputFormat->spaceBeforeListArgumentSeparator($this->sSeparator) . $this->sSeparator . $oOutputFormat->spaceAfterListArgumentSeparator($this->sSeparator), $this->aComponents); + return $oOutputFormat->implode( + $oOutputFormat->spaceBeforeListArgumentSeparator($this->sSeparator) . $this->sSeparator . $oOutputFormat->spaceAfterListArgumentSeparator($this->sSeparator), + $this->aComponents + ); } } diff --git a/tests/Sabberworm/CSS/OutputFormatTest.php b/tests/Sabberworm/CSS/OutputFormatTest.php index 45cab3f8..d4ce94c1 100644 --- a/tests/Sabberworm/CSS/OutputFormatTest.php +++ b/tests/Sabberworm/CSS/OutputFormatTest.php @@ -38,13 +38,19 @@ protected function setUp() public function testPlain() { - $this->assertSame('.main, .test {font: italic normal bold 16px/1.2 "Helvetica",Verdana,sans-serif;background: white;} -@media screen {.main {background-size: 100% 100%;font-size: 1.3em;background-color: #fff;}}', $this->oDocument->render()); + $this->assertSame( + '.main, .test {font: italic normal bold 16px/1.2 "Helvetica",Verdana,sans-serif;background: white;} +@media screen {.main {background-size: 100% 100%;font-size: 1.3em;background-color: #fff;}}', + $this->oDocument->render() + ); } public function testCompact() { - $this->assertSame('.main,.test{font:italic normal bold 16px/1.2 "Helvetica",Verdana,sans-serif;background:white;}@media screen{.main{background-size:100% 100%;font-size:1.3em;background-color:#fff;}}', $this->oDocument->render(OutputFormat::createCompact())); + $this->assertSame( + '.main,.test{font:italic normal bold 16px/1.2 "Helvetica",Verdana,sans-serif;background:white;}@media screen{.main{background-size:100% 100%;font-size:1.3em;background-color:#fff;}}', + $this->oDocument->render(OutputFormat::createCompact()) + ); } public function testPretty() @@ -55,21 +61,30 @@ public function testPretty() public function testSpaceAfterListArgumentSeparator() { - $this->assertSame('.main, .test {font: italic normal bold 16px/ 1.2 "Helvetica", Verdana, sans-serif;background: white;} -@media screen {.main {background-size: 100% 100%;font-size: 1.3em;background-color: #fff;}}', $this->oDocument->render(OutputFormat::create()->setSpaceAfterListArgumentSeparator(" "))); + $this->assertSame( + '.main, .test {font: italic normal bold 16px/ 1.2 "Helvetica", Verdana, sans-serif;background: white;} +@media screen {.main {background-size: 100% 100%;font-size: 1.3em;background-color: #fff;}}', + $this->oDocument->render(OutputFormat::create()->setSpaceAfterListArgumentSeparator(" ")) + ); } public function testSpaceAfterListArgumentSeparatorComplex() { - $this->assertSame('.main, .test {font: italic normal bold 16px/1.2 "Helvetica", Verdana, sans-serif;background: white;} -@media screen {.main {background-size: 100% 100%;font-size: 1.3em;background-color: #fff;}}', $this->oDocument->render(OutputFormat::create()->setSpaceAfterListArgumentSeparator(['default' => ' ', ',' => "\t", '/' => '', ' ' => '']))); + $this->assertSame( + '.main, .test {font: italic normal bold 16px/1.2 "Helvetica", Verdana, sans-serif;background: white;} +@media screen {.main {background-size: 100% 100%;font-size: 1.3em;background-color: #fff;}}', + $this->oDocument->render(OutputFormat::create()->setSpaceAfterListArgumentSeparator(['default' => ' ', ',' => "\t", '/' => '', ' ' => ''])) + ); } public function testSpaceAfterSelectorSeparator() { - $this->assertSame('.main, + $this->assertSame( + '.main, .test {font: italic normal bold 16px/1.2 "Helvetica",Verdana,sans-serif;background: white;} -@media screen {.main {background-size: 100% 100%;font-size: 1.3em;background-color: #fff;}}', $this->oDocument->render(OutputFormat::create()->setSpaceAfterSelectorSeparator("\n"))); +@media screen {.main {background-size: 100% 100%;font-size: 1.3em;background-color: #fff;}}', + $this->oDocument->render(OutputFormat::create()->setSpaceAfterSelectorSeparator("\n")) + ); } public function testStringQuotingType() @@ -138,7 +153,10 @@ public function testSpaceBoth() public function testSpaceBetweenBlocks() { - $this->assertSame('.main, .test {font: italic normal bold 16px/1.2 "Helvetica",Verdana,sans-serif;background: white;}@media screen {.main {background-size: 100% 100%;font-size: 1.3em;background-color: #fff;}}', $this->oDocument->render(OutputFormat::create()->setSpaceBetweenBlocks(''))); + $this->assertSame( + '.main, .test {font: italic normal bold 16px/1.2 "Helvetica",Verdana,sans-serif;background: white;}@media screen {.main {background-size: 100% 100%;font-size: 1.3em;background-color: #fff;}}', + $this->oDocument->render(OutputFormat::create()->setSpaceBetweenBlocks('')) + ); } public function testIndentation() @@ -160,8 +178,11 @@ public function testIndentation() public function testSpaceBeforeBraces() { - $this->assertSame('.main, .test{font: italic normal bold 16px/1.2 "Helvetica",Verdana,sans-serif;background: white;} -@media screen{.main{background-size: 100% 100%;font-size: 1.3em;background-color: #fff;}}', $this->oDocument->render(OutputFormat::create()->setSpaceBeforeOpeningBrace(''))); + $this->assertSame( + '.main, .test{font: italic normal bold 16px/1.2 "Helvetica",Verdana,sans-serif;background: white;} +@media screen{.main{background-size: 100% 100%;font-size: 1.3em;background-color: #fff;}}', + $this->oDocument->render(OutputFormat::create()->setSpaceBeforeOpeningBrace('')) + ); } /** @@ -172,8 +193,11 @@ public function testIgnoreExceptionsOff() $aBlocks = $this->oDocument->getAllDeclarationBlocks(); $oFirstBlock = $aBlocks[0]; $oFirstBlock->removeSelector('.main'); - $this->assertSame('.test {font: italic normal bold 16px/1.2 "Helvetica",Verdana,sans-serif;background: white;} -@media screen {.main {background-size: 100% 100%;font-size: 1.3em;background-color: #fff;}}', $this->oDocument->render(OutputFormat::create()->setIgnoreExceptions(false))); + $this->assertSame( + '.test {font: italic normal bold 16px/1.2 "Helvetica",Verdana,sans-serif;background: white;} +@media screen {.main {background-size: 100% 100%;font-size: 1.3em;background-color: #fff;}}', + $this->oDocument->render(OutputFormat::create()->setIgnoreExceptions(false)) + ); $oFirstBlock->removeSelector('.test'); $this->oDocument->render(OutputFormat::create()->setIgnoreExceptions(false)); } @@ -184,6 +208,9 @@ public function testIgnoreExceptionsOn() $oFirstBlock = $aBlocks[0]; $oFirstBlock->removeSelector('.main'); $oFirstBlock->removeSelector('.test'); - $this->assertSame('@media screen {.main {background-size: 100% 100%;font-size: 1.3em;background-color: #fff;}}', $this->oDocument->render(OutputFormat::create()->setIgnoreExceptions(true))); + $this->assertSame( + '@media screen {.main {background-size: 100% 100%;font-size: 1.3em;background-color: #fff;}}', + $this->oDocument->render(OutputFormat::create()->setIgnoreExceptions(true)) + ); } } diff --git a/tests/Sabberworm/CSS/ParserTest.php b/tests/Sabberworm/CSS/ParserTest.php index c96abb65..4440d410 100644 --- a/tests/Sabberworm/CSS/ParserTest.php +++ b/tests/Sabberworm/CSS/ParserTest.php @@ -258,7 +258,10 @@ public function testRuleGetters() public function testSlashedValues() { $oDoc = $this->parsedStructureForFile('slashed'); - $this->assertSame('.test {font: 12px/1.5 Verdana,Arial,sans-serif;border-radius: 5px 10px 5px 10px/10px 5px 10px 5px;}', $oDoc->render()); + $this->assertSame( + '.test {font: 12px/1.5 Verdana,Arial,sans-serif;border-radius: 5px 10px 5px 10px/10px 5px 10px 5px;}', + $oDoc->render() + ); foreach ($oDoc->getAllValues(null) as $mValue) { if ($mValue instanceof Size && $mValue->isSize() && !$mValue->isRelative()) { $mValue->setSize($mValue->getSize() * 3); @@ -284,7 +287,10 @@ public function testSlashedValues() $this->assertEquals(' ', $oSpaceList1->getListSeparator()); $this->assertEquals(' ', $oSpaceList2->getListSeparator()); } - $this->assertSame('.test {font: 36px/1.5 Verdana,Arial,sans-serif;border-radius: 15px 30px 15px 30px/30px 15px 30px 15px;}', $oDoc->render()); + $this->assertSame( + '.test {font: 36px/1.5 Verdana,Arial,sans-serif;border-radius: 15px 30px 15px 30px/30px 15px 30px 15px;}', + $oDoc->render() + ); } public function testFunctionSyntax() diff --git a/tests/Sabberworm/CSS/RuleSet/LenientParsingTest.php b/tests/Sabberworm/CSS/RuleSet/LenientParsingTest.php index 0b967f3d..6e72ddb3 100644 --- a/tests/Sabberworm/CSS/RuleSet/LenientParsingTest.php +++ b/tests/Sabberworm/CSS/RuleSet/LenientParsingTest.php @@ -23,7 +23,10 @@ public function testFaultToleranceOn() $sFile = __DIR__ . '/../../../files' . DIRECTORY_SEPARATOR . "-fault-tolerance.css"; $oParser = new Parser(file_get_contents($sFile), Settings::create()->withLenientParsing(true)); $oResult = $oParser->parse(); - $this->assertSame('.test1 {}' . "\n" . '.test2 {hello: 2.2;hello: 2000000000000.2;}' . "\n" . '#test {}' . "\n" . '#test2 {help: none;}', $oResult->render()); + $this->assertSame( + '.test1 {}' . "\n" . '.test2 {hello: 2.2;hello: 2000000000000.2;}' . "\n" . '#test {}' . "\n" . '#test2 {help: none;}', + $oResult->render() + ); } /** @@ -68,7 +71,10 @@ public function testLocaleTrap() $sFile = __DIR__ . '/../../../files' . DIRECTORY_SEPARATOR . "-fault-tolerance.css"; $oParser = new Parser(file_get_contents($sFile), Settings::create()->withLenientParsing(true)); $oResult = $oParser->parse(); - $this->assertSame('.test1 {}' . "\n" . '.test2 {hello: 2.2;hello: 2000000000000.2;}' . "\n" . '#test {}' . "\n" . '#test2 {help: none;}', $oResult->render()); + $this->assertSame( + '.test1 {}' . "\n" . '.test2 {hello: 2.2;hello: 2000000000000.2;}' . "\n" . '#test {}' . "\n" . '#test2 {help: none;}', + $oResult->render() + ); } public function testCaseInsensitivity()