LibWeb: Resolve basic-shape clip-paths

These will be ignored within SVGs (for now SVGs only support <clipPath>
elements), but will allow clipping standard HTML elements.
This commit is contained in:
MacDue 2024-05-26 00:25:05 +01:00 committed by Andreas Kling
parent 0135af6e60
commit 517379ff80
Notes: sideshowbarker 2024-07-16 23:59:28 +09:00
4 changed files with 24 additions and 6 deletions

View file

@ -21,6 +21,7 @@
#include <LibWeb/CSS/Ratio.h>
#include <LibWeb/CSS/Size.h>
#include <LibWeb/CSS/StyleValues/AbstractImageStyleValue.h>
#include <LibWeb/CSS/StyleValues/BasicShapeStyleValue.h>
#include <LibWeb/CSS/StyleValues/PositionStyleValue.h>
#include <LibWeb/CSS/StyleValues/ShadowStyleValue.h>
#include <LibWeb/CSS/Transformation.h>
@ -234,18 +235,31 @@ private:
};
// https://drafts.fxtf.org/css-masking/#the-clip-path
// TODO: Support clip sources.
class ClipPathReference {
public:
// TODO: Support clip sources.
ClipPathReference(URL::URL const& url)
: m_url(url)
: m_clip_source(url)
{
}
URL::URL const& url() const { return m_url; }
ClipPathReference(BasicShapeStyleValue const& basic_shape)
: m_clip_source(basic_shape)
{
}
bool is_basic_shape() const { return m_clip_source.has<BasicShape>(); }
bool is_url() const { return m_clip_source.has<URL::URL>(); }
URL::URL const& url() const { return m_clip_source.get<URL::URL>(); }
BasicShapeStyleValue const& basic_shape() const { return *m_clip_source.get<BasicShape>(); }
private:
URL::URL m_url;
using BasicShape = NonnullRefPtr<BasicShapeStyleValue const>;
Variant<URL::URL, BasicShape> m_clip_source;
};
struct BackgroundLayerData {

View file

@ -712,6 +712,7 @@
],
"__comment": "FIXME: This should be a <clip-source> | [ <basic-shape> || <geometry-box> ]",
"valid-types": [
"basic-shape",
"url"
],
"initial": "none"

View file

@ -801,8 +801,11 @@ void NodeWithStyle::apply_style(const CSS::StyleProperties& computed_style)
if (auto mask = computed_style.property(CSS::PropertyID::Mask); mask->is_url())
computed_values.set_mask(mask->as_url().url());
if (auto clip_path = computed_style.property(CSS::PropertyID::ClipPath); clip_path->is_url())
auto clip_path = computed_style.property(CSS::PropertyID::ClipPath);
if (clip_path->is_url())
computed_values.set_clip_path(clip_path->as_url().url());
else if (clip_path->is_basic_shape())
computed_values.set_clip_path(clip_path->as_basic_shape());
if (auto clip_rule = computed_style.clip_rule(); clip_rule.has_value())
computed_values.set_clip_rule(*clip_rule);

View file

@ -83,7 +83,7 @@ JS::GCPtr<SVG::SVGMaskElement const> SVGGraphicsElement::mask() const
JS::GCPtr<SVG::SVGClipPathElement const> SVGGraphicsElement::clip_path() const
{
auto const& clip_path_reference = layout_node()->computed_values().clip_path();
if (!clip_path_reference.has_value())
if (!clip_path_reference.has_value() || !clip_path_reference->is_url())
return {};
return try_resolve_url_to<SVG::SVGClipPathElement const>(clip_path_reference->url());
}