66use Sabberworm \CSS \Parsing \ParserState ;
77use Sabberworm \CSS \Parsing \SourceException ;
88use Sabberworm \CSS \Parsing \UnexpectedTokenException ;
9+ use Sabberworm \CSS \Parsing \UnexpectedEOFException ;
910use Sabberworm \CSS \Property \AtRule ;
1011use Sabberworm \CSS \Property \Charset ;
1112use Sabberworm \CSS \Property \CSSNamespace ;
@@ -110,15 +111,34 @@ private static function parseAtRule(ParserState $oParserState) {
110111 $ oLocation = URL ::parse ($ oParserState );
111112 $ oParserState ->consumeWhiteSpace ();
112113 $ sMediaQuery = null ;
113- if (!$ oParserState ->comes ('; ' )) {
114- $ sMediaQuery = $ oParserState ->consumeUntil ('; ' );
114+ try {
115+ if (!$ oParserState ->comes ('; ' )) {
116+ $ sMediaQuery = $ oParserState ->consumeUntil ('; ' );
117+ }
118+ $ oParserState ->consume ('; ' );
119+ } catch (UnexpectedEOFException $ e ) {
120+ // Save the media query if present
121+ $ sMediaQuery = '' ;
122+ try {
123+ while (!$ oParserState ->isEnd ()) {
124+ $ sMediaQuery .= $ oParserState ->consume (1 );
125+ }
126+ } catch (UnexpectedEOFException $ e ) {}
127+
128+ $ sMediaQuery = trim ($ sMediaQuery );
129+ if ($ sMediaQuery === '' ) {
130+ $ sMediaQuery = null ;
131+ }
115132 }
116- $ oParserState ->consume ('; ' );
117133 return new Import ($ oLocation , $ sMediaQuery , $ iIdentifierLineNum );
118134 } else if ($ sIdentifier === 'charset ' ) {
119135 $ sCharset = CSSString::parse ($ oParserState );
120- $ oParserState ->consumeWhiteSpace ();
121- $ oParserState ->consume ('; ' );
136+ try {
137+ $ oParserState ->consumeWhiteSpace ();
138+ $ oParserState ->consume ('; ' );
139+ } catch (UnexpectedEOFException $ e ) {
140+ // Nothing fatal, file ended before ; was found
141+ }
122142 return new Charset ($ sCharset , $ iIdentifierLineNum );
123143 } else if (self ::identifierIs ($ sIdentifier , 'keyframes ' )) {
124144 $ oResult = new KeyFrame ($ iIdentifierLineNum );
@@ -136,7 +156,11 @@ private static function parseAtRule(ParserState $oParserState) {
136156 $ sPrefix = $ mUrl ;
137157 $ mUrl = Value::parsePrimitiveValue ($ oParserState );
138158 }
139- $ oParserState ->consume ('; ' );
159+ try {
160+ $ oParserState ->consume ('; ' );
161+ } catch (UnexpectedEOFException $ e ) {
162+ // Nothing fatal, file ended before ; was found
163+ }
140164 if ($ sPrefix !== null && !is_string ($ sPrefix )) {
141165 throw new UnexpectedTokenException ('Wrong namespace prefix ' , $ sPrefix , 'custom ' , $ iIdentifierLineNum );
142166 }
0 commit comments