1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 15:47:44 +00:00

LibWeb: Make CSSStyleDeclaration an abstract class

This patch moves the CSS property+value storage down to a new subclass
of CSSStyleDeclaration called PropertyOwningCSSStyleDeclaration.

The JavaScript wrapper for CSSStyleDeclaration now calls virtual
functions on the C++ object.

This is preparation for supporting computed style CSSStyleDeclaration
objects which won't have internal property storage, but rather an
internal element pointer. :^)
This commit is contained in:
Andreas Kling 2021-09-12 19:24:01 +02:00
parent 10679b6df2
commit 0bcab60463
9 changed files with 106 additions and 69 deletions

View file

@ -4,9 +4,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/ScopeGuard.h>
#include <LibWeb/Bindings/CSSStyleDeclarationWrapper.h>
#include <LibWeb/CSS/Parser/Parser.h>
#include <LibWeb/DOM/Element.h>
namespace Web::Bindings {
@ -28,10 +26,8 @@ JS::Value CSSStyleDeclarationWrapper::internal_get(JS::PropertyName const& name,
auto property_id = CSS::property_id_from_string(name.to_string());
if (property_id == CSS::PropertyID::Invalid)
return Base::internal_get(name, receiver);
for (auto& property : impl().properties()) {
if (property.property_id == property_id)
return js_string(vm(), property.value->to_string());
}
if (auto maybe_property = impl().property(property_id); maybe_property.has_value())
return js_string(vm(), maybe_property->value->to_string());
return js_string(vm(), String::empty());
}
@ -48,32 +44,7 @@ bool CSSStyleDeclarationWrapper::internal_set(JS::PropertyName const& name, JS::
if (vm().exception())
return false;
auto new_value = parse_css_value(CSS::ParsingContext {}, css_text, property_id);
// FIXME: What are we supposed to do if we can't parse it?
if (!new_value)
return false;
ScopeGuard style_invalidation_guard = [&] {
auto& declaration = verify_cast<CSS::ElementInlineCSSStyleDeclaration>(impl());
if (auto* element = declaration.element())
element->invalidate_style();
};
// FIXME: I don't think '!important' is being handled correctly here..
for (auto& property : impl().m_properties) {
if (property.property_id == property_id) {
property.value = new_value.release_nonnull();
return true;
}
}
impl().m_properties.append(CSS::StyleProperty {
.property_id = property_id,
.value = new_value.release_nonnull(),
.important = false,
});
return true;
return impl().set_property(property_id, css_text);
}
}