Skip to content

Commit d60b996

Browse files
committed
Order of rules will now be preserved in most cases when expanding shorthands
1 parent 8304866 commit d60b996

File tree

4 files changed

+33
-12
lines changed

4 files changed

+33
-12
lines changed

lib/Sabberworm/CSS/Parsing/ParserState.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ public function currentLine() {
4444
return $this->iLineNo;
4545
}
4646

47+
public function currentColumn() {
48+
return $this->iCurrentPosition;
49+
}
50+
4751
public function getSettings() {
4852
return $this->oParserSettings;
4953
}

lib/Sabberworm/CSS/Rule/Rule.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,22 @@ class Rule implements Renderable, Commentable {
1919
private $bIsImportant;
2020
private $aIeHack;
2121
protected $iLineNo;
22+
protected $iColNo;
2223
protected $aComments;
2324

24-
public function __construct($sRule, $iLineNo = 0) {
25+
public function __construct($sRule, $iLineNo = 0, $iColNo = 0) {
2526
$this->sRule = $sRule;
2627
$this->mValue = null;
2728
$this->bIsImportant = false;
2829
$this->aIeHack = array();
2930
$this->iLineNo = $iLineNo;
31+
$this->iColNo = $iColNo;
3032
$this->aComments = array();
3133
}
3234

3335
public static function parse(ParserState $oParserState) {
3436
$aComments = $oParserState->consumeWhiteSpace();
35-
$oRule = new Rule($oParserState->parseIdentifier(!$oParserState->comes("--")), $oParserState->currentLine());
37+
$oRule = new Rule($oParserState->parseIdentifier(!$oParserState->comes("--")), $oParserState->currentLine(), $oParserState->currentColumn());
3638
$oRule->setComments($aComments);
3739
$oRule->addComments($oParserState->consumeWhiteSpace());
3840
$oParserState->consume(':');
@@ -75,6 +77,13 @@ public function getLineNo() {
7577
return $this->iLineNo;
7678
}
7779

80+
/**
81+
* @return int
82+
*/
83+
public function getColNo() {
84+
return $this->iColNo;
85+
}
86+
7887
public function setRule($sRule) {
7988
$this->sRule = $sRule;
8089
}

lib/Sabberworm/CSS/RuleSet/DeclarationBlock.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ public function expandBorderShorthand() {
189189
$sNewRuleName = $sBorderRule . "-style";
190190
}
191191
}
192-
$oNewRule = new Rule($sNewRuleName, $this->iLineNo);
192+
$oNewRule = new Rule($sNewRuleName, $oRule->getLineNo(), $oRule->getColNo());
193193
$oNewRule->setIsImportant($oRule->getIsImportant());
194194
$oNewRule->addValue(array($mNewValue));
195195
$this->addRule($oNewRule);
@@ -245,7 +245,7 @@ public function expandDimensionsShorthand() {
245245
break;
246246
}
247247
foreach (array('top', 'right', 'bottom', 'left') as $sPosition) {
248-
$oNewRule = new Rule(sprintf($sExpanded, $sPosition), $this->iLineNo);
248+
$oNewRule = new Rule(sprintf($sExpanded, $sPosition), $oRule->getLineNo(), $oRule->getColNo());
249249
$oNewRule->setIsImportant($oRule->getIsImportant());
250250
$oNewRule->addValue(${$sPosition});
251251
$this->addRule($oNewRule);
@@ -310,7 +310,7 @@ public function expandFontShorthand() {
310310
}
311311
}
312312
foreach ($aFontProperties as $sProperty => $mValue) {
313-
$oNewRule = new Rule($sProperty, $this->iLineNo);
313+
$oNewRule = new Rule($sProperty, $oRule->getLineNo(), $oRule->getColNo());
314314
$oNewRule->addValue($mValue);
315315
$oNewRule->setIsImportant($oRule->getIsImportant());
316316
$this->addRule($oNewRule);
@@ -344,7 +344,7 @@ public function expandBackgroundShorthand() {
344344
}
345345
if (count($aValues) == 1 && $aValues[0] == 'inherit') {
346346
foreach ($aBgProperties as $sProperty => $mValue) {
347-
$oNewRule = new Rule($sProperty, $this->iLineNo);
347+
$oNewRule = new Rule($sProperty, $oRule->getLineNo(), $oRule->getColNo());
348348
$oNewRule->addValue('inherit');
349349
$oNewRule->setIsImportant($oRule->getIsImportant());
350350
$this->addRule($oNewRule);
@@ -378,7 +378,7 @@ public function expandBackgroundShorthand() {
378378
}
379379
}
380380
foreach ($aBgProperties as $sProperty => $mValue) {
381-
$oNewRule = new Rule($sProperty, $this->iLineNo);
381+
$oNewRule = new Rule($sProperty, $oRule->getLineNo(), $oRule->getColNo());
382382
$oNewRule->setIsImportant($oRule->getIsImportant());
383383
$oNewRule->addValue($mValue);
384384
$this->addRule($oNewRule);
@@ -414,7 +414,7 @@ public function expandListStyleShorthand() {
414414
}
415415
if (count($aValues) == 1 && $aValues[0] == 'inherit') {
416416
foreach ($aListProperties as $sProperty => $mValue) {
417-
$oNewRule = new Rule($sProperty, $this->iLineNo);
417+
$oNewRule = new Rule($sProperty, $oRule->getLineNo(), $oRule->getColNo());
418418
$oNewRule->addValue('inherit');
419419
$oNewRule->setIsImportant($oRule->getIsImportant());
420420
$this->addRule($oNewRule);
@@ -435,7 +435,7 @@ public function expandListStyleShorthand() {
435435
}
436436
}
437437
foreach ($aListProperties as $sProperty => $mValue) {
438-
$oNewRule = new Rule($sProperty, $this->iLineNo);
438+
$oNewRule = new Rule($sProperty, $oRule->getLineNo(), $oRule->getColNo());
439439
$oNewRule->setIsImportant($oRule->getIsImportant());
440440
$oNewRule->addValue($mValue);
441441
$this->addRule($oNewRule);
@@ -465,7 +465,7 @@ public function createShorthandProperties(array $aProperties, $sShorthand) {
465465
}
466466
}
467467
if (count($aNewValues)) {
468-
$oNewRule = new Rule($sShorthand, $this->iLineNo);
468+
$oNewRule = new Rule($sShorthand, $oRule->getLineNo(), $oRule->getColNo());
469469
foreach ($aNewValues as $mValue) {
470470
$oNewRule->addValue($mValue);
471471
}
@@ -538,7 +538,7 @@ public function createDimensionsShorthand() {
538538
}
539539
$aValues[$sPosition] = $aRuleValues;
540540
}
541-
$oNewRule = new Rule($sProperty, $this->iLineNo);
541+
$oNewRule = new Rule($sProperty, $oRule->getLineNo(), $oRule->getColNo());
542542
if ((string) $aValues['left'][0] == (string) $aValues['right'][0]) {
543543
if ((string) $aValues['top'][0] == (string) $aValues['bottom'][0]) {
544544
if ((string) $aValues['top'][0] == (string) $aValues['left'][0]) {
@@ -583,7 +583,9 @@ public function createFontShorthand() {
583583
if (!isset($aRules['font-size']) || !isset($aRules['font-family'])) {
584584
return;
585585
}
586-
$oNewRule = new Rule('font', $this->iLineNo);
586+
$oOldRule = isset($aRules['font-size']) ? $aRules['font-size'] : $aRules['font-family'];
587+
$oNewRule = new Rule('font', $oOldRule->getLineNo(), $oOldRule->getColNo());
588+
unset($oOldRule);
587589
foreach (array('font-style', 'font-variant', 'font-weight') as $sProperty) {
588590
if (isset($aRules[$sProperty])) {
589591
$oRule = $aRules[$sProperty];

lib/Sabberworm/CSS/RuleSet/RuleSet.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,12 @@ public function getRules($mRule = null) {
102102
$aResult = array_merge($aResult, $aRules);
103103
}
104104
}
105+
usort($aResult, function (Rule $first, Rule $second) {
106+
if ($first->getLineNo() === $second->getLineNo()) {
107+
return $first->getColNo() - $second->getColNo();
108+
}
109+
return $first->getLineNo() - $second->getLineNo();
110+
});
105111
return $aResult;
106112
}
107113

0 commit comments

Comments
 (0)