diff options
author | Timothy Flynn <trflynn89@pm.me> | 2021-10-29 08:38:50 -0400 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-10-29 22:06:49 +0200 |
commit | eb0fb38caca5f77e323d1745b6974cd7d6946634 (patch) | |
tree | c21a66cc14dcbda86736ce2b41bcc2542273d421 /Userland/Libraries/LibWeb/CSS | |
parent | 1e6afa9fd51885a0f42d3d986c4ed109b9f3b504 (diff) | |
download | serenity-eb0fb38caca5f77e323d1745b6974cd7d6946634.zip |
LibWeb: Support parsing some data: URLs in CSS
For now, data URLs are explicitly limited to expected MIME types. For
example, image-related styles accept image MIME types.
Diffstat (limited to 'Userland/Libraries/LibWeb/CSS')
-rw-r--r-- | Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp | 32 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/CSS/Parser/Parser.h | 7 |
2 files changed, 28 insertions, 11 deletions
diff --git a/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp b/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp index 09c3189d4a..44f47c1700 100644 --- a/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp +++ b/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp @@ -1706,20 +1706,34 @@ Vector<Vector<StyleComponentValueRule>> Parser::parse_a_comma_separated_list_of_ return lists; } -Optional<AK::URL> Parser::parse_url_function(ParsingContext const& context, StyleComponentValueRule const& component_value) +Optional<AK::URL> Parser::parse_url_function(ParsingContext const& context, StyleComponentValueRule const& component_value, AllowedDataUrlType allowed_data_url_type) { // FIXME: Handle list of media queries. https://www.w3.org/TR/css-cascade-3/#conditional-import // FIXME: Handle data: urls (RFC2397) - auto is_data_url = [](StringView& url_string) -> bool { - return url_string.starts_with("data:", CaseSensitivity::CaseInsensitive); + auto convert_string_to_url = [&](StringView& url_string) -> Optional<AK::URL> { + if (url_string.starts_with("data:", CaseSensitivity::CaseInsensitive)) { + auto data_url = AK::URL(url_string); + + switch (allowed_data_url_type) { + case AllowedDataUrlType::Image: + if (data_url.data_mime_type().starts_with("image"sv, CaseSensitivity::CaseInsensitive)) + return data_url; + break; + + default: + break; + } + + return {}; + } + + return context.complete_url(url_string); }; if (component_value.is(Token::Type::Url)) { auto url_string = component_value.token().url(); - if (is_data_url(url_string)) - return {}; - return context.complete_url(url_string); + return convert_string_to_url(url_string); } if (component_value.is_function() && component_value.function().name().equals_ignoring_case("url")) { auto& function_values = component_value.function().values(); @@ -1730,9 +1744,7 @@ Optional<AK::URL> Parser::parse_url_function(ParsingContext const& context, Styl continue; if (value.is(Token::Type::String)) { auto url_string = value.token().string(); - if (is_data_url(url_string)) - return {}; - return context.complete_url(url_string); + return convert_string_to_url(url_string); } break; } @@ -2370,7 +2382,7 @@ RefPtr<StyleValue> Parser::parse_string_value(ParsingContext const&, StyleCompon RefPtr<StyleValue> Parser::parse_image_value(ParsingContext const& context, StyleComponentValueRule const& component_value) { - auto url = parse_url_function(context, component_value); + auto url = parse_url_function(context, component_value, AllowedDataUrlType::Image); if (url.has_value()) return ImageStyleValue::create(url.value()); // FIXME: Handle gradients. diff --git a/Userland/Libraries/LibWeb/CSS/Parser/Parser.h b/Userland/Libraries/LibWeb/CSS/Parser/Parser.h index 3bd48747c6..2863a3c19d 100644 --- a/Userland/Libraries/LibWeb/CSS/Parser/Parser.h +++ b/Userland/Libraries/LibWeb/CSS/Parser/Parser.h @@ -195,7 +195,12 @@ private: static Optional<float> try_parse_float(StringView string); static Optional<Color> parse_color(ParsingContext const&, StyleComponentValueRule const&); static Optional<Length> parse_length(ParsingContext const&, StyleComponentValueRule const&); - static Optional<AK::URL> parse_url_function(ParsingContext const&, StyleComponentValueRule const&); + + enum class AllowedDataUrlType { + None, + Image, + }; + static Optional<AK::URL> parse_url_function(ParsingContext const&, StyleComponentValueRule const&, AllowedDataUrlType = AllowedDataUrlType::None); Result<NonnullRefPtr<StyleValue>, ParsingResult> parse_css_value(PropertyID, TokenStream<StyleComponentValueRule>&); static RefPtr<StyleValue> parse_css_value(ParsingContext const&, StyleComponentValueRule const&); |