mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 16:37:35 +00:00
LibWeb: Implement remaining CSS parse_as...() entry points
This commit is contained in:
parent
65fe3895e6
commit
57f6d86996
2 changed files with 52 additions and 38 deletions
|
@ -923,7 +923,8 @@ Optional<StyleProperty> Parser::parse_as_declaration(TokenStream<T>& tokens)
|
||||||
}
|
}
|
||||||
|
|
||||||
auto declaration = consume_a_declaration(tokens);
|
auto declaration = consume_a_declaration(tokens);
|
||||||
// FIXME: Return declaration
|
if (declaration.has_value())
|
||||||
|
return convert_to_style_property(declaration.value());
|
||||||
|
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
@ -934,10 +935,33 @@ RefPtr<CSSStyleDeclaration> Parser::parse_as_list_of_declarations()
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
RefPtr<CSSStyleDeclaration> Parser::parse_as_list_of_declarations(TokenStream<T>&)
|
RefPtr<CSSStyleDeclaration> Parser::parse_as_list_of_declarations(TokenStream<T>& tokens)
|
||||||
{
|
{
|
||||||
// FIXME: Return the declarations.
|
auto declarations_and_at_rules = consume_a_list_of_declarations(tokens);
|
||||||
return {};
|
|
||||||
|
Vector<StyleProperty> properties;
|
||||||
|
HashMap<String, StyleProperty> custom_properties;
|
||||||
|
|
||||||
|
for (auto& declaration_or_at_rule : declarations_and_at_rules) {
|
||||||
|
if (declaration_or_at_rule.is_at_rule()) {
|
||||||
|
dbgln("Parser::parse_as_list_of_declarations(): At-rule is not allowed here!");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto& declaration = declaration_or_at_rule.m_declaration;
|
||||||
|
|
||||||
|
auto maybe_property = convert_to_style_property(declaration);
|
||||||
|
if (maybe_property.has_value()) {
|
||||||
|
auto property = maybe_property.value();
|
||||||
|
if (property.property_id == PropertyID::Custom) {
|
||||||
|
custom_properties.set(property.custom_name, property);
|
||||||
|
} else {
|
||||||
|
properties.append(property);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return CSSStyleDeclaration::create(move(properties), move(custom_properties));
|
||||||
}
|
}
|
||||||
|
|
||||||
Optional<StyleComponentValueRule> Parser::parse_as_component_value()
|
Optional<StyleComponentValueRule> Parser::parse_as_component_value()
|
||||||
|
@ -1061,45 +1085,34 @@ RefPtr<CSSStyleDeclaration> Parser::convert_to_declaration(NonnullRefPtr<StyleBl
|
||||||
if (!block->is_curly())
|
if (!block->is_curly())
|
||||||
return {};
|
return {};
|
||||||
|
|
||||||
Vector<StyleProperty> properties;
|
|
||||||
HashMap<String, StyleProperty> custom_properties;
|
|
||||||
|
|
||||||
auto stream = TokenStream(block->m_values);
|
auto stream = TokenStream(block->m_values);
|
||||||
auto declarations_and_at_rules = consume_a_list_of_declarations(stream);
|
return parse_as_list_of_declarations(stream);
|
||||||
|
}
|
||||||
|
|
||||||
for (auto& declaration_or_at_rule : declarations_and_at_rules) {
|
Optional<StyleProperty> Parser::convert_to_style_property(StyleDeclarationRule& declaration)
|
||||||
if (declaration_or_at_rule.is_at_rule()) {
|
{
|
||||||
dbgln("CSS::Parser::convert_to_declaration(): Skipping @ rule.");
|
auto& property_name = declaration.m_name;
|
||||||
continue;
|
auto property_id = property_id_from_string(property_name);
|
||||||
}
|
if (property_id == PropertyID::Invalid && property_name.starts_with("--"))
|
||||||
|
property_id = PropertyID::Custom;
|
||||||
|
|
||||||
auto& declaration = declaration_or_at_rule.m_declaration;
|
if (property_id == PropertyID::Invalid && !property_name.starts_with("-")) {
|
||||||
|
dbgln("Parser::convert_to_style_property(): Unrecognized property '{}'", property_name);
|
||||||
auto& property_name = declaration.m_name;
|
return {};
|
||||||
auto property_id = property_id_from_string(property_name);
|
|
||||||
if (property_id == PropertyID::Invalid && property_name.starts_with("--"))
|
|
||||||
property_id = PropertyID::Custom;
|
|
||||||
|
|
||||||
if (property_id == PropertyID::Invalid && !property_name.starts_with("-")) {
|
|
||||||
dbgln("CSS::Parser::convert_to_declaration(): Unrecognized property '{}'", property_name);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
auto value_token_stream = TokenStream(declaration.m_values);
|
|
||||||
auto value = parse_css_value(property_id, value_token_stream);
|
|
||||||
if (!value) {
|
|
||||||
dbgln("CSS::Parser::convert_to_declaration(): Property '{}' has no value.", property_name);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (property_id == PropertyID::Custom) {
|
|
||||||
custom_properties.set(property_name, StyleProperty { property_id, value.release_nonnull(), declaration.m_name, declaration.m_important });
|
|
||||||
} else {
|
|
||||||
properties.append(StyleProperty { property_id, value.release_nonnull(), {}, declaration.m_important });
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return CSSStyleDeclaration::create(move(properties), move(custom_properties));
|
auto value_token_stream = TokenStream(declaration.m_values);
|
||||||
|
auto value = parse_css_value(property_id, value_token_stream);
|
||||||
|
if (!value) {
|
||||||
|
dbgln("Parser::convert_to_style_property(): Property '{}' has no value.", property_name);
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (property_id == PropertyID::Custom) {
|
||||||
|
return StyleProperty { property_id, value.release_nonnull(), declaration.m_name, declaration.m_important };
|
||||||
|
} else {
|
||||||
|
return StyleProperty { property_id, value.release_nonnull(), {}, declaration.m_important };
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Optional<float> Parser::try_parse_float(StringView string)
|
Optional<float> Parser::try_parse_float(StringView string)
|
||||||
|
|
|
@ -169,6 +169,7 @@ private:
|
||||||
|
|
||||||
[[nodiscard]] RefPtr<CSSRule> convert_to_rule(NonnullRefPtr<StyleRule>);
|
[[nodiscard]] RefPtr<CSSRule> convert_to_rule(NonnullRefPtr<StyleRule>);
|
||||||
[[nodiscard]] RefPtr<CSSStyleDeclaration> convert_to_declaration(NonnullRefPtr<StyleBlockRule>);
|
[[nodiscard]] RefPtr<CSSStyleDeclaration> convert_to_declaration(NonnullRefPtr<StyleBlockRule>);
|
||||||
|
[[nodiscard]] Optional<StyleProperty> convert_to_style_property(StyleDeclarationRule&);
|
||||||
|
|
||||||
static Optional<float> try_parse_float(StringView string);
|
static Optional<float> try_parse_float(StringView string);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue