diff --git a/CHANGELOG.md b/CHANGELOG.md index 113ffb4c..9ca3c21c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,6 +26,7 @@ Please also have a look at our ### Removed +- Remove expansion of shorthand properties (#838) - Remove `Parser::setCharset/getCharset` (#808) - Remove `Rule::getValues()` (#582) - Remove `Rule::setValues()` (#562) diff --git a/src/CSSList/Document.php b/src/CSSList/Document.php index d577ffac..8d855409 100644 --- a/src/CSSList/Document.php +++ b/src/CSSList/Document.php @@ -111,30 +111,6 @@ public function getSelectorsBySpecificity($sSpecificitySearch = null): array return $aResult; } - /** - * Expands all shorthand properties to their long value. - * - * @deprecated since 8.7.0, will be removed without substitution in version 9.0 in #511 - */ - public function expandShorthands(): void - { - foreach ($this->getAllDeclarationBlocks() as $oDeclaration) { - $oDeclaration->expandShorthands(); - } - } - - /** - * Create shorthands properties whenever possible. - * - * @deprecated since 8.7.0, will be removed without substitution in version 9.0 in #511 - */ - public function createShorthands(): void - { - foreach ($this->getAllDeclarationBlocks() as $oDeclaration) { - $oDeclaration->createShorthands(); - } - } - /** * Overrides `render()` to make format argument optional. * diff --git a/src/RuleSet/DeclarationBlock.php b/src/RuleSet/DeclarationBlock.php index 76ead48d..72a37304 100644 --- a/src/RuleSet/DeclarationBlock.php +++ b/src/RuleSet/DeclarationBlock.php @@ -13,12 +13,6 @@ use Sabberworm\CSS\Parsing\UnexpectedTokenException; use Sabberworm\CSS\Property\KeyframeSelector; use Sabberworm\CSS\Property\Selector; -use Sabberworm\CSS\Rule\Rule; -use Sabberworm\CSS\Value\Color; -use Sabberworm\CSS\Value\RuleValueList; -use Sabberworm\CSS\Value\Size; -use Sabberworm\CSS\Value\URL; -use Sabberworm\CSS\Value\Value; /** * This class represents a `RuleSet` constrained by a `Selector`. @@ -154,627 +148,6 @@ public function getSelectors() return $this->aSelectors; } - /** - * Splits shorthand declarations (e.g. `margin` or `font`) into their constituent parts. - * - * @deprecated since 8.7.0, will be removed without substitution in version 9.0 in #511 - */ - public function expandShorthands(): void - { - // border must be expanded before dimensions - $this->expandBorderShorthand(); - $this->expandDimensionsShorthand(); - $this->expandFontShorthand(); - $this->expandBackgroundShorthand(); - $this->expandListStyleShorthand(); - } - - /** - * Creates shorthand declarations (e.g. `margin` or `font`) whenever possible. - * - * @deprecated since 8.7.0, will be removed without substitution in version 9.0 in #511 - */ - public function createShorthands(): void - { - $this->createBackgroundShorthand(); - $this->createDimensionsShorthand(); - // border must be shortened after dimensions - $this->createBorderShorthand(); - $this->createFontShorthand(); - $this->createListStyleShorthand(); - } - - /** - * Splits shorthand border declarations (e.g. `border: 1px red;`). - * - * Additional splitting happens in expandDimensionsShorthand. - * - * Multiple borders are not yet supported as of 3. - * - * @deprecated since 8.7.0, will be removed without substitution in version 9.0 in #511 - */ - public function expandBorderShorthand(): void - { - $aBorderRules = [ - 'border', - 'border-left', - 'border-right', - 'border-top', - 'border-bottom', - ]; - $aBorderSizes = [ - 'thin', - 'medium', - 'thick', - ]; - $aRules = $this->getRulesAssoc(); - foreach ($aBorderRules as $sBorderRule) { - if (!isset($aRules[$sBorderRule])) { - continue; - } - $oRule = $aRules[$sBorderRule]; - $mRuleValue = $oRule->getValue(); - $aValues = []; - if (!$mRuleValue instanceof RuleValueList) { - $aValues[] = $mRuleValue; - } else { - $aValues = $mRuleValue->getListComponents(); - } - foreach ($aValues as $mValue) { - if ($mValue instanceof Value) { - $mNewValue = clone $mValue; - } else { - $mNewValue = $mValue; - } - if ($mValue instanceof Size) { - $sNewRuleName = $sBorderRule . '-width'; - } elseif ($mValue instanceof Color) { - $sNewRuleName = $sBorderRule . '-color'; - } else { - if (\in_array($mValue, $aBorderSizes, true)) { - $sNewRuleName = $sBorderRule . '-width'; - } else { - $sNewRuleName = $sBorderRule . '-style'; - } - } - $oNewRule = new Rule($sNewRuleName, $oRule->getLineNo(), $oRule->getColNo()); - $oNewRule->setIsImportant($oRule->getIsImportant()); - $oNewRule->addValue([$mNewValue]); - $this->addRule($oNewRule); - } - $this->removeRule($sBorderRule); - } - } - - /** - * Splits shorthand dimensional declarations (e.g. `margin: 0px auto;`) - * into their constituent parts. - * - * Handles `margin`, `padding`, `border-color`, `border-style` and `border-width`. - * - * @deprecated since 8.7.0, will be removed without substitution in version 9.0 in #511 - */ - public function expandDimensionsShorthand(): void - { - $aExpansions = [ - 'margin' => 'margin-%s', - 'padding' => 'padding-%s', - 'border-color' => 'border-%s-color', - 'border-style' => 'border-%s-style', - 'border-width' => 'border-%s-width', - ]; - $aRules = $this->getRulesAssoc(); - foreach ($aExpansions as $sProperty => $sExpanded) { - if (!isset($aRules[$sProperty])) { - continue; - } - $oRule = $aRules[$sProperty]; - $mRuleValue = $oRule->getValue(); - $aValues = []; - if (!$mRuleValue instanceof RuleValueList) { - $aValues[] = $mRuleValue; - } else { - $aValues = $mRuleValue->getListComponents(); - } - $top = $right = $bottom = $left = null; - switch (\count($aValues)) { - case 1: - $top = $right = $bottom = $left = $aValues[0]; - break; - case 2: - $top = $bottom = $aValues[0]; - $left = $right = $aValues[1]; - break; - case 3: - $top = $aValues[0]; - $left = $right = $aValues[1]; - $bottom = $aValues[2]; - break; - case 4: - $top = $aValues[0]; - $right = $aValues[1]; - $bottom = $aValues[2]; - $left = $aValues[3]; - break; - } - foreach (['top', 'right', 'bottom', 'left'] as $sPosition) { - $oNewRule = new Rule(\sprintf($sExpanded, $sPosition), $oRule->getLineNo(), $oRule->getColNo()); - $oNewRule->setIsImportant($oRule->getIsImportant()); - $oNewRule->addValue(${$sPosition}); - $this->addRule($oNewRule); - } - $this->removeRule($sProperty); - } - } - - /** - * Converts shorthand font declarations - * (e.g. `font: 300 italic 11px/14px verdana, helvetica, sans-serif;`) - * into their constituent parts. - * - * @deprecated since 8.7.0, will be removed without substitution in version 9.0 in #511 - */ - public function expandFontShorthand(): void - { - $aRules = $this->getRulesAssoc(); - if (!isset($aRules['font'])) { - return; - } - $oRule = $aRules['font']; - // reset properties to 'normal' per http://www.w3.org/TR/21/fonts.html#font-shorthand - $aFontProperties = [ - 'font-style' => 'normal', - 'font-variant' => 'normal', - 'font-weight' => 'normal', - 'font-size' => 'normal', - 'line-height' => 'normal', - ]; - $mRuleValue = $oRule->getValue(); - $aValues = []; - if (!$mRuleValue instanceof RuleValueList) { - $aValues[] = $mRuleValue; - } else { - $aValues = $mRuleValue->getListComponents(); - } - foreach ($aValues as $mValue) { - if (!$mValue instanceof Value) { - $mValue = \mb_strtolower($mValue); - } - if (\in_array($mValue, ['normal', 'inherit'], true)) { - foreach (['font-style', 'font-weight', 'font-variant'] as $sProperty) { - if (!isset($aFontProperties[$sProperty])) { - $aFontProperties[$sProperty] = $mValue; - } - } - } elseif (\in_array($mValue, ['italic', 'oblique'], true)) { - $aFontProperties['font-style'] = $mValue; - } elseif ($mValue == 'small-caps') { - $aFontProperties['font-variant'] = $mValue; - } elseif ( - \in_array($mValue, ['bold', 'bolder', 'lighter'], true) - || ($mValue instanceof Size && \in_array($mValue->getSize(), \range(100.0, 900.0, 100.0), true)) - ) { - $aFontProperties['font-weight'] = $mValue; - } elseif ($mValue instanceof RuleValueList && $mValue->getListSeparator() == '/') { - [$oSize, $oHeight] = $mValue->getListComponents(); - $aFontProperties['font-size'] = $oSize; - $aFontProperties['line-height'] = $oHeight; - } elseif ($mValue instanceof Size && $mValue->getUnit() !== null) { - $aFontProperties['font-size'] = $mValue; - } else { - $aFontProperties['font-family'] = $mValue; - } - } - foreach ($aFontProperties as $sProperty => $mValue) { - $oNewRule = new Rule($sProperty, $oRule->getLineNo(), $oRule->getColNo()); - $oNewRule->addValue($mValue); - $oNewRule->setIsImportant($oRule->getIsImportant()); - $this->addRule($oNewRule); - } - $this->removeRule('font'); - } - - /** - * Converts shorthand background declarations - * (e.g. `background: url("chess.png") gray 50% repeat fixed;`) - * into their constituent parts. - * - * @see http://www.w3.org/TR/21/colors.html#propdef-background - * - * @deprecated since 8.7.0, will be removed without substitution in version 9.0 in #511 - */ - public function expandBackgroundShorthand(): void - { - $aRules = $this->getRulesAssoc(); - if (!isset($aRules['background'])) { - return; - } - $oRule = $aRules['background']; - $aBgProperties = [ - 'background-color' => ['transparent'], - 'background-image' => ['none'], - 'background-repeat' => ['repeat'], - 'background-attachment' => ['scroll'], - 'background-position' => [ - new Size(0, '%', false, $this->lineNumber), - new Size(0, '%', false, $this->lineNumber), - ], - ]; - $mRuleValue = $oRule->getValue(); - $aValues = []; - if (!$mRuleValue instanceof RuleValueList) { - $aValues[] = $mRuleValue; - } else { - $aValues = $mRuleValue->getListComponents(); - } - if (\count($aValues) == 1 && $aValues[0] == 'inherit') { - foreach ($aBgProperties as $sProperty => $mValue) { - $oNewRule = new Rule($sProperty, $oRule->getLineNo(), $oRule->getColNo()); - $oNewRule->addValue('inherit'); - $oNewRule->setIsImportant($oRule->getIsImportant()); - $this->addRule($oNewRule); - } - $this->removeRule('background'); - return; - } - $iNumBgPos = 0; - foreach ($aValues as $mValue) { - if (!$mValue instanceof Value) { - $mValue = \mb_strtolower($mValue); - } - if ($mValue instanceof URL) { - $aBgProperties['background-image'] = $mValue; - } elseif ($mValue instanceof Color) { - $aBgProperties['background-color'] = $mValue; - } elseif (\in_array($mValue, ['scroll', 'fixed'], true)) { - $aBgProperties['background-attachment'] = $mValue; - } elseif (\in_array($mValue, ['repeat', 'no-repeat', 'repeat-x', 'repeat-y'], true)) { - $aBgProperties['background-repeat'] = $mValue; - } elseif ( - \in_array($mValue, ['left', 'center', 'right', 'top', 'bottom'], true) - || $mValue instanceof Size - ) { - if ($iNumBgPos == 0) { - $aBgProperties['background-position'][0] = $mValue; - $aBgProperties['background-position'][1] = 'center'; - } else { - $aBgProperties['background-position'][$iNumBgPos] = $mValue; - } - $iNumBgPos++; - } - } - foreach ($aBgProperties as $sProperty => $mValue) { - $oNewRule = new Rule($sProperty, $oRule->getLineNo(), $oRule->getColNo()); - $oNewRule->setIsImportant($oRule->getIsImportant()); - $oNewRule->addValue($mValue); - $this->addRule($oNewRule); - } - $this->removeRule('background'); - } - - /** - * @deprecated since 8.7.0, will be removed without substitution in version 9.0 in #511 - */ - public function expandListStyleShorthand(): void - { - $aListProperties = [ - 'list-style-type' => 'disc', - 'list-style-position' => 'outside', - 'list-style-image' => 'none', - ]; - $aListStyleTypes = [ - 'none', - 'disc', - 'circle', - 'square', - 'decimal-leading-zero', - 'decimal', - 'lower-roman', - 'upper-roman', - 'lower-greek', - 'lower-alpha', - 'lower-latin', - 'upper-alpha', - 'upper-latin', - 'hebrew', - 'armenian', - 'georgian', - 'cjk-ideographic', - 'hiragana', - 'hira-gana-iroha', - 'katakana-iroha', - 'katakana', - ]; - $aListStylePositions = [ - 'inside', - 'outside', - ]; - $aRules = $this->getRulesAssoc(); - if (!isset($aRules['list-style'])) { - return; - } - $oRule = $aRules['list-style']; - $mRuleValue = $oRule->getValue(); - $aValues = []; - if (!$mRuleValue instanceof RuleValueList) { - $aValues[] = $mRuleValue; - } else { - $aValues = $mRuleValue->getListComponents(); - } - if (\count($aValues) == 1 && $aValues[0] == 'inherit') { - foreach ($aListProperties as $sProperty => $mValue) { - $oNewRule = new Rule($sProperty, $oRule->getLineNo(), $oRule->getColNo()); - $oNewRule->addValue('inherit'); - $oNewRule->setIsImportant($oRule->getIsImportant()); - $this->addRule($oNewRule); - } - $this->removeRule('list-style'); - return; - } - foreach ($aValues as $mValue) { - if (!$mValue instanceof Value) { - $mValue = \mb_strtolower($mValue); - } - if ($mValue instanceof Url) { - $aListProperties['list-style-image'] = $mValue; - } elseif (\in_array($mValue, $aListStyleTypes, true)) { - $aListProperties['list-style-types'] = $mValue; - } elseif (\in_array($mValue, $aListStylePositions, true)) { - $aListProperties['list-style-position'] = $mValue; - } - } - foreach ($aListProperties as $sProperty => $mValue) { - $oNewRule = new Rule($sProperty, $oRule->getLineNo(), $oRule->getColNo()); - $oNewRule->setIsImportant($oRule->getIsImportant()); - $oNewRule->addValue($mValue); - $this->addRule($oNewRule); - } - $this->removeRule('list-style'); - } - - /** - * @param array $aProperties - * @param string $sShorthand - * - * @deprecated since 8.7.0, will be removed without substitution in version 9.0 in #511 - */ - public function createShorthandProperties(array $aProperties, $sShorthand): void - { - $aRules = $this->getRulesAssoc(); - $oRule = null; - $aNewValues = []; - foreach ($aProperties as $sProperty) { - if (!isset($aRules[$sProperty])) { - continue; - } - $oRule = $aRules[$sProperty]; - if (!$oRule->getIsImportant()) { - $mRuleValue = $oRule->getValue(); - $aValues = []; - if (!$mRuleValue instanceof RuleValueList) { - $aValues[] = $mRuleValue; - } else { - $aValues = $mRuleValue->getListComponents(); - } - foreach ($aValues as $mValue) { - $aNewValues[] = $mValue; - } - $this->removeRule($sProperty); - } - } - if ($aNewValues !== [] && $oRule instanceof Rule) { - $oNewRule = new Rule($sShorthand, $oRule->getLineNo(), $oRule->getColNo()); - foreach ($aNewValues as $mValue) { - $oNewRule->addValue($mValue); - } - $this->addRule($oNewRule); - } - } - - /** - * @deprecated since 8.7.0, will be removed without substitution in version 9.0 in #511 - */ - public function createBackgroundShorthand(): void - { - $aProperties = [ - 'background-color', - 'background-image', - 'background-repeat', - 'background-position', - 'background-attachment', - ]; - $this->createShorthandProperties($aProperties, 'background'); - } - - /** - * @deprecated since 8.7.0, will be removed without substitution in version 9.0 in #511 - */ - public function createListStyleShorthand(): void - { - $aProperties = [ - 'list-style-type', - 'list-style-position', - 'list-style-image', - ]; - $this->createShorthandProperties($aProperties, 'list-style'); - } - - /** - * Combines `border-color`, `border-style` and `border-width` into `border`. - * - * Should be run after `create_dimensions_shorthand`! - * - * @deprecated since 8.7.0, will be removed without substitution in version 9.0 in #511 - */ - public function createBorderShorthand(): void - { - $aProperties = [ - 'border-width', - 'border-style', - 'border-color', - ]; - $this->createShorthandProperties($aProperties, 'border'); - } - - /** - * Looks for long format CSS dimensional properties - * (margin, padding, border-color, border-style and border-width) - * and converts them into shorthand CSS properties. - * - * @deprecated since 8.7.0, will be removed without substitution in version 9.0 in #511 - */ - public function createDimensionsShorthand(): void - { - $aPositions = ['top', 'right', 'bottom', 'left']; - $aExpansions = [ - 'margin' => 'margin-%s', - 'padding' => 'padding-%s', - 'border-color' => 'border-%s-color', - 'border-style' => 'border-%s-style', - 'border-width' => 'border-%s-width', - ]; - $aRules = $this->getRulesAssoc(); - foreach ($aExpansions as $sProperty => $sExpanded) { - $aFoldable = []; - foreach ($aRules as $sRuleName => $oRule) { - foreach ($aPositions as $sPosition) { - if ($sRuleName == \sprintf($sExpanded, $sPosition)) { - $aFoldable[$sRuleName] = $oRule; - } - } - } - // All four dimensions must be present - if (\count($aFoldable) == 4) { - $aValues = []; - foreach ($aPositions as $sPosition) { - $oRule = $aRules[\sprintf($sExpanded, $sPosition)]; - $mRuleValue = $oRule->getValue(); - $aRuleValues = []; - if (!$mRuleValue instanceof RuleValueList) { - $aRuleValues[] = $mRuleValue; - } else { - $aRuleValues = $mRuleValue->getListComponents(); - } - $aValues[$sPosition] = $aRuleValues; - } - $oNewRule = new Rule($sProperty, $oRule->getLineNo(), $oRule->getColNo()); - if ((string) $aValues['left'][0] == (string) $aValues['right'][0]) { - if ((string) $aValues['top'][0] == (string) $aValues['bottom'][0]) { - if ((string) $aValues['top'][0] == (string) $aValues['left'][0]) { - // All 4 sides are equal - $oNewRule->addValue($aValues['top']); - } else { - // Top and bottom are equal, left and right are equal - $oNewRule->addValue($aValues['top']); - $oNewRule->addValue($aValues['left']); - } - } else { - // Only left and right are equal - $oNewRule->addValue($aValues['top']); - $oNewRule->addValue($aValues['left']); - $oNewRule->addValue($aValues['bottom']); - } - } else { - // No sides are equal - $oNewRule->addValue($aValues['top']); - $oNewRule->addValue($aValues['left']); - $oNewRule->addValue($aValues['bottom']); - $oNewRule->addValue($aValues['right']); - } - $this->addRule($oNewRule); - foreach ($aPositions as $sPosition) { - $this->removeRule(\sprintf($sExpanded, $sPosition)); - } - } - } - } - - /** - * Looks for long format CSS font properties (e.g. `font-weight`) and - * tries to convert them into a shorthand CSS `font` property. - * - * At least `font-size` AND `font-family` must be present in order to create a shorthand declaration. - * - * @deprecated since 8.7.0, will be removed without substitution in version 9.0 in #511 - */ - public function createFontShorthand(): void - { - $aFontProperties = [ - 'font-style', - 'font-variant', - 'font-weight', - 'font-size', - 'line-height', - 'font-family', - ]; - $aRules = $this->getRulesAssoc(); - if (!isset($aRules['font-size']) || !isset($aRules['font-family'])) { - return; - } - $oOldRule = $aRules['font-size'] ?? $aRules['font-family']; - $oNewRule = new Rule('font', $oOldRule->getLineNo(), $oOldRule->getColNo()); - unset($oOldRule); - foreach (['font-style', 'font-variant', 'font-weight'] as $sProperty) { - if (isset($aRules[$sProperty])) { - $oRule = $aRules[$sProperty]; - $mRuleValue = $oRule->getValue(); - $aValues = []; - if (!$mRuleValue instanceof RuleValueList) { - $aValues[] = $mRuleValue; - } else { - $aValues = $mRuleValue->getListComponents(); - } - if ($aValues[0] !== 'normal') { - $oNewRule->addValue($aValues[0]); - } - } - } - // Get the font-size value - $oRule = $aRules['font-size']; - $mRuleValue = $oRule->getValue(); - $aFSValues = []; - if (!$mRuleValue instanceof RuleValueList) { - $aFSValues[] = $mRuleValue; - } else { - $aFSValues = $mRuleValue->getListComponents(); - } - // But wait to know if we have line-height to add it - if (isset($aRules['line-height'])) { - $oRule = $aRules['line-height']; - $mRuleValue = $oRule->getValue(); - $aLHValues = []; - if (!$mRuleValue instanceof RuleValueList) { - $aLHValues[] = $mRuleValue; - } else { - $aLHValues = $mRuleValue->getListComponents(); - } - if ($aLHValues[0] !== 'normal') { - $val = new RuleValueList('/', $this->lineNumber); - $val->addListComponent($aFSValues[0]); - $val->addListComponent($aLHValues[0]); - $oNewRule->addValue($val); - } - } else { - $oNewRule->addValue($aFSValues[0]); - } - $oRule = $aRules['font-family']; - $mRuleValue = $oRule->getValue(); - $aFFValues = []; - if (!$mRuleValue instanceof RuleValueList) { - $aFFValues[] = $mRuleValue; - } else { - $aFFValues = $mRuleValue->getListComponents(); - } - $oFFValue = new RuleValueList(',', $this->lineNumber); - $oFFValue->setListComponents($aFFValues); - $oNewRule->addValue($oFFValue); - - $this->addRule($oNewRule); - foreach ($aFontProperties as $sProperty) { - $this->removeRule($sProperty); - } - } - /** * @throws OutputException */ diff --git a/tests/ParserTest.php b/tests/ParserTest.php index ae91bf1d..fbff74e8 100644 --- a/tests/ParserTest.php +++ b/tests/ParserTest.php @@ -492,47 +492,6 @@ public function functionSyntax(): void self::assertSame($expected, $document->render()); } - /** - * @test - */ - public function expandShorthands(): void - { - $document = self::parsedStructureForFile('expand-shorthands'); - $expected = 'body {font: italic 500 14px/1.618 "Trebuchet MS",Georgia,serif;border: 2px solid #f0f;' - . 'background: #ccc url("/images/foo.png") no-repeat left top;margin: 1em !important;' - . 'padding: 2px 6px 3px;}'; - self::assertSame($expected, $document->render()); - $document->expandShorthands(); - $expected = 'body {margin-top: 1em !important;margin-right: 1em !important;margin-bottom: 1em !important;' - . 'margin-left: 1em !important;padding-top: 2px;padding-right: 6px;padding-bottom: 3px;' - . 'padding-left: 6px;border-top-color: #f0f;border-right-color: #f0f;border-bottom-color: #f0f;' - . 'border-left-color: #f0f;border-top-style: solid;border-right-style: solid;' - . 'border-bottom-style: solid;border-left-style: solid;border-top-width: 2px;' - . 'border-right-width: 2px;border-bottom-width: 2px;border-left-width: 2px;font-style: italic;' - . 'font-variant: normal;font-weight: 500;font-size: 14px;line-height: 1.618;' - . 'font-family: "Trebuchet MS",Georgia,serif;background-color: #ccc;' - . 'background-image: url("/images/foo.png");background-repeat: no-repeat;background-attachment: scroll;' - . 'background-position: left top;}'; - self::assertSame($expected, $document->render()); - } - - /** - * @test - */ - public function createShorthands(): void - { - $document = self::parsedStructureForFile('create-shorthands'); - $expected = 'body {font-size: 2em;font-family: Helvetica,Arial,sans-serif;font-weight: bold;' - . 'border-width: 2px;border-color: #999;border-style: dotted;background-color: #fff;' - . 'background-image: url("foobar.png");background-repeat: repeat-y;margin-top: 2px;margin-right: 3px;' - . 'margin-bottom: 4px;margin-left: 5px;}'; - self::assertSame($expected, $document->render()); - $document->createShorthands(); - $expected = 'body {background: #fff url("foobar.png") repeat-y;margin: 2px 5px 4px 3px;' - . 'border: 2px dotted #999;font: bold 2em Helvetica,Arial,sans-serif;}'; - self::assertSame($expected, $document->render()); - } - /** * @test */ diff --git a/tests/RuleSet/DeclarationBlockTest.php b/tests/RuleSet/DeclarationBlockTest.php index 15430ae9..2de33b96 100644 --- a/tests/RuleSet/DeclarationBlockTest.php +++ b/tests/RuleSet/DeclarationBlockTest.php @@ -16,325 +16,6 @@ */ final class DeclarationBlockTest extends TestCase { - /** - * @dataProvider expandBorderShorthandProvider - * - * @test - */ - public function expandBorderShorthand(string $sCss, string $sExpected): void - { - $parser = new Parser($sCss); - $document = $parser->parse(); - foreach ($document->getAllDeclarationBlocks() as $declarationBlock) { - $declarationBlock->expandBorderShorthand(); - } - self::assertSame(\trim((string) $document), $sExpected); - } - - /** - * @return array> - */ - public static function expandBorderShorthandProvider(): array - { - return [ - ['body{ border: 2px solid #000 }', 'body {border-width: 2px;border-style: solid;border-color: #000;}'], - ['body{ border: none }', 'body {border-style: none;}'], - ['body{ border: 2px }', 'body {border-width: 2px;}'], - ['body{ border: #f00 }', 'body {border-color: #f00;}'], - ['body{ border: 1em solid }', 'body {border-width: 1em;border-style: solid;}'], - ['body{ margin: 1em; }', 'body {margin: 1em;}'], - ]; - } - - /** - * @dataProvider expandFontShorthandProvider - * - * @test - */ - public function expandFontShorthand(string $sCss, string $sExpected): void - { - $parser = new Parser($sCss); - $document = $parser->parse(); - foreach ($document->getAllDeclarationBlocks() as $declarationBlock) { - $declarationBlock->expandFontShorthand(); - } - self::assertSame(\trim((string) $document), $sExpected); - } - - /** - * @return array> - */ - public static function expandFontShorthandProvider(): array - { - return [ - [ - 'body{ margin: 1em; }', - 'body {margin: 1em;}', - ], - [ - 'body {font: 12px serif;}', - 'body {font-style: normal;font-variant: normal;font-weight: normal;font-size: 12px;' - . 'line-height: normal;font-family: serif;}', - ], - [ - 'body {font: italic 12px serif;}', - 'body {font-style: italic;font-variant: normal;font-weight: normal;font-size: 12px;' - . 'line-height: normal;font-family: serif;}', - ], - [ - 'body {font: italic bold 12px serif;}', - 'body {font-style: italic;font-variant: normal;font-weight: bold;font-size: 12px;' - . 'line-height: normal;font-family: serif;}', - ], - [ - 'body {font: italic bold 12px/1.6 serif;}', - 'body {font-style: italic;font-variant: normal;font-weight: bold;font-size: 12px;' - . 'line-height: 1.6;font-family: serif;}', - ], - [ - 'body {font: italic small-caps bold 12px/1.6 serif;}', - 'body {font-style: italic;font-variant: small-caps;font-weight: bold;font-size: 12px;' - . 'line-height: 1.6;font-family: serif;}', - ], - ]; - } - - /** - * @dataProvider expandBackgroundShorthandProvider - * - * @test - */ - public function expandBackgroundShorthand(string $sCss, string $sExpected): void - { - $parser = new Parser($sCss); - $document = $parser->parse(); - foreach ($document->getAllDeclarationBlocks() as $declarationBlock) { - $declarationBlock->expandBackgroundShorthand(); - } - self::assertSame(\trim((string) $document), $sExpected); - } - - /** - * @return array> - */ - public static function expandBackgroundShorthandProvider(): array - { - return [ - ['body {border: 1px;}', 'body {border: 1px;}'], - [ - 'body {background: #f00;}', - 'body {background-color: #f00;background-image: none;background-repeat: repeat;' - . 'background-attachment: scroll;background-position: 0% 0%;}', - ], - [ - 'body {background: #f00 url("foobar.png");}', - 'body {background-color: #f00;background-image: url("foobar.png");background-repeat: repeat;' - . 'background-attachment: scroll;background-position: 0% 0%;}', - ], - [ - 'body {background: #f00 url("foobar.png") no-repeat;}', - 'body {background-color: #f00;background-image: url("foobar.png");background-repeat: no-repeat;' - . 'background-attachment: scroll;background-position: 0% 0%;}', - ], - [ - 'body {background: #f00 url("foobar.png") no-repeat center;}', - 'body {background-color: #f00;background-image: url("foobar.png");background-repeat: no-repeat;' - . 'background-attachment: scroll;background-position: center center;}', - ], - [ - 'body {background: #f00 url("foobar.png") no-repeat top left;}', - 'body {background-color: #f00;background-image: url("foobar.png");background-repeat: no-repeat;' - . 'background-attachment: scroll;background-position: top left;}', - ], - ]; - } - - /** - * @dataProvider expandDimensionsShorthandProvider - * - * @test - */ - public function expandDimensionsShorthand(string $sCss, string $sExpected): void - { - $parser = new Parser($sCss); - $document = $parser->parse(); - foreach ($document->getAllDeclarationBlocks() as $declarationBlock) { - $declarationBlock->expandDimensionsShorthand(); - } - self::assertSame(\trim((string) $document), $sExpected); - } - - /** - * @return array> - */ - public static function expandDimensionsShorthandProvider(): array - { - return [ - ['body {border: 1px;}', 'body {border: 1px;}'], - ['body {margin-top: 1px;}', 'body {margin-top: 1px;}'], - ['body {margin: 1em;}', 'body {margin-top: 1em;margin-right: 1em;margin-bottom: 1em;margin-left: 1em;}'], - [ - 'body {margin: 1em 2em;}', - 'body {margin-top: 1em;margin-right: 2em;margin-bottom: 1em;margin-left: 2em;}', - ], - [ - 'body {margin: 1em 2em 3em;}', - 'body {margin-top: 1em;margin-right: 2em;margin-bottom: 3em;margin-left: 2em;}', - ], - ]; - } - - /** - * @dataProvider createBorderShorthandProvider - * - * @test - */ - public function createBorderShorthand(string $sCss, string $sExpected): void - { - $parser = new Parser($sCss); - $document = $parser->parse(); - foreach ($document->getAllDeclarationBlocks() as $declarationBlock) { - $declarationBlock->createBorderShorthand(); - } - self::assertSame(\trim((string) $document), $sExpected); - } - - /** - * @return array> - */ - public static function createBorderShorthandProvider(): array - { - return [ - ['body {border-width: 2px;border-style: solid;border-color: #000;}', 'body {border: 2px solid #000;}'], - ['body {border-style: none;}', 'body {border: none;}'], - ['body {border-width: 1em;border-style: solid;}', 'body {border: 1em solid;}'], - ['body {margin: 1em;}', 'body {margin: 1em;}'], - ]; - } - - /** - * @dataProvider createFontShorthandProvider - * - * @test - */ - public function createFontShorthand(string $sCss, string $sExpected): void - { - $parser = new Parser($sCss); - $document = $parser->parse(); - foreach ($document->getAllDeclarationBlocks() as $declarationBlock) { - $declarationBlock->createFontShorthand(); - } - self::assertSame(\trim((string) $document), $sExpected); - } - - /** - * @return array> - */ - public static function createFontShorthandProvider(): array - { - return [ - ['body {font-size: 12px; font-family: serif}', 'body {font: 12px serif;}'], - ['body {font-size: 12px; font-family: serif; font-style: italic;}', 'body {font: italic 12px serif;}'], - [ - 'body {font-size: 12px; font-family: serif; font-style: italic; font-weight: bold;}', - 'body {font: italic bold 12px serif;}', - ], - [ - 'body {font-size: 12px; font-family: serif; font-style: italic; font-weight: bold; line-height: 1.6;}', - 'body {font: italic bold 12px/1.6 serif;}', - ], - [ - 'body {font-size: 12px; font-family: serif; font-style: italic; font-weight: bold; ' - . 'line-height: 1.6; font-variant: small-caps;}', - 'body {font: italic small-caps bold 12px/1.6 serif;}', - ], - ['body {margin: 1em;}', 'body {margin: 1em;}'], - ]; - } - - /** - * @dataProvider createDimensionsShorthandProvider - * - * @test - */ - public function createDimensionsShorthand(string $sCss, string $sExpected): void - { - $parser = new Parser($sCss); - $document = $parser->parse(); - foreach ($document->getAllDeclarationBlocks() as $declarationBlock) { - $declarationBlock->createDimensionsShorthand(); - } - self::assertSame(\trim((string) $document), $sExpected); - } - - /** - * @return array> - */ - public static function createDimensionsShorthandProvider(): array - { - return [ - ['body {border: 1px;}', 'body {border: 1px;}'], - ['body {margin-top: 1px;}', 'body {margin-top: 1px;}'], - ['body {margin-top: 1em; margin-right: 1em; margin-bottom: 1em; margin-left: 1em;}', 'body {margin: 1em;}'], - [ - 'body {margin-top: 1em; margin-right: 2em; margin-bottom: 1em; margin-left: 2em;}', - 'body {margin: 1em 2em;}', - ], - [ - 'body {margin-top: 1em; margin-right: 2em; margin-bottom: 3em; margin-left: 2em;}', - 'body {margin: 1em 2em 3em;}', - ], - ]; - } - - /** - * @dataProvider createBackgroundShorthandProvider - * - * @test - */ - public function createBackgroundShorthand(string $sCss, string $sExpected): void - { - $parser = new Parser($sCss); - $document = $parser->parse(); - foreach ($document->getAllDeclarationBlocks() as $declarationBlock) { - $declarationBlock->createBackgroundShorthand(); - } - self::assertSame(\trim((string) $document), $sExpected); - } - - /** - * @return array> - */ - public static function createBackgroundShorthandProvider(): array - { - return [ - ['body {border: 1px;}', 'body {border: 1px;}'], - ['body {background-color: #f00;}', 'body {background: #f00;}'], - [ - 'body {background-color: #f00;background-image: url(foobar.png);}', - 'body {background: #f00 url("foobar.png");}', - ], - [ - 'body {background-color: #f00;background-image: url(foobar.png);background-repeat: no-repeat;}', - 'body {background: #f00 url("foobar.png") no-repeat;}', - ], - [ - 'body {background-color: #f00;background-image: url(foobar.png);background-repeat: no-repeat;}', - 'body {background: #f00 url("foobar.png") no-repeat;}', - ], - [ - 'body {background-color: #f00;background-image: url(foobar.png);background-repeat: no-repeat;' - . 'background-position: center;}', - 'body {background: #f00 url("foobar.png") no-repeat center;}', - ], - [ - 'body {background-color: #f00;background-image: url(foobar.png);background-repeat: no-repeat;' - . 'background-position: top left;}', - 'body {background: #f00 url("foobar.png") no-repeat top left;}', - ], - ]; - } - /** * @test */ @@ -403,34 +84,6 @@ public function ruleInsertion(): void ); } - /** - * @test - * - * TODO: The order is different on PHP 5.6 than on PHP >= 7.0. - */ - public function orderOfElementsMatchingOriginalOrderAfterExpandingShorthands(): void - { - $sCss = '.rule{padding:5px;padding-top: 20px}'; - $parser = new Parser($sCss); - $document = $parser->parse(); - $declarationBlocks = $document->getAllDeclarationBlocks(); - - self::assertCount(1, $declarationBlocks); - - $lastDeclarationBlock = \array_pop($declarationBlocks); - $lastDeclarationBlock->expandShorthands(); - - self::assertEquals( - [ - 'padding-top' => 'padding-top: 20px;', - 'padding-right' => 'padding-right: 5px;', - 'padding-bottom' => 'padding-bottom: 5px;', - 'padding-left' => 'padding-left: 5px;', - ], - \array_map('strval', $lastDeclarationBlock->getRulesAssoc()) - ); - } - /** * @return array */ diff --git a/tests/fixtures/create-shorthands.css b/tests/fixtures/create-shorthands.css deleted file mode 100644 index 72198043..00000000 --- a/tests/fixtures/create-shorthands.css +++ /dev/null @@ -1,6 +0,0 @@ -body { - font-size: 2em; font-family: Helvetica,Arial,sans-serif; font-weight: bold; - border-width: 2px; border-color: #999; border-style: dotted; - background-color: #fff; background-image: url('foobar.png'); background-repeat: repeat-y; - margin-top: 2px; margin-right: 3px; margin-bottom: 4px; margin-left: 5px; -} diff --git a/tests/fixtures/expand-shorthands.css b/tests/fixtures/expand-shorthands.css deleted file mode 100644 index 89aab1e2..00000000 --- a/tests/fixtures/expand-shorthands.css +++ /dev/null @@ -1,7 +0,0 @@ -body { - font: italic 500 14px/1.618 "Trebuchet MS", Georgia, serif; - border: 2px solid #f0f; - background: #ccc url("/images/foo.png") no-repeat left top; - margin: 1em !important; - padding: 2px 6px 3px; -}