Skip to content

Commit cd1ff11

Browse files
authored
Merge pull request #204 from Idrinth/master
Fixes #203
2 parents 8304866 + aa32320 commit cd1ff11

File tree

5 files changed

+71
-12
lines changed

5 files changed

+71
-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: 16 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,18 @@ public function getLineNo() {
7577
return $this->iLineNo;
7678
}
7779

80+
/**
81+
* @return int
82+
*/
83+
public function getColNo() {
84+
return $this->iColNo;
85+
}
86+
87+
public function setPosition($iLine, $iColumn) {
88+
$this->iColNo = $iColumn;
89+
$this->iLineNo = $iLine;
90+
}
91+
7892
public function setRule($sRule) {
7993
$this->sRule = $sRule;
8094
}

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: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,16 @@ public function addRule(Rule $oRule, Rule $oSibling = null) {
7878
$iSiblingPos = array_search($oSibling, $this->aRules[$sRule], true);
7979
if ($iSiblingPos !== false) {
8080
$iPosition = $iSiblingPos;
81+
$oRule->setPosition($oSibling->getLineNo(), $oSibling->getColNo() - 1);
82+
}
83+
}
84+
if ($oRule->getLineNo() === 0 && $oRule->getColNo() === 0) {
85+
//this node is added manually, give it the next best line
86+
$rules = $this->getRules();
87+
$pos = count($rules);
88+
if ($pos > 0) {
89+
$last = $rules[$pos - 1];
90+
$oRule->setPosition($last->getLineNo() + 1, 0);
8191
}
8292
}
8393

@@ -102,6 +112,12 @@ public function getRules($mRule = null) {
102112
$aResult = array_merge($aResult, $aRules);
103113
}
104114
}
115+
usort($aResult, function (Rule $first, Rule $second) {
116+
if ($first->getLineNo() === $second->getLineNo()) {
117+
return $first->getColNo() - $second->getColNo();
118+
}
119+
return $first->getLineNo() - $second->getLineNo();
120+
});
105121
return $aResult;
106122
}
107123

tests/Sabberworm/CSS/RuleSet/DeclarationBlockTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,4 +264,27 @@ public function testRuleInsertion() {
264264
$this->assertSame('.wrapper {left: 16em;left: 10px;text-align: 1;text-align: left;border-bottom-width: 1px;}', $oDoc->render());
265265
}
266266

267+
public function testOrderOfElementsMatchingOriginalOrderAfterExpandingShorthands()
268+
{
269+
$sCss = '.rule{padding:5px;padding-top: 20px}';
270+
$oParser = new Parser($sCss);
271+
$oDoc = $oParser->parse();
272+
$aDocs = $oDoc->getAllDeclarationBlocks();
273+
274+
$this->assertCount(1, $aDocs);
275+
276+
$oDeclaration = array_pop($aDocs);
277+
$oDeclaration->expandShorthands();
278+
279+
$this->assertEquals(
280+
array(
281+
'padding-top' => 'padding-top: 20px;',
282+
'padding-right' => 'padding-right: 5px;',
283+
'padding-bottom' => 'padding-bottom: 5px;',
284+
'padding-left' => 'padding-left: 5px;',
285+
),
286+
array_map('strval', $oDeclaration->getRulesAssoc())
287+
);
288+
}
289+
267290
}

0 commit comments

Comments
 (0)