summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSam Atkins <atkinssj@gmail.com>2021-07-22 13:00:29 +0100
committerAndreas Kling <kling@serenityos.org>2021-07-22 23:09:01 +0200
commit6e08b200d45cbe10baf0c5fdca68147cade647dc (patch)
tree49bcac1a4acae4ad3e0102e582bfa5268990643a
parent82f3228dd20659d7060df6bb10f92deca7df61a9 (diff)
downloadserenity-6e08b200d45cbe10baf0c5fdca68147cade647dc.zip
LibWeb: Implement ImageStyleValue parsing
Later we will want to make a distinction between URL and Image values, but this works for now.
-rw-r--r--Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp27
-rw-r--r--Userland/Libraries/LibWeb/CSS/Parser/Parser.h9
-rw-r--r--Userland/Libraries/LibWeb/CSS/Parser/Token.h6
3 files changed, 36 insertions, 6 deletions
diff --git a/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp b/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp
index 108f59b698..385a2ab1b2 100644
--- a/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp
+++ b/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp
@@ -34,12 +34,12 @@ ParsingContext::ParsingContext()
{
}
-ParsingContext::ParsingContext(DOM::Document const& document)
+ParsingContext::ParsingContext(DOM::Document& document)
: m_document(&document)
{
}
-ParsingContext::ParsingContext(DOM::ParentNode const& parent_node)
+ParsingContext::ParsingContext(DOM::ParentNode& parent_node)
: m_document(&parent_node.document())
{
}
@@ -1589,6 +1589,26 @@ RefPtr<StyleValue> Parser::parse_string_value(ParsingContext const&, StyleCompon
return {};
}
+RefPtr<StyleValue> Parser::parse_image_value(ParsingContext const& context, StyleComponentValueRule const& component_value)
+{
+ if (component_value.is(Token::Type::Url))
+ return ImageStyleValue::create(context.complete_url(component_value.token().url()), *context.document());
+ if (component_value.is_function() && component_value.function().name().equals_ignoring_case("url")) {
+ auto& function_values = component_value.function().values();
+ // FIXME: Handle url-modifiers. https://www.w3.org/TR/css-values-4/#url-modifiers
+ for (size_t i = 0; i < function_values.size(); ++i) {
+ auto& value = function_values[i];
+ if (value.is(Token::Type::Whitespace))
+ continue;
+ if (value.is(Token::Type::String))
+ return ImageStyleValue::create(context.complete_url(value.token().string()), *context.document());
+ }
+ }
+ // FIXME: Handle gradients.
+
+ return {};
+}
+
RefPtr<StyleValue> Parser::parse_css_value(PropertyID property_id, TokenStream<StyleComponentValueRule>& tokens)
{
dbgln_if(CSS_PARSER_TRACE, "Parser::parse_css_value");
@@ -1653,6 +1673,9 @@ RefPtr<StyleValue> Parser::parse_css_value(ParsingContext const& context, Proper
if (auto string = parse_string_value(context, component_value))
return string;
+ if (auto image = parse_image_value(context, component_value))
+ return image;
+
return {};
}
diff --git a/Userland/Libraries/LibWeb/CSS/Parser/Parser.h b/Userland/Libraries/LibWeb/CSS/Parser/Parser.h
index fc0d6fed2b..efa823f3ff 100644
--- a/Userland/Libraries/LibWeb/CSS/Parser/Parser.h
+++ b/Userland/Libraries/LibWeb/CSS/Parser/Parser.h
@@ -32,15 +32,15 @@ enum class PropertyID;
class ParsingContext {
public:
ParsingContext();
- explicit ParsingContext(DOM::Document const&);
- explicit ParsingContext(DOM::ParentNode const&);
+ explicit ParsingContext(DOM::Document&);
+ explicit ParsingContext(DOM::ParentNode&);
bool in_quirks_mode() const;
-
+ DOM::Document* document() const { return m_document; }
URL complete_url(String const&) const;
private:
- const DOM::Document* m_document { nullptr };
+ DOM::Document* m_document { nullptr };
};
template<typename T>
@@ -174,6 +174,7 @@ private:
static RefPtr<StyleValue> parse_identifier_value(ParsingContext const&, StyleComponentValueRule const&);
static RefPtr<StyleValue> parse_color_value(ParsingContext const&, StyleComponentValueRule const&);
static RefPtr<StyleValue> parse_string_value(ParsingContext const&, StyleComponentValueRule const&);
+ static RefPtr<StyleValue> parse_image_value(ParsingContext const&, StyleComponentValueRule const&);
ParsingContext m_context;
diff --git a/Userland/Libraries/LibWeb/CSS/Parser/Token.h b/Userland/Libraries/LibWeb/CSS/Parser/Token.h
index 3006b734db..ec6146114f 100644
--- a/Userland/Libraries/LibWeb/CSS/Parser/Token.h
+++ b/Userland/Libraries/LibWeb/CSS/Parser/Token.h
@@ -76,6 +76,12 @@ public:
return m_value.string_view();
}
+ StringView url() const
+ {
+ VERIFY(m_type == Type::Url);
+ return m_value.string_view();
+ }
+
bool is(NumberType number_type) const { return is(Token::Type::Number) && m_number_type == number_type; }
int integer() const