Skip to content

Truly independent web browser

4.9 KB
Raw Blame
/*
 * Copyright (c) 2026, Tim Ledbetter <[email protected]>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/IntrusiveList.h>
#include <LibGC/RootHashTable.h>
#include <LibGfx/Matrix4x4.h>
#include <LibWeb/CSS/ElementBoxKind.h>
#include <LibWeb/Forward.h>
#include <LibWeb/Layout/Node.h>
#include <LibWeb/SVG/AttributeParsing.h>
#include <LibWeb/SVG/SVGAnimatedLength.h>
#include <LibWeb/SVG/SVGElement.h>
#include <LibWeb/SVG/SVGFitToViewBox.h>
#include <LibWeb/SVG/SVGGradientElement.h>
#include <LibWeb/SVG/SVGURIReference.h>

namespace Web::SVG {

class SVGAnimatedTransformList;

class SVGPatternElement
    : public SVGElement
    , public SVGFitToViewBox
    , public SVGURIReferenceMixin<SupportsXLinkHref::Yes> {
    WEB_WRAPPABLE(SVGPatternElement, SVGElement);
    GC_DECLARE_ALLOCATOR(SVGPatternElement);

public:
    virtual ~SVGPatternElement() override = default;

    virtual SVGFitToViewBox const* fit_to_view_box() const override { return this; }

    virtual void attribute_changed(Utf16FlyString const& name, Optional<Utf16String> const& old_value, Optional<Utf16String> const& value, Optional<Utf16FlyString> const& namespace_) override;
    virtual void inserted() override;
    virtual void removed_from(IsSubtreeRoot, Node* old_ancestor, Node& old_root) override;
    virtual void moved_from(IsSubtreeRoot, GC::Ptr<Node> old_ancestor) override;
    virtual void finalize() override;

    Optional<Utf16String> href_attribute_value() const;
    // The id this pattern's href names, if any.
    Optional<Utf16String> linked_id() const;

    SVGUnits pattern_units() const;
    SVGUnits pattern_content_units() const;
    Optional<Gfx::AffineTransform> pattern_transform() const;
    NumberPercentage pattern_x() const;
    NumberPercentage pattern_y() const;
    NumberPercentage pattern_width() const;
    NumberPercentage pattern_height() const;

    GC::Ref<SVGAnimatedEnumeration> pattern_units_for_bindings();
    GC::Ref<SVGAnimatedEnumeration> pattern_content_units_for_bindings();
    GC::Ref<SVGAnimatedTransformList> pattern_transform_for_bindings();

    // https://w3c.github.io/svgwg/svg2-draft/pservers.html#__svg__SVGPatternElement__x
    REFLECT_ANIMATED_LENGTH_ATTRIBUTE(x, Horizontal, SVGLengthValue::number(0));

    // https://w3c.github.io/svgwg/svg2-draft/pservers.html#__svg__SVGPatternElement__y
    REFLECT_ANIMATED_LENGTH_ATTRIBUTE(y, Vertical, SVGLengthValue::number(0));

    // https://w3c.github.io/svgwg/svg2-draft/pservers.html#__svg__SVGPatternElement__width
    REFLECT_ANIMATED_LENGTH_ATTRIBUTE(width, Horizontal, SVGLengthValue::number(0));

    // https://w3c.github.io/svgwg/svg2-draft/pservers.html#__svg__SVGPatternElement__height
    REFLECT_ANIMATED_LENGTH_ATTRIBUTE(height, Vertical, SVGLengthValue::number(0));

    GC::Ptr<SVGPatternElement const> pattern_content_element() const;

    void push_paint_server_description(void* sink, Layout::Node const& target_layout_node) const;

    virtual CSS::ElementBoxKind box_kind() const override { return CSS::ElementBoxKind::NoBox; }

protected:
    SVGPatternElement(DOM::Document&, DOM::QualifiedName);

    virtual void initialize_element() override;
    virtual void visit_edges(Cell::Visitor&) override;

private:
    virtual bool is_svg_pattern_element() const final { return true; }

    void update_document_pattern_list_membership();

    IntrusiveListNode<SVGPatternElement> m_list_node;

    GC::Ptr<SVGPatternElement const> linked_pattern(GC::RootHashTable<SVGPatternElement const*>& seen_patterns) const;
    GC::Ptr<SVGPatternElement const> pattern_content_element_impl(GC::RootHashTable<SVGPatternElement const*>& seen_patterns) const;

    SVGUnits pattern_units_impl(GC::RootHashTable<SVGPatternElement const*>& seen_patterns) const;
    SVGUnits pattern_content_units_impl(GC::RootHashTable<SVGPatternElement const*>& seen_patterns) const;
    Optional<Gfx::AffineTransform> pattern_transform_impl(GC::RootHashTable<SVGPatternElement const*>& seen_patterns) const;
    NumberPercentage pattern_x_impl(GC::RootHashTable<SVGPatternElement const*>& seen_patterns) const;
    NumberPercentage pattern_y_impl(GC::RootHashTable<SVGPatternElement const*>& seen_patterns) const;
    NumberPercentage pattern_width_impl(GC::RootHashTable<SVGPatternElement const*>& seen_patterns) const;
    NumberPercentage pattern_height_impl(GC::RootHashTable<SVGPatternElement const*>& seen_patterns) const;

    Optional<SVGUnits> m_pattern_units;
    Optional<SVGUnits> m_pattern_content_units;
    Optional<Gfx::AffineTransform> m_pattern_transform;
    GC::Ptr<SVGAnimatedTransformList> m_pattern_transform_for_bindings;
    Optional<NumberPercentage> m_x;
    Optional<NumberPercentage> m_y;
    Optional<NumberPercentage> m_width;
    Optional<NumberPercentage> m_height;

public:
    using DocumentPatternElementList = IntrusiveList<&SVGPatternElement::m_list_node>;
};

}

namespace Web::DOM {

template<>
inline bool Node::fast_is<SVG::SVGPatternElement>() const { return is_svg_pattern_element(); }

}