LibWeb/CSS: Implement CSSImageValue and reify images as it

This is specced to be opaque, so there's not much to it.
This commit is contained in:
Sam Atkins 2025-09-12 15:07:19 +01:00
commit 5904694844
Notes: github-actions[bot] 2025-09-15 07:48:05 +00:00
10 changed files with 109 additions and 4 deletions

View file

@ -110,6 +110,7 @@ set(SOURCES
CSS/CSSFontFaceDescriptors.cpp
CSS/CSSFontFaceRule.cpp
CSS/CSSGroupingRule.cpp
CSS/CSSImageValue.cpp
CSS/CSSImportRule.cpp
CSS/CSSKeyframeRule.cpp
CSS/CSSKeyframesRule.cpp
@ -206,6 +207,7 @@ set(SOURCES
CSS/StyleSheet.cpp
CSS/StyleSheetIdentifier.cpp
CSS/StyleSheetList.cpp
CSS/StyleValues/AbstractImageStyleValue.cpp
CSS/StyleValues/AnchorStyleValue.cpp
CSS/StyleValues/AnchorSizeStyleValue.cpp
CSS/StyleValues/AngleStyleValue.cpp

View file

@ -0,0 +1,40 @@
/*
* Copyright (c) 2025, Sam Atkins <sam@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "CSSImageValue.h"
#include <LibWeb/Bindings/CSSImageValuePrototype.h>
#include <LibWeb/Bindings/Intrinsics.h>
namespace Web::CSS {
GC_DEFINE_ALLOCATOR(CSSImageValue);
GC::Ref<CSSImageValue> CSSImageValue::create(JS::Realm& realm, String constructed_from_string)
{
return realm.create<CSSImageValue>(realm, move(constructed_from_string));
}
CSSImageValue::CSSImageValue(JS::Realm& realm, String constructed_from_string)
: CSSStyleValue(realm)
, m_constructed_from_string(move(constructed_from_string))
{
}
void CSSImageValue::initialize(JS::Realm& realm)
{
WEB_SET_PROTOTYPE_FOR_INTERFACE(CSSImageValue);
Base::initialize(realm);
}
// https://drafts.css-houdini.org/css-typed-om-1/#stylevalue-serialization
String CSSImageValue::to_string() const
{
// AD-HOC: The spec doesn't say how to serialize this, as it's intentionally a black box.
// We just serialize the source string that was used to construct this.
return m_constructed_from_string;
}
}

View file

@ -0,0 +1,33 @@
/*
* Copyright (c) 2025, Sam Atkins <sam@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibWeb/CSS/CSSStyleValue.h>
namespace Web::CSS {
// https://drafts.css-houdini.org/css-typed-om-1/#cssimagevalue
class CSSImageValue final : public CSSStyleValue {
WEB_PLATFORM_OBJECT(CSSImageValue, CSSStyleValue);
GC_DECLARE_ALLOCATOR(CSSImageValue);
public:
[[nodiscard]] static GC::Ref<CSSImageValue> create(JS::Realm&, String constructed_from_string);
virtual ~CSSImageValue() override = default;
virtual String to_string() const override;
private:
explicit CSSImageValue(JS::Realm&, String constructed_from_string);
virtual void initialize(JS::Realm&) override;
String m_constructed_from_string;
};
}

View file

@ -0,0 +1,6 @@
#import <CSS/CSSStyleValue.idl>
// https://drafts.css-houdini.org/css-typed-om-1/#cssimagevalue
[Exposed=(Window, Worker, PaintWorklet, LayoutWorklet)]
interface CSSImageValue : CSSStyleValue {
};

View file

@ -0,0 +1,19 @@
/*
* Copyright (c) 2025, Sam Atkins <sam@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "AbstractImageStyleValue.h"
#include <LibWeb/CSS/CSSImageValue.h>
namespace Web::CSS {
// https://drafts.css-houdini.org/css-typed-om-1/#reify-stylevalue
GC::Ref<CSSStyleValue> AbstractImageStyleValue::reify(JS::Realm& realm, String const&) const
{
// AD-HOC: There's no spec description of how to reify as a CSSImageValue.
return CSSImageValue::create(realm, to_string(SerializationMode::Normal));
}
}

View file

@ -40,6 +40,8 @@ public:
virtual void paint(DisplayListRecordingContext& context, DevicePixelRect const& dest_rect, ImageRendering) const = 0;
virtual Optional<Gfx::Color> color_if_single_pixel_bitmap() const { return {}; }
virtual GC::Ref<CSSStyleValue> reify(JS::Realm&, String const& associated_property) const override;
};
// And now, some gradient related things. Maybe these should live somewhere else.

View file

@ -235,6 +235,7 @@ class CSSDescriptors;
class CSSFontFaceDescriptors;
class CSSFontFaceRule;
class CSSGroupingRule;
class CSSImageValue;
class CSSImportRule;
class CSSKeyframeRule;
class CSSKeyframesRule;

View file

@ -29,6 +29,7 @@ libweb_js_bindings(CSS/CSSCounterStyleRule)
libweb_js_bindings(CSS/CSSFontFaceDescriptors)
libweb_js_bindings(CSS/CSSFontFaceRule)
libweb_js_bindings(CSS/CSSGroupingRule)
libweb_js_bindings(CSS/CSSImageValue)
libweb_js_bindings(CSS/CSSImportRule)
libweb_js_bindings(CSS/CSSKeyframeRule)
libweb_js_bindings(CSS/CSSKeyframesRule)

View file

@ -42,6 +42,7 @@ CSSCounterStyleRule
CSSFontFaceDescriptors
CSSFontFaceRule
CSSGroupingRule
CSSImageValue
CSSImportRule
CSSKeyframeRule
CSSKeyframesRule

View file

@ -2,7 +2,7 @@ Harness status: OK
Found 3 tests
3 Fail
Fail Normalizing a valid <url> returns a CSSImageValue
Fail Normalizing a bad <url> returns a CSSImageValue
Fail Normalizing a <gradient> returns a CSSImageValue
3 Pass
Pass Normalizing a valid <url> returns a CSSImageValue
Pass Normalizing a bad <url> returns a CSSImageValue
Pass Normalizing a <gradient> returns a CSSImageValue