Coverage Report

Created: 2026-09-26 06:49

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/serenity/Userland/Libraries/LibWeb/SVG/SVGLineElement.cpp
Line
Count
Source
1
/*
2
 * Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
3
 *
4
 * SPDX-License-Identifier: BSD-2-Clause
5
 */
6
7
#include <LibWeb/Bindings/Intrinsics.h>
8
#include <LibWeb/Bindings/SVGLineElementPrototype.h>
9
#include <LibWeb/SVG/AttributeNames.h>
10
#include <LibWeb/SVG/AttributeParser.h>
11
#include <LibWeb/SVG/SVGLineElement.h>
12
13
namespace Web::SVG {
14
15
JS_DEFINE_ALLOCATOR(SVGLineElement);
16
17
SVGLineElement::SVGLineElement(DOM::Document& document, DOM::QualifiedName qualified_name)
18
0
    : SVGGeometryElement(document, qualified_name)
19
0
{
20
0
}
21
22
void SVGLineElement::initialize(JS::Realm& realm)
23
0
{
24
0
    Base::initialize(realm);
25
0
    WEB_SET_PROTOTYPE_FOR_INTERFACE(SVGLineElement);
26
0
}
27
28
void SVGLineElement::attribute_changed(FlyString const& name, Optional<String> const& old_value, Optional<String> const& value)
29
0
{
30
0
    SVGGeometryElement::attribute_changed(name, old_value, value);
31
32
0
    if (name == SVG::AttributeNames::x1) {
33
0
        m_x1 = AttributeParser::parse_number_percentage(value.value_or(String {}));
34
0
    } else if (name == SVG::AttributeNames::y1) {
35
0
        m_y1 = AttributeParser::parse_number_percentage(value.value_or(String {}));
36
0
    } else if (name == SVG::AttributeNames::x2) {
37
0
        m_x2 = AttributeParser::parse_number_percentage(value.value_or(String {}));
38
0
    } else if (name == SVG::AttributeNames::y2) {
39
0
        m_y2 = AttributeParser::parse_number_percentage(value.value_or(String {}));
40
0
    }
41
0
}
42
43
Gfx::Path SVGLineElement::get_path(CSSPixelSize viewport_size)
44
0
{
45
0
    auto const viewport_width = viewport_size.width().to_float();
46
0
    auto const viewport_height = viewport_size.height().to_float();
47
48
0
    Gfx::Path path;
49
0
    float const x1 = m_x1.value_or({ 0, false }).resolve_relative_to(viewport_width);
50
0
    float const y1 = m_y1.value_or({ 0, false }).resolve_relative_to(viewport_height);
51
0
    float const x2 = m_x2.value_or({ 0, false }).resolve_relative_to(viewport_width);
52
0
    float const y2 = m_y2.value_or({ 0, false }).resolve_relative_to(viewport_height);
53
54
    // 1. perform an absolute moveto operation to absolute location (x1,y1)
55
0
    path.move_to({ x1, y1 });
56
57
    // 2. perform an absolute lineto operation to absolute location (x2,y2)
58
0
    path.line_to({ x2, y2 });
59
60
0
    return path;
61
0
}
62
63
// https://www.w3.org/TR/SVG11/shapes.html#LineElementX1Attribute
64
JS::NonnullGCPtr<SVGAnimatedLength> SVGLineElement::x1() const
65
0
{
66
    // FIXME: Populate the unit type when it is parsed (0 here is "unknown").
67
    // FIXME: Create a proper animated value when animations are supported.
68
0
    auto base_length = SVGLength::create(realm(), 0, m_x1.value_or({ 0, false }).value());
69
0
    auto anim_length = SVGLength::create(realm(), 0, m_x1.value_or({ 0, false }).value());
70
0
    return SVGAnimatedLength::create(realm(), move(base_length), move(anim_length));
71
0
}
72
73
// https://www.w3.org/TR/SVG11/shapes.html#LineElementY1Attribute
74
JS::NonnullGCPtr<SVGAnimatedLength> SVGLineElement::y1() const
75
0
{
76
    // FIXME: Populate the unit type when it is parsed (0 here is "unknown").
77
    // FIXME: Create a proper animated value when animations are supported.
78
0
    auto base_length = SVGLength::create(realm(), 0, m_y1.value_or({ 0, false }).value());
79
0
    auto anim_length = SVGLength::create(realm(), 0, m_y1.value_or({ 0, false }).value());
80
0
    return SVGAnimatedLength::create(realm(), move(base_length), move(anim_length));
81
0
}
82
83
// https://www.w3.org/TR/SVG11/shapes.html#LineElementX2Attribute
84
JS::NonnullGCPtr<SVGAnimatedLength> SVGLineElement::x2() const
85
0
{
86
    // FIXME: Populate the unit type when it is parsed (0 here is "unknown").
87
    // FIXME: Create a proper animated value when animations are supported.
88
0
    auto base_length = SVGLength::create(realm(), 0, m_x2.value_or({ 0, false }).value());
89
0
    auto anim_length = SVGLength::create(realm(), 0, m_x2.value_or({ 0, false }).value());
90
0
    return SVGAnimatedLength::create(realm(), move(base_length), move(anim_length));
91
0
}
92
93
// https://www.w3.org/TR/SVG11/shapes.html#LineElementY2Attribute
94
JS::NonnullGCPtr<SVGAnimatedLength> SVGLineElement::y2() const
95
0
{
96
    // FIXME: Populate the unit type when it is parsed (0 here is "unknown").
97
    // FIXME: Create a proper animated value when animations are supported.
98
0
    auto base_length = SVGLength::create(realm(), 0, m_y2.value_or({ 0, false }).value());
99
0
    auto anim_length = SVGLength::create(realm(), 0, m_y2.value_or({ 0, false }).value());
100
0
    return SVGAnimatedLength::create(realm(), move(base_length), move(anim_length));
101
0
}
102
103
}