1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 20:08:13 +00:00

LibHTML: Fix incorrect CSS object model

A StyleRule has a StyleDeclaration which has many StyleProperty. :^)
This commit is contained in:
Andreas Kling 2019-09-30 20:06:17 +02:00
parent ac20919b13
commit 8d797fc62e
7 changed files with 26 additions and 31 deletions

View file

@ -1,8 +1,7 @@
#include <LibHTML/CSS/StyleDeclaration.h>
StyleDeclaration::StyleDeclaration(const String& property_name, NonnullRefPtr<StyleValue>&& value)
: m_property_name(property_name)
, m_value(move(value))
StyleDeclaration::StyleDeclaration(Vector<StyleProperty>&& properties)
: m_properties(move(properties))
{
}

View file

@ -3,21 +3,24 @@
#include <AK/String.h>
#include <LibHTML/CSS/StyleValue.h>
struct StyleProperty {
String name;
NonnullRefPtr<StyleValue> value;
};
class StyleDeclaration : public RefCounted<StyleDeclaration> {
public:
static NonnullRefPtr<StyleDeclaration> create(const String& property_name, NonnullRefPtr<StyleValue>&& value)
static NonnullRefPtr<StyleDeclaration> create(Vector<StyleProperty>&& properties)
{
return adopt(*new StyleDeclaration(property_name, move(value)));
return adopt(*new StyleDeclaration(move(properties)));
}
~StyleDeclaration();
const String& property_name() const { return m_property_name; }
const StyleValue& value() const { return *m_value; }
const Vector<StyleProperty>& properties() const { return m_properties; }
public:
StyleDeclaration(const String& property_name, NonnullRefPtr<StyleValue>&&);
explicit StyleDeclaration(Vector<StyleProperty>&&);
String m_property_name;
NonnullRefPtr<StyleValue> m_value;
Vector<StyleProperty> m_properties;
};

View file

@ -70,8 +70,8 @@ StyleProperties StyleResolver::resolve_style(const Element& element, const Style
auto matching_rules = collect_matching_rules(element);
for (auto& rule : matching_rules) {
for (auto& declaration : rule.declarations()) {
style_properties.set_property(declaration.property_name(), declaration.value());
for (auto& property : rule.declaration().properties()) {
style_properties.set_property(property.name, property.value);
}
}
return style_properties;

View file

@ -1,8 +1,8 @@
#include <LibHTML/CSS/StyleRule.h>
StyleRule::StyleRule(Vector<Selector>&& selectors, NonnullRefPtrVector<StyleDeclaration>&& declarations)
StyleRule::StyleRule(Vector<Selector>&& selectors, NonnullRefPtr<StyleDeclaration>&& declaration)
: m_selectors(move(selectors))
, m_declarations(move(declarations))
, m_declaration(move(declaration))
{
}

View file

@ -6,26 +6,19 @@
class StyleRule : public RefCounted<StyleRule> {
public:
static NonnullRefPtr<StyleRule> create(Vector<Selector>&& selectors, NonnullRefPtrVector<StyleDeclaration>&& declarations)
static NonnullRefPtr<StyleRule> create(Vector<Selector>&& selectors, NonnullRefPtr<StyleDeclaration>&& declaration)
{
return adopt(*new StyleRule(move(selectors), move(declarations)));
return adopt(*new StyleRule(move(selectors), move(declaration)));
}
~StyleRule();
const Vector<Selector>& selectors() const { return m_selectors; }
const NonnullRefPtrVector<StyleDeclaration>& declarations() const { return m_declarations; }
template<typename C>
void for_each_declaration(C callback) const
{
for (auto& declaration : m_declarations)
callback(declaration);
}
const StyleDeclaration& declaration() const { return m_declaration; }
private:
StyleRule(Vector<Selector>&&, NonnullRefPtrVector<StyleDeclaration>&&);
StyleRule(Vector<Selector>&&, NonnullRefPtr<StyleDeclaration>&&);
Vector<Selector> m_selectors;
NonnullRefPtrVector<StyleDeclaration> m_declarations;
NonnullRefPtr<StyleDeclaration> m_declaration;
};

View file

@ -123,8 +123,8 @@ void dump_rule(const StyleRule& rule)
}
}
printf(" Declarations:\n");
for (auto& declaration : rule.declarations()) {
printf(" '%s': '%s'\n", declaration.property_name().characters(), declaration.value().to_string().characters());
for (auto& property : rule.declaration().properties()) {
printf(" '%s': '%s'\n", property.name.characters(), property.value->to_string().characters());
}
}

View file

@ -50,7 +50,7 @@ NonnullRefPtr<StyleSheet> parse_css(const String& css)
struct CurrentRule {
Vector<Selector> selectors;
NonnullRefPtrVector<StyleDeclaration> declarations;
Vector<StyleProperty> properties;
};
CurrentRule current_rule;
@ -146,7 +146,7 @@ NonnullRefPtr<StyleSheet> parse_css(const String& css)
auto property_value = String::copy(buffer);
buffer.clear();
consume_specific(';');
current_rule.declarations.append(StyleDeclaration::create(property_name, parse_css_value(property_value)));
current_rule.properties.append({ property_name, parse_css_value(property_value) });
};
auto parse_declarations = [&] {
@ -163,7 +163,7 @@ NonnullRefPtr<StyleSheet> parse_css(const String& css)
consume_specific('{');
parse_declarations();
consume_specific('}');
rules.append(StyleRule::create(move(current_rule.selectors), move(current_rule.declarations)));
rules.append(StyleRule::create(move(current_rule.selectors), StyleDeclaration::create(move(current_rule.properties))));
consume_whitespace();
};