Skip to content

Commit 7388355

Browse files
committed
REGRESSION (257227@main): initial whitespace breaks the query in window.matchMedia()
https://bugs.webkit.org/show_bug.cgi?id=251147 rdar://104104606 Reviewed by Alan Baradlay. * LayoutTests/imported/w3c/web-platform-tests/css/mediaqueries/match-media-parsing-expected.txt: Added. * LayoutTests/imported/w3c/web-platform-tests/css/mediaqueries/match-media-parsing.html: Added. * Source/WebCore/css/query/MediaQueryParser.cpp: (WebCore::MQ::MediaQueryParser::consumeMediaQueryList): Consume initial whitespace. Parsing functions expect to start with a non-whitespace token. This was not an issue with the regular media queries because stylesheet parsing had already consumed any initial whitespace. Also fix the loop so that commas generate "not all" list items even for empty strings "foo," -> "foo, not all". Canonical link: https://commits.webkit.org/259357@main
1 parent 006cef0 commit 7388355

File tree

3 files changed

+67
-4
lines changed

3 files changed

+67
-4
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
PASS Test parsing '' with matchMedia
3+
PASS Test parsing ' ' with matchMedia
4+
PASS Test parsing 'all' with matchMedia
5+
PASS Test parsing ' all' with matchMedia
6+
PASS Test parsing ' all ' with matchMedia
7+
PASS Test parsing 'all,all' with matchMedia
8+
PASS Test parsing ' all , all ' with matchMedia
9+
PASS Test parsing '(color)' with matchMedia
10+
PASS Test parsing '(color' with matchMedia
11+
PASS Test parsing ' (color)' with matchMedia
12+
PASS Test parsing ' ( color ) ' with matchMedia
13+
PASS Test parsing ' ( color ' with matchMedia
14+
PASS Test parsing 'color)' with matchMedia
15+
PASS Test parsing ' color)' with matchMedia
16+
PASS Test parsing ' color ), ( color' with matchMedia
17+
PASS Test parsing ' foo ' with matchMedia
18+
PASS Test parsing ',' with matchMedia
19+
PASS Test parsing ' , ' with matchMedia
20+
PASS Test parsing ',,' with matchMedia
21+
PASS Test parsing ' , , ' with matchMedia
22+
PASS Test parsing ' foo,' with matchMedia
23+
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<!DOCTYPE html>
2+
<link rel="help" href="https://drafts.csswg.org/css-syntax-3/#parse-comma-separated-list-of-component-values">
3+
<script type="text/javascript" src="/resources/testharness.js"></script>
4+
<script type="text/javascript" src="/resources/testharnessreport.js"></script>
5+
6+
<script>
7+
function test_parsing(query, expected) {
8+
test(() => {
9+
const match = window.matchMedia(query);
10+
assert_equals(match.media, expected)
11+
}, "Test parsing '" + query + "' with matchMedia");
12+
}
13+
test_parsing("", "");
14+
test_parsing(" ", "");
15+
test_parsing("all", "all");
16+
test_parsing(" all", "all");
17+
test_parsing(" all ", "all");
18+
test_parsing("all,all", "all, all");
19+
test_parsing(" all , all ", "all, all");
20+
test_parsing("(color)", "(color)");
21+
test_parsing("(color", "(color)");
22+
test_parsing(" (color)", "(color)");
23+
test_parsing(" ( color ) ", "(color)");
24+
test_parsing(" ( color ", "(color)");
25+
test_parsing("color)", "not all");
26+
test_parsing(" color)", "not all");
27+
test_parsing(" color ), ( color", "not all, (color)");
28+
test_parsing(" foo ", "foo");
29+
test_parsing(",", "not all, not all");
30+
test_parsing(" , ", "not all, not all");
31+
test_parsing(",,", "not all, not all, not all");
32+
test_parsing(" , , ", "not all, not all, not all");
33+
test_parsing(" foo,", "foo, not all");
34+
</script>

Source/WebCore/css/query/MediaQueryParser.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,18 +79,20 @@ std::optional<MediaQuery> MediaQueryParser::parseCondition(CSSParserTokenRange r
7979

8080
MediaQueryList MediaQueryParser::consumeMediaQueryList(CSSParserTokenRange& range)
8181
{
82+
range.consumeWhitespace();
83+
84+
if (range.atEnd())
85+
return { };
86+
8287
MediaQueryList list;
8388

84-
while (!range.atEnd()) {
89+
while (true) {
8590
auto begin = range.begin();
8691
while (!range.atEnd() && range.peek().type() != CommaToken)
8792
range.consumeComponentValue();
8893

8994
auto subrange = range.makeSubRange(begin, &range.peek());
9095

91-
if (!range.atEnd())
92-
range.consumeIncludingWhitespace();
93-
9496
auto consumeMediaQueryOrNotAll = [&] {
9597
if (auto query = consumeMediaQuery(subrange))
9698
return *query;
@@ -99,6 +101,10 @@ MediaQueryList MediaQueryParser::consumeMediaQueryList(CSSParserTokenRange& rang
99101
};
100102

101103
list.append(consumeMediaQueryOrNotAll());
104+
105+
if (range.atEnd())
106+
break;
107+
range.consumeIncludingWhitespace();
102108
}
103109

104110
return list;

0 commit comments

Comments
 (0)