LibWeb: Implement ImageStyleValue parsing

Later we will want to make a distinction between URL and Image values,
but this works for now.
This commit is contained in:
Sam Atkins 2021-07-22 13:00:29 +01:00 committed by Andreas Kling
parent 82f3228dd2
commit 6e08b200d4
Notes: sideshowbarker 2024-07-18 08:31:05 +09:00
3 changed files with 36 additions and 6 deletions

View File

@ -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 {};
}

View File

@ -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;

View File

@ -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