Skip to content

Commit 8ac156d

Browse files
committed
Re-implement ParserState anchors with dedicated Anchor class
1 parent 2cef8a0 commit 8ac156d

File tree

4 files changed

+51
-17
lines changed

4 files changed

+51
-17
lines changed

src/Parsing/Anchor.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Sabberworm\CSS\Parsing;
4+
5+
class Anchor
6+
{
7+
/**
8+
* @var int
9+
*/
10+
private $iPosition;
11+
12+
/**
13+
* @var \Sabberworm\CSS\Parsing\ParserState
14+
*/
15+
private $oParserState;
16+
17+
/**
18+
* @param int $iPosition
19+
* @param \Sabberworm\CSS\Parsing\ParserState $oParserState
20+
*/
21+
public function __construct($iPosition, ParserState $oParserState)
22+
{
23+
$this->iPosition = $iPosition;
24+
$this->oParserState = $oParserState;
25+
}
26+
27+
/**
28+
* @return void
29+
*/
30+
public function backtrack()
31+
{
32+
$this->oParserState->setPosition($this->iPosition);
33+
}
34+
}

src/Parsing/ParserState.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,6 @@ class ParserState
4949
*/
5050
private $iLineNo;
5151

52-
/**
53-
* @var int
54-
*/
55-
private $iAnchor;
56-
5752
/**
5853
* @param string $sText the complete CSS as text (i.e., usually the contents of a CSS file)
5954
* @param int $iLineNo
@@ -64,7 +59,6 @@ public function __construct($sText, Settings $oParserSettings, $iLineNo = 1)
6459
$this->sText = $sText;
6560
$this->iCurrentPosition = 0;
6661
$this->iLineNo = $iLineNo;
67-
$this->iAnchor = null;
6862
$this->setCharset($this->oParserSettings->sDefaultCharset);
6963
}
7064

@@ -118,16 +112,22 @@ public function getSettings()
118112
return $this->oParserSettings;
119113
}
120114

121-
public function setAnchor()
115+
/**
116+
* @return \Sabberworm\CSS\Parsing\Anchor
117+
*/
118+
public function anchor()
122119
{
123-
$this->iAnchor = $this->iCurrentPosition;
120+
return new Anchor($this->iCurrentPosition, $this);
124121
}
125122

126-
public function backtrackToAnchor()
123+
/**
124+
* @param int $iPosition
125+
*
126+
* @return void
127+
*/
128+
public function setPosition($iPosition)
127129
{
128-
if ($this->iAnchor !== null) {
129-
$this->iCurrentPosition = $this->iAnchor;
130-
}
130+
$this->iCurrentPosition = $iPosition;
131131
}
132132

133133
/**

src/Value/URL.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function __construct(CSSString $oURL, $iLineNo = 0)
3636
*/
3737
public static function parse(ParserState $oParserState)
3838
{
39-
$oParserState->setAnchor();
39+
$oAnchor = $oParserState->anchor();
4040
$sIdentifier = '';
4141
for ($i = 0; $i < 3; $i++) {
4242
$sChar = $oParserState->parseCharacter(true);
@@ -50,7 +50,7 @@ public static function parse(ParserState $oParserState)
5050
$oParserState->consumeWhiteSpace();
5151
$oParserState->consume('(');
5252
} else {
53-
$oParserState->backtrackToAnchor();
53+
$oAnchor->backtrack();
5454
}
5555
$oParserState->consumeWhiteSpace();
5656
$oResult = new URL(CSSString::parse($oParserState), $oParserState->currentLine());

src/Value/Value.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,15 +106,15 @@ public static function parseValue(ParserState $oParserState, array $aListDelimit
106106
*/
107107
public static function parseIdentifierOrFunction(ParserState $oParserState, $bIgnoreCase = false)
108108
{
109-
$oParserState->setAnchor();
109+
$oAnchor = $oParserState->anchor();
110110
$mResult = $oParserState->parseIdentifier($bIgnoreCase);
111111

112112
if ($oParserState->comes('(')) {
113113
if ($oParserState->streql('url', $mResult)) {
114-
$oParserState->backtrackToAnchor();
114+
$oAnchor->backtrack();
115115
$mResult = URL::parse($oParserState);
116116
} else if ($oParserState->streql('calc', $mResult) || $oParserState->streql('-webkit-calc', $mResult) || $oParserState->streql('-moz-calc', $mResult)) {
117-
$oParserState->backtrackToAnchor();
117+
$oAnchor->backtrack();
118118
$mResult = CalcFunction::parse($oParserState);
119119
} else {
120120
$oParserState->consume('(');

0 commit comments

Comments
 (0)