Skip to content

Commit

Permalink
LibWeb: Ensure inline CSS loaded from HTML is ElementInline
Browse files Browse the repository at this point in the history
This commit changes inline CSS loaded from style attributes of HTML
elements to be loaded as CSS::ElementInlineCSSStyleDeclaration instead
of CSS::CSSStyleDeclaration, fixing a crash when the style of that
element is changed from JavaScript.
  • Loading branch information
DoubleNegation authored and awesomekling committed Aug 15, 2021
1 parent afcd053 commit 0fdfdbe
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
6 changes: 6 additions & 0 deletions Userland/Libraries/LibWeb/CSS/CSSStyleDeclaration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ ElementInlineCSSStyleDeclaration::ElementInlineCSSStyleDeclaration(DOM::Element&
{
}

ElementInlineCSSStyleDeclaration::ElementInlineCSSStyleDeclaration(DOM::Element& element, CSSStyleDeclaration& declaration)
: CSSStyleDeclaration(move(declaration.m_properties), move(declaration.m_custom_properties))
, m_element(element.make_weak_ptr<DOM::Element>())
{
}

ElementInlineCSSStyleDeclaration::~ElementInlineCSSStyleDeclaration()
{
}
Expand Down
3 changes: 3 additions & 0 deletions Userland/Libraries/LibWeb/CSS/CSSStyleDeclaration.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class CSSStyleDeclaration
explicit CSSStyleDeclaration(Vector<StyleProperty>&&, HashMap<String, StyleProperty>&&);

private:
friend class ElementInlineCSSStyleDeclaration;
friend class Bindings::CSSStyleDeclarationWrapper;

Vector<StyleProperty> m_properties;
Expand All @@ -53,13 +54,15 @@ class CSSStyleDeclaration
class ElementInlineCSSStyleDeclaration final : public CSSStyleDeclaration {
public:
static NonnullRefPtr<ElementInlineCSSStyleDeclaration> create(DOM::Element& element) { return adopt_ref(*new ElementInlineCSSStyleDeclaration(element)); }
static NonnullRefPtr<ElementInlineCSSStyleDeclaration> create_and_take_properties_from(DOM::Element& element, CSSStyleDeclaration& declaration) { return adopt_ref(*new ElementInlineCSSStyleDeclaration(element, declaration)); }
virtual ~ElementInlineCSSStyleDeclaration() override;

DOM::Element* element() { return m_element.ptr(); }
const DOM::Element* element() const { return m_element.ptr(); }

private:
explicit ElementInlineCSSStyleDeclaration(DOM::Element&);
explicit ElementInlineCSSStyleDeclaration(DOM::Element&, CSSStyleDeclaration&);

WeakPtr<DOM::Element> m_element;
};
Expand Down
7 changes: 5 additions & 2 deletions Userland/Libraries/LibWeb/DOM/Element.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,11 @@ void Element::parse_attribute(const FlyString& name, const String& value)
m_classes.unchecked_append(new_class);
}
} else if (name == HTML::AttributeNames::style) {
m_inline_style = parse_css_declaration(CSS::ParsingContext(document()), value);
set_needs_style_update(true);
auto parsed_style = parse_css_declaration(CSS::ParsingContext(document()), value);
if (!parsed_style.is_null()) {
m_inline_style = CSS::ElementInlineCSSStyleDeclaration::create_and_take_properties_from(*this, parsed_style.release_nonnull());
set_needs_style_update(true);
}
}
}

Expand Down

0 comments on commit 0fdfdbe

Please sign in to comment.