mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 04:17:35 +00:00
LibHTML: Fix incorrect CSS object model
A StyleRule has a StyleDeclaration which has many StyleProperty. :^)
This commit is contained in:
parent
ac20919b13
commit
8d797fc62e
7 changed files with 26 additions and 31 deletions
|
@ -1,8 +1,7 @@
|
||||||
#include <LibHTML/CSS/StyleDeclaration.h>
|
#include <LibHTML/CSS/StyleDeclaration.h>
|
||||||
|
|
||||||
StyleDeclaration::StyleDeclaration(const String& property_name, NonnullRefPtr<StyleValue>&& value)
|
StyleDeclaration::StyleDeclaration(Vector<StyleProperty>&& properties)
|
||||||
: m_property_name(property_name)
|
: m_properties(move(properties))
|
||||||
, m_value(move(value))
|
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,21 +3,24 @@
|
||||||
#include <AK/String.h>
|
#include <AK/String.h>
|
||||||
#include <LibHTML/CSS/StyleValue.h>
|
#include <LibHTML/CSS/StyleValue.h>
|
||||||
|
|
||||||
|
struct StyleProperty {
|
||||||
|
String name;
|
||||||
|
NonnullRefPtr<StyleValue> value;
|
||||||
|
};
|
||||||
|
|
||||||
class StyleDeclaration : public RefCounted<StyleDeclaration> {
|
class StyleDeclaration : public RefCounted<StyleDeclaration> {
|
||||||
public:
|
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();
|
~StyleDeclaration();
|
||||||
|
|
||||||
const String& property_name() const { return m_property_name; }
|
const Vector<StyleProperty>& properties() const { return m_properties; }
|
||||||
const StyleValue& value() const { return *m_value; }
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
StyleDeclaration(const String& property_name, NonnullRefPtr<StyleValue>&&);
|
explicit StyleDeclaration(Vector<StyleProperty>&&);
|
||||||
|
|
||||||
String m_property_name;
|
Vector<StyleProperty> m_properties;
|
||||||
NonnullRefPtr<StyleValue> m_value;
|
|
||||||
};
|
};
|
||||||
|
|
|
@ -70,8 +70,8 @@ StyleProperties StyleResolver::resolve_style(const Element& element, const Style
|
||||||
|
|
||||||
auto matching_rules = collect_matching_rules(element);
|
auto matching_rules = collect_matching_rules(element);
|
||||||
for (auto& rule : matching_rules) {
|
for (auto& rule : matching_rules) {
|
||||||
for (auto& declaration : rule.declarations()) {
|
for (auto& property : rule.declaration().properties()) {
|
||||||
style_properties.set_property(declaration.property_name(), declaration.value());
|
style_properties.set_property(property.name, property.value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return style_properties;
|
return style_properties;
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
#include <LibHTML/CSS/StyleRule.h>
|
#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_selectors(move(selectors))
|
||||||
, m_declarations(move(declarations))
|
, m_declaration(move(declaration))
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,26 +6,19 @@
|
||||||
|
|
||||||
class StyleRule : public RefCounted<StyleRule> {
|
class StyleRule : public RefCounted<StyleRule> {
|
||||||
public:
|
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();
|
~StyleRule();
|
||||||
|
|
||||||
const Vector<Selector>& selectors() const { return m_selectors; }
|
const Vector<Selector>& selectors() const { return m_selectors; }
|
||||||
const NonnullRefPtrVector<StyleDeclaration>& declarations() const { return m_declarations; }
|
const StyleDeclaration& declaration() const { return m_declaration; }
|
||||||
|
|
||||||
template<typename C>
|
|
||||||
void for_each_declaration(C callback) const
|
|
||||||
{
|
|
||||||
for (auto& declaration : m_declarations)
|
|
||||||
callback(declaration);
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
StyleRule(Vector<Selector>&&, NonnullRefPtrVector<StyleDeclaration>&&);
|
StyleRule(Vector<Selector>&&, NonnullRefPtr<StyleDeclaration>&&);
|
||||||
|
|
||||||
Vector<Selector> m_selectors;
|
Vector<Selector> m_selectors;
|
||||||
NonnullRefPtrVector<StyleDeclaration> m_declarations;
|
NonnullRefPtr<StyleDeclaration> m_declaration;
|
||||||
};
|
};
|
||||||
|
|
|
@ -123,8 +123,8 @@ void dump_rule(const StyleRule& rule)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
printf(" Declarations:\n");
|
printf(" Declarations:\n");
|
||||||
for (auto& declaration : rule.declarations()) {
|
for (auto& property : rule.declaration().properties()) {
|
||||||
printf(" '%s': '%s'\n", declaration.property_name().characters(), declaration.value().to_string().characters());
|
printf(" '%s': '%s'\n", property.name.characters(), property.value->to_string().characters());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -50,7 +50,7 @@ NonnullRefPtr<StyleSheet> parse_css(const String& css)
|
||||||
|
|
||||||
struct CurrentRule {
|
struct CurrentRule {
|
||||||
Vector<Selector> selectors;
|
Vector<Selector> selectors;
|
||||||
NonnullRefPtrVector<StyleDeclaration> declarations;
|
Vector<StyleProperty> properties;
|
||||||
};
|
};
|
||||||
|
|
||||||
CurrentRule current_rule;
|
CurrentRule current_rule;
|
||||||
|
@ -146,7 +146,7 @@ NonnullRefPtr<StyleSheet> parse_css(const String& css)
|
||||||
auto property_value = String::copy(buffer);
|
auto property_value = String::copy(buffer);
|
||||||
buffer.clear();
|
buffer.clear();
|
||||||
consume_specific(';');
|
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 = [&] {
|
auto parse_declarations = [&] {
|
||||||
|
@ -163,7 +163,7 @@ NonnullRefPtr<StyleSheet> parse_css(const String& css)
|
||||||
consume_specific('{');
|
consume_specific('{');
|
||||||
parse_declarations();
|
parse_declarations();
|
||||||
consume_specific('}');
|
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();
|
consume_whitespace();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue