@@ -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 }
0 commit comments