2020-01-18 09:38:21 +01:00
|
|
|
|
/*
|
2024-09-21 20:11:03 +02:00
|
|
|
|
* Copyright (c) 2018-2024, Andreas Kling <andreas@ladybird.org>
|
2025-02-06 11:10:36 +00:00
|
|
|
|
* Copyright (c) 2023-2025, Sam Atkins <sam@ladybird.org>
|
2020-01-18 09:38:21 +01:00
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
|
*/
|
|
|
|
|
|
2022-09-24 16:34:04 -06:00
|
|
|
|
#include <LibWeb/Bindings/CSSStyleDeclarationPrototype.h>
|
2022-10-30 17:50:04 +00:00
|
|
|
|
#include <LibWeb/Bindings/ExceptionOrUtils.h>
|
2022-09-24 16:34:04 -06:00
|
|
|
|
#include <LibWeb/Bindings/Intrinsics.h>
|
2021-03-13 20:11:33 +01:00
|
|
|
|
#include <LibWeb/CSS/CSSStyleDeclaration.h>
|
2024-09-21 20:11:03 +02:00
|
|
|
|
#include <LibWeb/CSS/StyleComputer.h>
|
2022-08-07 16:21:26 +02:00
|
|
|
|
#include <LibWeb/DOM/Document.h>
|
2021-03-16 18:55:53 +01:00
|
|
|
|
#include <LibWeb/DOM/Element.h>
|
2019-06-20 23:25:25 +02:00
|
|
|
|
|
2020-07-26 20:01:35 +02:00
|
|
|
|
namespace Web::CSS {
|
2020-03-07 10:27:02 +01:00
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
|
GC_DEFINE_ALLOCATOR(CSSStyleDeclaration);
|
2023-11-19 19:47:52 +01:00
|
|
|
|
|
2025-03-18 15:05:42 +00:00
|
|
|
|
CSSStyleDeclaration::CSSStyleDeclaration(JS::Realm& realm, Computed computed, Readonly readonly)
|
2023-05-21 12:42:22 +02:00
|
|
|
|
: PlatformObject(realm)
|
2025-03-18 15:05:42 +00:00
|
|
|
|
, m_computed(computed == Computed::Yes)
|
|
|
|
|
, m_readonly(readonly == Readonly::Yes)
|
2022-08-07 16:21:26 +02:00
|
|
|
|
{
|
2024-11-14 17:05:56 +00:00
|
|
|
|
m_legacy_platform_object_flags = LegacyPlatformObjectFlags {
|
|
|
|
|
.supports_indexed_properties = true,
|
|
|
|
|
};
|
2022-08-07 16:21:26 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-08-07 08:41:28 +02:00
|
|
|
|
void CSSStyleDeclaration::initialize(JS::Realm& realm)
|
2023-05-21 12:42:22 +02:00
|
|
|
|
{
|
2024-03-16 13:13:08 +01:00
|
|
|
|
WEB_SET_PROTOTYPE_FOR_INTERFACE(CSSStyleDeclaration);
|
2025-04-20 16:22:57 +02:00
|
|
|
|
Base::initialize(realm);
|
2023-05-21 12:42:22 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-11 16:10:55 +02:00
|
|
|
|
// https://drafts.csswg.org/cssom/#update-style-attribute-for
|
2025-03-17 13:04:41 +00:00
|
|
|
|
void CSSStyleDeclaration::update_style_attribute()
|
2022-04-11 16:10:55 +02:00
|
|
|
|
{
|
2022-04-11 16:56:52 +02:00
|
|
|
|
// 1. Assert: declaration block’s computed flag is unset.
|
2025-03-18 15:05:42 +00:00
|
|
|
|
VERIFY(!is_computed());
|
2022-04-11 16:56:52 +02:00
|
|
|
|
|
|
|
|
|
// 2. Let owner node be declaration block’s owner node.
|
|
|
|
|
// 3. If owner node is null, then return.
|
2025-03-18 15:05:42 +00:00
|
|
|
|
if (!owner_node().has_value())
|
2022-04-11 16:10:55 +02:00
|
|
|
|
return;
|
|
|
|
|
|
2022-04-11 16:56:52 +02:00
|
|
|
|
// 4. Set declaration block’s updating flag.
|
2025-03-18 15:05:42 +00:00
|
|
|
|
set_is_updating(true);
|
2022-04-11 16:56:52 +02:00
|
|
|
|
|
|
|
|
|
// 5. Set an attribute value for owner node using "style" and the result of serializing declaration block.
|
2025-03-18 15:05:42 +00:00
|
|
|
|
MUST(owner_node()->element().set_attribute(HTML::AttributeNames::style, serialized()));
|
2022-04-11 16:56:52 +02:00
|
|
|
|
|
|
|
|
|
// 6. Unset declaration block’s updating flag.
|
2025-03-18 15:05:42 +00:00
|
|
|
|
set_is_updating(false);
|
2022-04-11 16:10:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-11-05 04:51:05 +00:00
|
|
|
|
// https://drafts.csswg.org/cssom/#dom-cssstyledeclaration-csstext
|
2023-11-21 10:39:54 +13:00
|
|
|
|
String CSSStyleDeclaration::css_text() const
|
2021-10-01 19:57:45 +02:00
|
|
|
|
{
|
2022-11-05 04:51:05 +00:00
|
|
|
|
// 1. If the computed flag is set, then return the empty string.
|
LibWeb/CSS: Merge style declaration subclasses into CSSStyleProperties
We previously had PropertyOwningCSSStyleDeclaration and
ResolvedCSSStyleDeclaration, representing the current style properties
and resolved style respectively. Both of these were the
CSSStyleDeclaration type in the CSSOM. (We also had
ElementInlineCSSStyleDeclaration but I removed that in a previous
commit.)
In the meantime, the spec has changed so that these should now be a new
CSSStyleProperties type in the CSSOM. Also, we need to subclass
CSSStyleDeclaration for things like CSSFontFaceRule's list of
descriptors, which means it wouldn't hold style properties.
So, this commit does the fairly messy work of combining these two types
into a new CSSStyleProperties class. A lot of what previously was done
as separate methods in the two classes, now follows the spec steps of
"if the readonly flag is set, do X" instead, which is hopefully easier
to follow too.
There is still some functionality in CSSStyleDeclaration that belongs in
CSSStyleProperties, but I'll do that next. To avoid a huge diff for
"CSSStyleDeclaration-all-supported-properties-and-default-values.txt"
both here and in the following commit, we don't apply the (currently
empty) CSSStyleProperties prototype yet.
2025-03-17 17:50:49 +00:00
|
|
|
|
if (is_computed())
|
|
|
|
|
return {};
|
2021-10-01 19:57:45 +02:00
|
|
|
|
|
2022-11-05 04:51:05 +00:00
|
|
|
|
// 2. Return the result of serializing the declarations.
|
|
|
|
|
return serialized();
|
2021-10-01 19:57:45 +02:00
|
|
|
|
}
|
|
|
|
|
|
2024-11-14 17:05:56 +00:00
|
|
|
|
Optional<JS::Value> CSSStyleDeclaration::item_value(size_t index) const
|
|
|
|
|
{
|
|
|
|
|
auto value = item(index);
|
|
|
|
|
if (value.is_empty())
|
|
|
|
|
return {};
|
|
|
|
|
|
|
|
|
|
return JS::PrimitiveString::create(vm(), value);
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-18 15:05:42 +00:00
|
|
|
|
void CSSStyleDeclaration::visit_edges(Visitor& visitor)
|
|
|
|
|
{
|
|
|
|
|
Base::visit_edges(visitor);
|
|
|
|
|
visitor.visit(m_parent_rule);
|
|
|
|
|
if (m_owner_node.has_value())
|
|
|
|
|
m_owner_node->visit(visitor);
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-07 10:27:02 +01:00
|
|
|
|
}
|