diff --git a/Userland/Libraries/LibWeb/CMakeLists.txt b/Userland/Libraries/LibWeb/CMakeLists.txt index ea774a9bc74..4b062f01186 100644 --- a/Userland/Libraries/LibWeb/CMakeLists.txt +++ b/Userland/Libraries/LibWeb/CMakeLists.txt @@ -283,6 +283,7 @@ function(libweb_js_wrapper class) add_custom_target(generate_${basename}Prototype.cpp DEPENDS Bindings/${class}Prototype.cpp) endfunction() +libweb_js_wrapper(CSS/CSSStyleDeclaration) libweb_js_wrapper(CSS/CSSStyleSheet) libweb_js_wrapper(CSS/StyleSheet) libweb_js_wrapper(CSS/StyleSheetList) diff --git a/Userland/Libraries/LibWeb/CSS/CSSStyleDeclaration.cpp b/Userland/Libraries/LibWeb/CSS/CSSStyleDeclaration.cpp index 04b291298ab..621e500c224 100644 --- a/Userland/Libraries/LibWeb/CSS/CSSStyleDeclaration.cpp +++ b/Userland/Libraries/LibWeb/CSS/CSSStyleDeclaration.cpp @@ -37,4 +37,11 @@ CSSStyleDeclaration::~CSSStyleDeclaration() { } +String CSSStyleDeclaration::item(size_t index) const +{ + if (index >= m_properties.size()) + return {}; + return CSS::string_from_property_id(m_properties[index].property_id); +} + } diff --git a/Userland/Libraries/LibWeb/CSS/CSSStyleDeclaration.h b/Userland/Libraries/LibWeb/CSS/CSSStyleDeclaration.h index b81b08dcdc8..ec553198b88 100644 --- a/Userland/Libraries/LibWeb/CSS/CSSStyleDeclaration.h +++ b/Userland/Libraries/LibWeb/CSS/CSSStyleDeclaration.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018-2020, Andreas Kling + * Copyright (c) 2018-2021, Andreas Kling * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -28,6 +28,7 @@ #include #include +#include #include namespace Web::CSS { @@ -38,8 +39,12 @@ struct StyleProperty { bool important { false }; }; -class CSSStyleDeclaration : public RefCounted { +class CSSStyleDeclaration + : public RefCounted + , public Bindings::Wrappable { public: + using WrapperType = Bindings::CSSStyleDeclarationWrapper; + static NonnullRefPtr create(Vector&& properties) { return adopt(*new CSSStyleDeclaration(move(properties))); @@ -49,6 +54,9 @@ public: const Vector& properties() const { return m_properties; } + size_t length() const { return m_properties.size(); } + String item(size_t index) const; + private: explicit CSSStyleDeclaration(Vector&&); @@ -56,3 +64,9 @@ private: }; } + +namespace Web::Bindings { + +CSSStyleDeclarationWrapper* wrap(JS::GlobalObject&, CSS::CSSStyleDeclaration&); + +} diff --git a/Userland/Libraries/LibWeb/CSS/CSSStyleDeclaration.idl b/Userland/Libraries/LibWeb/CSS/CSSStyleDeclaration.idl new file mode 100644 index 00000000000..d894e72cdad --- /dev/null +++ b/Userland/Libraries/LibWeb/CSS/CSSStyleDeclaration.idl @@ -0,0 +1,6 @@ +interface CSSStyleDeclaration { + + readonly attribute unsigned long length; + CSSOMString item(unsigned long index); + +}; diff --git a/Userland/Libraries/LibWeb/CodeGenerators/WrapperGenerator.cpp b/Userland/Libraries/LibWeb/CodeGenerators/WrapperGenerator.cpp index 21d96beb501..5c93f89dee0 100644 --- a/Userland/Libraries/LibWeb/CodeGenerators/WrapperGenerator.cpp +++ b/Userland/Libraries/LibWeb/CodeGenerators/WrapperGenerator.cpp @@ -1086,6 +1086,7 @@ void generate_prototype_implementation(const IDL::Interface& interface) #include #include #include +#include #include #include #include diff --git a/Userland/Libraries/LibWeb/DOM/Element.cpp b/Userland/Libraries/LibWeb/DOM/Element.cpp index b506948e1c6..04aece74503 100644 --- a/Userland/Libraries/LibWeb/DOM/Element.cpp +++ b/Userland/Libraries/LibWeb/DOM/Element.cpp @@ -380,4 +380,11 @@ void Element::set_shadow_root(RefPtr shadow_root) invalidate_style(); } +NonnullRefPtr Element::style_for_bindings() +{ + if (!m_inline_style) + m_inline_style = CSS::CSSStyleDeclaration::create({}); + return *m_inline_style; +} + } diff --git a/Userland/Libraries/LibWeb/DOM/Element.h b/Userland/Libraries/LibWeb/DOM/Element.h index fdafd19fd8f..767a6dd6dec 100644 --- a/Userland/Libraries/LibWeb/DOM/Element.h +++ b/Userland/Libraries/LibWeb/DOM/Element.h @@ -92,6 +92,8 @@ public: const CSS::CSSStyleDeclaration* inline_style() const { return m_inline_style; } + NonnullRefPtr style_for_bindings(); + // FIXME: innerHTML also appears on shadow roots. https://w3c.github.io/DOM-Parsing/#dom-innerhtml String inner_html() const; void set_inner_html(StringView); diff --git a/Userland/Libraries/LibWeb/DOM/Element.idl b/Userland/Libraries/LibWeb/DOM/Element.idl index 20675943210..6d4a397c589 100644 --- a/Userland/Libraries/LibWeb/DOM/Element.idl +++ b/Userland/Libraries/LibWeb/DOM/Element.idl @@ -23,4 +23,6 @@ interface Element : Node { readonly attribute Element? nextElementSibling; readonly attribute Element? previousElementSibling; + + [ImplementedAs=style_for_bindings] readonly attribute CSSStyleDeclaration style; }; diff --git a/Userland/Libraries/LibWeb/Forward.h b/Userland/Libraries/LibWeb/Forward.h index 3b8e4c42223..624f4b22098 100644 --- a/Userland/Libraries/LibWeb/Forward.h +++ b/Userland/Libraries/LibWeb/Forward.h @@ -203,6 +203,7 @@ class XMLHttpRequestEventTarget; } namespace Web::Bindings { +class CSSStyleDeclarationWrapper; class CSSStyleSheetWrapper; class CanvasRenderingContext2DWrapper; class CharacterDataWrapper;