Skip to content

Commit e0efd9f

Browse files
committed
Resolve issues with using exceptions for flow control
1 parent 8db468c commit e0efd9f

File tree

2 files changed

+18
-30
lines changed

2 files changed

+18
-30
lines changed

lib/Sabberworm/CSS/CSSList/CSSList.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,9 @@ private static function parseAtRule(ParserState $oParserState) {
120120
return new Import($oLocation, $sMediaQuery, $iIdentifierLineNum);
121121
} else if ($sIdentifier === 'charset') {
122122
$sCharset = CSSString::parse($oParserState);
123-
try {
124-
$oParserState->consumeWhiteSpace();
123+
$oParserState->consumeWhiteSpace();
124+
if (!$oParserState->isEnd()) {
125125
$oParserState->consume(';');
126-
} catch (UnexpectedEOFException $e) {
127-
// Nothing fatal, file ended before ; was found
128126
}
129127
return new Charset($sCharset, $iIdentifierLineNum);
130128
} else if (self::identifierIs($sIdentifier, 'keyframes')) {
@@ -143,11 +141,7 @@ private static function parseAtRule(ParserState $oParserState) {
143141
$sPrefix = $mUrl;
144142
$mUrl = Value::parsePrimitiveValue($oParserState);
145143
}
146-
try {
147-
$oParserState->consume(';');
148-
} catch (UnexpectedEOFException $e) {
149-
// Nothing fatal, file ended before ; was found
150-
}
144+
$oParserState->consume(';');
151145
if ($sPrefix !== null && !is_string($sPrefix)) {
152146
throw new UnexpectedTokenException('Wrong namespace prefix', $sPrefix, 'custom', $iIdentifierLineNum);
153147
}

lib/Sabberworm/CSS/Parsing/ParserState.php

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -216,30 +216,24 @@ public function consumeUntil($aEnd, $bIncludeEnd = false, $consumeEnd = false, a
216216
$out = '';
217217
$start = $this->iCurrentPosition;
218218

219-
try {
220-
while (true) {
221-
$char = $this->consume(1);
222-
if (in_array($char, $aEnd)) {
223-
if ($bIncludeEnd) {
224-
$out .= $char;
225-
} elseif (!$consumeEnd) {
226-
$this->iCurrentPosition -= $this->strlen($char);
227-
}
228-
return $out;
229-
}
230-
$out .= $char;
231-
if ($comment = $this->consumeComment()) {
232-
$comments[] = $comment;
219+
while (!$this->isEnd()) {
220+
$char = $this->consume(1);
221+
if (in_array($char, $aEnd)) {
222+
if ($bIncludeEnd) {
223+
$out .= $char;
224+
} elseif (!$consumeEnd) {
225+
$this->iCurrentPosition -= $this->strlen($char);
233226
}
234-
}
235-
} catch (UnexpectedEOFException $e) {
236-
if (in_array(self::EOF, $aEnd)) {
237227
return $out;
238-
} else {
239-
// Reset the position and forward the EOF exception, so the caller can distinguish between EOF and the standard unexpected token error
240-
$this->iCurrentPosition = $start;
241-
throw $e;
242228
}
229+
$out .= $char;
230+
if ($comment = $this->consumeComment()) {
231+
$comments[] = $comment;
232+
}
233+
}
234+
235+
if (in_array(self::EOF, $aEnd)) {
236+
return $out;
243237
}
244238

245239
$this->iCurrentPosition = $start;

0 commit comments

Comments
 (0)