Coverage Report

Created: 2025-03-04 07:22

/src/serenity/Userland/Libraries/LibWeb/SVG/SVGSymbolElement.cpp
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 2023, Preston Taylor <95388976+PrestonLTaylor@users.noreply.github.com>
3
 *
4
 * SPDX-License-Identifier: BSD-2-Clause
5
 */
6
7
#include <LibWeb/Bindings/Intrinsics.h>
8
#include <LibWeb/Bindings/SVGSymbolElementPrototype.h>
9
#include <LibWeb/CSS/StyleProperties.h>
10
#include <LibWeb/CSS/StyleValues/CSSKeywordValue.h>
11
#include <LibWeb/CSS/StyleValues/DisplayStyleValue.h>
12
#include <LibWeb/CSS/StyleValues/ShorthandStyleValue.h>
13
#include <LibWeb/DOM/ShadowRoot.h>
14
#include <LibWeb/Layout/SVGGraphicsBox.h>
15
#include <LibWeb/SVG/AttributeNames.h>
16
#include <LibWeb/SVG/SVGAnimatedRect.h>
17
#include <LibWeb/SVG/SVGSymbolElement.h>
18
#include <LibWeb/SVG/SVGUseElement.h>
19
20
namespace Web::SVG {
21
22
JS_DEFINE_ALLOCATOR(SVGSymbolElement);
23
24
SVGSymbolElement::SVGSymbolElement(DOM::Document& document, DOM::QualifiedName qualified_name)
25
0
    : SVGGraphicsElement(document, qualified_name)
26
0
{
27
0
}
28
29
void SVGSymbolElement::initialize(JS::Realm& realm)
30
0
{
31
0
    Base::initialize(realm);
32
0
    WEB_SET_PROTOTYPE_FOR_INTERFACE(SVGSymbolElement);
33
0
    m_view_box_for_bindings = heap().allocate<SVGAnimatedRect>(realm, realm);
34
0
}
35
36
void SVGSymbolElement::visit_edges(Cell::Visitor& visitor)
37
0
{
38
0
    Base::visit_edges(visitor);
39
0
    visitor.visit(m_view_box_for_bindings);
40
0
}
41
42
// https://svgwg.org/svg2-draft/struct.html#SymbolNotes
43
void SVGSymbolElement::apply_presentational_hints(CSS::StyleProperties& style) const
44
0
{
45
0
    Base::apply_presentational_hints(style);
46
47
0
    if (is_direct_child_of_use_shadow_tree()) {
48
        // The generated instance of a ‘symbol’ that is the direct referenced element of a ‘use’ element must always have a computed value of inline for the display property.
49
0
        style.set_property(CSS::PropertyID::Display, CSS::DisplayStyleValue::create(CSS::Display::from_short(CSS::Display::Short::Inline)));
50
0
    }
51
0
}
52
53
void SVGSymbolElement::attribute_changed(FlyString const& name, Optional<String> const& old_value, Optional<String> const& value)
54
0
{
55
0
    Base::attribute_changed(name, old_value, value);
56
0
    if (name.equals_ignoring_ascii_case(SVG::AttributeNames::viewBox)) {
57
0
        m_view_box = try_parse_view_box(value.value_or(String {}));
58
0
        m_view_box_for_bindings->set_nulled(!m_view_box.has_value());
59
0
        if (m_view_box.has_value()) {
60
0
            m_view_box_for_bindings->set_base_val(Gfx::DoubleRect { m_view_box->min_x, m_view_box->min_y, m_view_box->width, m_view_box->height });
61
0
            m_view_box_for_bindings->set_anim_val(Gfx::DoubleRect { m_view_box->min_x, m_view_box->min_y, m_view_box->width, m_view_box->height });
62
0
        }
63
0
    }
64
0
}
65
66
bool SVGSymbolElement::is_direct_child_of_use_shadow_tree() const
67
0
{
68
0
    auto maybe_shadow_root = parent();
69
0
    if (!is<DOM::ShadowRoot>(maybe_shadow_root)) {
70
0
        return false;
71
0
    }
72
73
0
    auto host = static_cast<const DOM::ShadowRoot&>(*maybe_shadow_root).host();
74
0
    return is<SVGUseElement>(host);
75
0
}
76
77
JS::GCPtr<Layout::Node> SVGSymbolElement::create_layout_node(NonnullRefPtr<CSS::StyleProperties> style)
78
0
{
79
0
    return heap().allocate_without_realm<Layout::SVGGraphicsBox>(document(), *this, move(style));
80
0
}
81
82
}