Skip to content
This repository was archived by the owner on Sep 10, 2022. It is now read-only.

Commit 173c7ae

Browse files
committed
store line numbers in css objects
1 parent d1470fe commit 173c7ae

22 files changed

+165
-62
lines changed

lib/Sabberworm/CSS/CSSList/AtRuleBlockList.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ class AtRuleBlockList extends CSSBlockList implements AtRule {
1212
private $sType;
1313
private $sArgs;
1414

15-
public function __construct($sType, $sArgs = '') {
16-
parent::__construct();
15+
public function __construct($sType, $sArgs = '', $iLineNum = 0) {
16+
parent::__construct($iLineNum);
1717
$this->sType = $sType;
1818
$this->sArgs = $sArgs;
1919
}

lib/Sabberworm/CSS/CSSList/CSSBlockList.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
* Most CSSLists conform to this category but some at-rules (such as @keyframes) do not.
1515
*/
1616
abstract class CSSBlockList extends CSSList {
17+
public function __construct($iLineNum = 0) {
18+
parent::__construct($iLineNum);
19+
}
20+
1721
protected function allDeclarationBlocks(&$aResult) {
1822
foreach ($this->aContents as $mContent) {
1923
if ($mContent instanceof DeclarationBlock) {

lib/Sabberworm/CSS/CSSList/CSSList.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,19 @@
1616
abstract class CSSList {
1717

1818
protected $aContents;
19+
protected $iLineNum;
1920

20-
public function __construct() {
21+
public function __construct($iLineNum = 0) {
2122
$this->aContents = array();
23+
$this->iLineNum = $iLineNum;
24+
}
25+
26+
/**
27+
* @return int
28+
*/
29+
public function getLineNum()
30+
{
31+
return $this->iLineNum;
2232
}
2333

2434
public function append($oItem) {

lib/Sabberworm/CSS/CSSList/Document.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@
66
* The root CSSList of a parsed file. Contains all top-level css contents, mostly declaration blocks, but also any @-rules encountered.
77
*/
88
class Document extends CSSBlockList {
9+
/**
10+
* Document constructor.
11+
* @param int $iLineNum
12+
*/
13+
public function __construct($iLineNum = 0)
14+
{
15+
parent::__construct($iLineNum);
16+
}
917

1018
/**
1119
* Gets all DeclarationBlock objects recursively.

lib/Sabberworm/CSS/CSSList/KeyFrame.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ class KeyFrame extends CSSList implements AtRule {
99
private $vendorKeyFrame;
1010
private $animationName;
1111

12-
public function __construct() {
13-
parent::__construct();
12+
public function __construct($iLineNum = 0) {
13+
parent::__construct($iLineNum);
1414
$this->vendorKeyFrame = null;
1515
$this->animationName = null;
1616
}

lib/Sabberworm/CSS/Parser.php

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class Parser {
3434
private $iLength;
3535
private $blockRules;
3636
private $aSizeUnits;
37+
private $iLineNum = 1;
3738

3839
public function __construct($sText, Settings $oParserSettings = null) {
3940
$this->sText = $sText;
@@ -66,7 +67,7 @@ public function getCharset() {
6667

6768
public function parse() {
6869
$this->setCharset($this->oParserSettings->sDefaultCharset);
69-
$oResult = new Document();
70+
$oResult = new Document($this->iLineNum);
7071
$this->parseDocument($oResult);
7172
return $oResult;
7273
}
@@ -139,14 +140,14 @@ private function parseAtRule() {
139140
$sMediaQuery = $this->consumeUntil(';');
140141
}
141142
$this->consume(';');
142-
return new Import($oLocation, $sMediaQuery);
143+
return new Import($oLocation, $sMediaQuery, $this->iLineNum);
143144
} else if ($sIdentifier === 'charset') {
144145
$sCharset = $this->parseStringValue();
145146
$this->consumeWhiteSpace();
146147
$this->consume(';');
147-
return new Charset($sCharset);
148+
return new Charset($sCharset, $this->iLineNum);
148149
} else if ($this->identifierIs($sIdentifier, 'keyframes')) {
149-
$oResult = new KeyFrame();
150+
$oResult = new KeyFrame($this->iLineNum);
150151
$oResult->setVendorKeyFrame($sIdentifier);
151152
$oResult->setAnimationName(trim($this->consumeUntil('{', false, true)));
152153
$this->consumeWhiteSpace();
@@ -166,7 +167,7 @@ private function parseAtRule() {
166167
if (!($mUrl instanceof CSSString || $mUrl instanceof URL)) {
167168
throw new UnexpectedTokenException('Wrong namespace url of invalid type', $mUrl, 'custom');
168169
}
169-
return new CSSNamespace($mUrl, $sPrefix);
170+
return new CSSNamespace($mUrl, $sPrefix, $this->iLineNum);
170171
} else {
171172
//Unknown other at rule (font-face or such)
172173
$sArgs = trim($this->consumeUntil('{', false, true));
@@ -179,10 +180,10 @@ private function parseAtRule() {
179180
}
180181
}
181182
if($bUseRuleSet) {
182-
$oAtRule = new AtRuleSet($sIdentifier, $sArgs);
183+
$oAtRule = new AtRuleSet($sIdentifier, $sArgs, $this->iLineNum);
183184
$this->parseRuleSet($oAtRule);
184185
} else {
185-
$oAtRule = new AtRuleBlockList($sIdentifier, $sArgs);
186+
$oAtRule = new AtRuleBlockList($sIdentifier, $sArgs, $this->iLineNum);
186187
$this->parseList($oAtRule);
187188
}
188189
return $oAtRule;
@@ -204,7 +205,7 @@ private function parseIdentifier($bAllowFunctions = true, $bIgnoreCase = true) {
204205
if ($bAllowFunctions && $this->comes('(')) {
205206
$this->consume('(');
206207
$aArguments = $this->parseValue(array('=', ' ', ','));
207-
$sResult = new CSSFunction($sResult, $aArguments);
208+
$sResult = new CSSFunction($sResult, $aArguments, ',', $this->iLineNum);
208209
$this->consume(')');
209210
}
210211
return $sResult;
@@ -238,7 +239,7 @@ private function parseStringValue() {
238239
}
239240
$this->consume($sQuote);
240241
}
241-
return new CSSString($sResult);
242+
return new CSSString($sResult, $this->iLineNum);
242243
}
243244

244245
private function parseCharacter($bIsForIdentifier) {
@@ -287,7 +288,7 @@ private function parseCharacter($bIsForIdentifier) {
287288
}
288289

289290
private function parseSelector() {
290-
$oResult = new DeclarationBlock();
291+
$oResult = new DeclarationBlock($this->iLineNum);
291292
$oResult->setSelector($this->consumeUntil('{', false, true));
292293
$this->consumeWhiteSpace();
293294
$this->parseRuleSet($oResult);
@@ -333,7 +334,7 @@ private function parseRuleSet($oRuleSet) {
333334
}
334335

335336
private function parseRule() {
336-
$oRule = new Rule($this->parseIdentifier());
337+
$oRule = new Rule($this->parseIdentifier(), $this->iLineNum);
337338
$this->consumeWhiteSpace();
338339
$this->consume(':');
339340
$oValue = $this->parseValue(self::listDelimiterForRule($oRule->getRule()));
@@ -387,7 +388,7 @@ private function parseValue($aListDelimiters) {
387388
break;
388389
}
389390
}
390-
$oList = new RuleValueList($sDelimiter);
391+
$oList = new RuleValueList($sDelimiter, $this->iLineNum);
391392
for ($i = $iStartPosition - 1; $i - $iStartPosition + 1 < $iLength * 2; $i+=2) {
392393
$oList->addListComponent($aStack[$i]);
393394
}
@@ -445,7 +446,7 @@ private function parseNumericValue($bForColor = false) {
445446
}
446447
}
447448
}
448-
return new Size(floatval($sSize), $sUnit, $bForColor);
449+
return new Size(floatval($sSize), $sUnit, $bForColor, $this->iLineNum);
449450
}
450451

451452
private function parseColorValue() {
@@ -456,7 +457,7 @@ private function parseColorValue() {
456457
if ($this->strlen($sValue) === 3) {
457458
$sValue = $sValue[0] . $sValue[0] . $sValue[1] . $sValue[1] . $sValue[2] . $sValue[2];
458459
}
459-
$aColor = array('r' => new Size(intval($sValue[0] . $sValue[1], 16), null, true), 'g' => new Size(intval($sValue[2] . $sValue[3], 16), null, true), 'b' => new Size(intval($sValue[4] . $sValue[5], 16), null, true));
460+
$aColor = array('r' => new Size(intval($sValue[0] . $sValue[1], 16), null, true, $this->iLineNum), 'g' => new Size(intval($sValue[2] . $sValue[3], 16), null, true, $this->iLineNum), 'b' => new Size(intval($sValue[4] . $sValue[5], 16), null, true, $this->iLineNum));
460461
} else {
461462
$sColorMode = $this->parseIdentifier(false);
462463
$this->consumeWhiteSpace();
@@ -472,7 +473,7 @@ private function parseColorValue() {
472473
}
473474
$this->consume(')');
474475
}
475-
return new Color($aColor);
476+
return new Color($aColor, $this->iLineNum);
476477
}
477478

478479
private function parseURLValue() {
@@ -483,7 +484,7 @@ private function parseURLValue() {
483484
$this->consume('(');
484485
}
485486
$this->consumeWhiteSpace();
486-
$oResult = new URL($this->parseStringValue());
487+
$oResult = new URL($this->parseStringValue(), $this->iLineNum);
487488
if ($bUseUrl) {
488489
$this->consumeWhiteSpace();
489490
$this->consume(')');
@@ -516,13 +517,18 @@ private function peek($iLength = 1, $iOffset = 0) {
516517

517518
private function consume($mValue = 1) {
518519
if (is_string($mValue)) {
520+
$noLines = substr_count($mValue, "\n");
519521
$iLength = $this->strlen($mValue);
520522
if (!$this->streql($this->substr($this->iCurrentPosition, $iLength), $mValue)) {
521523
throw new UnexpectedTokenException($mValue, $this->peek(max($iLength, 5)));
522524
}
525+
$this->iLineNum += $noLines;
523526
$this->iCurrentPosition += $this->strlen($mValue);
524527
return $mValue;
525528
} else {
529+
$substring = $this->substr($this->iCurrentPosition, $mValue);
530+
$noLines = substr_count($substring, "\n");
531+
$this->iLineNum += $noLines;
526532
if ($this->iCurrentPosition + $mValue > $this->iLength) {
527533
throw new UnexpectedTokenException($mValue, $this->peek(5), 'count');
528534
}

lib/Sabberworm/CSS/Property/CSSNamespace.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,21 @@
88
class CSSNamespace implements AtRule {
99
private $mUrl;
1010
private $sPrefix;
11+
private $iLineNum;
1112

12-
public function __construct($mUrl, $sPrefix = null) {
13+
public function __construct($mUrl, $sPrefix = null, $iLineNum = 0) {
1314
$this->mUrl = $mUrl;
1415
$this->sPrefix = $sPrefix;
16+
$this->iLineNum = $iLineNum;
1517
}
16-
18+
19+
/**
20+
* @return int
21+
*/
22+
public function getLineNum() {
23+
return $this->iLineNum;
24+
}
25+
1726
public function __toString() {
1827
return $this->render(new \Sabberworm\CSS\OutputFormat());
1928
}

lib/Sabberworm/CSS/Property/Charset.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,18 @@
1212
class Charset implements AtRule {
1313

1414
private $sCharset;
15+
protected $iLineNum;
1516

16-
public function __construct($sCharset) {
17+
public function __construct($sCharset, $iLineNum = 0) {
1718
$this->sCharset = $sCharset;
19+
$this->iLineNum = $iLineNum;
20+
}
21+
22+
/**
23+
* @return int
24+
*/
25+
public function getLineNum() {
26+
return $this->iLineNum;
1827
}
1928

2029
public function setCharset($sCharset) {

lib/Sabberworm/CSS/Property/Import.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,21 @@
1010
class Import implements AtRule {
1111
private $oLocation;
1212
private $sMediaQuery;
13+
protected $iLineNum;
1314

14-
public function __construct(URL $oLocation, $sMediaQuery) {
15+
public function __construct(URL $oLocation, $sMediaQuery, $iLineNum = 0) {
1516
$this->oLocation = $oLocation;
1617
$this->sMediaQuery = $sMediaQuery;
18+
$this->iLineNum = $iLineNum;
1719
}
18-
20+
21+
/**
22+
* @return int
23+
*/
24+
public function getLineNum() {
25+
return $this->iLineNum;
26+
}
27+
1928
public function setLocation($oLocation) {
2029
$this->oLocation = $oLocation;
2130
}

lib/Sabberworm/CSS/Rule/Rule.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,21 @@ class Rule {
1414
private $sRule;
1515
private $mValue;
1616
private $bIsImportant;
17+
protected $iLineNum;
1718

18-
public function __construct($sRule) {
19+
public function __construct($sRule, $iLineNum = 0) {
1920
$this->sRule = $sRule;
2021
$this->mValue = null;
2122
$this->bIsImportant = false;
23+
$this->iLineNum = $iLineNum;
24+
}
25+
26+
/**
27+
* @return int
28+
*/
29+
public function getLineNum()
30+
{
31+
return $this->iLineNum;
2232
}
2333

2434
public function setRule($sRule) {
@@ -43,12 +53,12 @@ public function setValue($mValue) {
4353
public function setValues($aSpaceSeparatedValues) {
4454
$oSpaceSeparatedList = null;
4555
if (count($aSpaceSeparatedValues) > 1) {
46-
$oSpaceSeparatedList = new RuleValueList(' ');
56+
$oSpaceSeparatedList = new RuleValueList(' ', $this->iLineNum);
4757
}
4858
foreach ($aSpaceSeparatedValues as $aCommaSeparatedValues) {
4959
$oCommaSeparatedList = null;
5060
if (count($aCommaSeparatedValues) > 1) {
51-
$oCommaSeparatedList = new RuleValueList(',');
61+
$oCommaSeparatedList = new RuleValueList(',', $this->iLineNum);
5262
}
5363
foreach ($aCommaSeparatedValues as $mValue) {
5464
if (!$oSpaceSeparatedList && !$oCommaSeparatedList) {
@@ -107,7 +117,7 @@ public function addValue($mValue, $sType = ' ') {
107117
}
108118
if (!$this->mValue instanceof RuleValueList || $this->mValue->getListSeparator() !== $sType) {
109119
$mCurrentValue = $this->mValue;
110-
$this->mValue = new RuleValueList($sType);
120+
$this->mValue = new RuleValueList($sType, $this->iLineNum);
111121
if ($mCurrentValue) {
112122
$this->mValue->addListComponent($mCurrentValue);
113123
}

lib/Sabberworm/CSS/RuleSet/AtRuleSet.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ class AtRuleSet extends RuleSet implements AtRule {
1212
private $sType;
1313
private $sArgs;
1414

15-
public function __construct($sType, $sArgs = '') {
16-
parent::__construct();
15+
public function __construct($sType, $sArgs = '', $iLineNum = 0) {
16+
parent::__construct($iLineNum);
1717
$this->sType = $sType;
1818
$this->sArgs = $sArgs;
1919
}

0 commit comments

Comments
 (0)