srctree

Robin Linden parent 05c7489d 14230461
css: Fix crash when a media query isn't followed by a space

inlinesplit
css/parser.h added: 15, removed: 5, total 10
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2021 Robin Lindén <dev@robinlinden.eu>
// SPDX-FileCopyrightText: 2021-2022 Robin Lindén <dev@robinlinden.eu>
// SPDX-FileCopyrightText: 2021 Mikael Larsson <c.mikael.larsson@gmail.com>
//
// SPDX-License-Identifier: BSD-2-Clause
@@ -32,7 +32,7 @@ public:
 
skip_whitespace_and_comments();
while (!is_eof()) {
if (starts_with("@media ")) {
if (starts_with("@media ") || starts_with("@media(")) {
advance(std::strlen("@media"));
skip_whitespace_and_comments();
 
 
css/parser_test.cpp added: 15, removed: 5, total 10
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2021 Robin Lindén <dev@robinlinden.eu>
// SPDX-FileCopyrightText: 2021-2022 Robin Lindén <dev@robinlinden.eu>
// SPDX-FileCopyrightText: 2021 Mikael Larsson <c.mikael.larsson@gmail.com>
//
// SPDX-License-Identifier: BSD-2-Clause
@@ -228,6 +228,16 @@ int main() {
expect(a.media_query.empty());
});
 
etest::test("parser: minified media query", [] {
auto rules = css::parse("@media(prefers-color-scheme: dark){p{font-size:10px;}}");
require_eq(rules.size(), std::size_t{1});
auto const &rule = rules[0];
expect_eq(rule.media_query, "(prefers-color-scheme: dark)");
expect_eq(rule.selectors, std::vector{"p"s});
require_eq(rule.declarations.size(), std::size_t{1});
expect_eq(rule.declarations.at("font-size"), "10px");
});
 
auto box_shorthand_one_value = [](std::string property, std::string value, std::string post_fix = "") {
return [=] {
auto rules = css::parse(fmt::format("p {{ {}: {}; }}"sv, property, value));