Coverage Report

Created: 2025-09-05 06:52

/src/serenity/Userland/Libraries/LibWeb/CSS/VisualViewport.cpp
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 2023, Andreas Kling <kling@serenityos.org>
3
 *
4
 * SPDX-License-Identifier: BSD-2-Clause
5
 */
6
7
#include <LibWeb/Bindings/Intrinsics.h>
8
#include <LibWeb/Bindings/VisualViewportPrototype.h>
9
#include <LibWeb/CSS/VisualViewport.h>
10
#include <LibWeb/DOM/Document.h>
11
#include <LibWeb/DOM/EventDispatcher.h>
12
#include <LibWeb/DOM/IDLEventListener.h>
13
#include <LibWeb/HTML/EventHandler.h>
14
#include <LibWeb/HTML/EventNames.h>
15
16
namespace Web::CSS {
17
18
JS_DEFINE_ALLOCATOR(VisualViewport);
19
20
JS::NonnullGCPtr<VisualViewport> VisualViewport::create(DOM::Document& document)
21
0
{
22
0
    return document.heap().allocate<VisualViewport>(document.realm(), document);
23
0
}
24
25
VisualViewport::VisualViewport(DOM::Document& document)
26
0
    : DOM::EventTarget(document.realm())
27
0
    , m_document(document)
28
0
{
29
0
}
30
31
void VisualViewport::initialize(JS::Realm& realm)
32
0
{
33
0
    Base::initialize(realm);
34
0
    WEB_SET_PROTOTYPE_FOR_INTERFACE(VisualViewport);
35
0
}
36
37
void VisualViewport::visit_edges(Cell::Visitor& visitor)
38
0
{
39
0
    Base::visit_edges(visitor);
40
0
    visitor.visit(m_document);
41
0
}
42
43
// https://drafts.csswg.org/cssom-view/#dom-visualviewport-offsetleft
44
double VisualViewport::offset_left() const
45
0
{
46
    // 1. If the visual viewport’s associated document is not fully active, return 0.
47
0
    if (!m_document->is_fully_active())
48
0
        return 0;
49
50
    // 2. Otherwise, return the offset of the left edge of the visual viewport from the left edge of the layout viewport.
51
0
    VERIFY(m_document->navigable());
52
0
    return m_document->viewport_rect().left().to_double();
53
0
}
54
55
// https://drafts.csswg.org/cssom-view/#dom-visualviewport-offsettop
56
double VisualViewport::offset_top() const
57
0
{
58
    // 1. If the visual viewport’s associated document is not fully active, return 0.
59
0
    if (!m_document->is_fully_active())
60
0
        return 0;
61
62
    // 2. Otherwise, return the offset of the top edge of the visual viewport from the top edge of the layout viewport.
63
0
    VERIFY(m_document->navigable());
64
0
    return m_document->viewport_rect().top().to_double();
65
0
}
66
67
// https://drafts.csswg.org/cssom-view/#dom-visualviewport-pageleft
68
double VisualViewport::page_left() const
69
0
{
70
    // 1. If the visual viewport’s associated document is not fully active, return 0.
71
0
    if (!m_document->is_fully_active())
72
0
        return 0;
73
74
    // FIXME: 2. Otherwise, return the offset of the left edge of the visual viewport
75
    //           from the left edge of the initial containing block of the layout viewport’s document.
76
0
    return offset_left();
77
0
}
78
79
// https://drafts.csswg.org/cssom-view/#dom-visualviewport-pagetop
80
double VisualViewport::page_top() const
81
0
{
82
    // 1. If the visual viewport’s associated document is not fully active, return 0.
83
0
    if (!m_document->is_fully_active())
84
0
        return 0;
85
86
    // FIXME: 2. Otherwise, return the offset of the top edge of the visual viewport
87
    //           from the top edge of the initial containing block of the layout viewport’s document.
88
0
    return offset_top();
89
0
}
90
91
// https://drafts.csswg.org/cssom-view/#dom-visualviewport-width
92
double VisualViewport::width() const
93
0
{
94
    // 1. If the visual viewport’s associated document is not fully active, return 0.
95
0
    if (!m_document->is_fully_active())
96
0
        return 0;
97
98
    // 2. Otherwise, return the width of the visual viewport
99
    //    FIXME: excluding the width of any rendered vertical classic scrollbar that is fixed to the visual viewport.
100
0
    VERIFY(m_document->navigable());
101
0
    return m_document->viewport_rect().width().to_double();
102
0
}
103
104
// https://drafts.csswg.org/cssom-view/#dom-visualviewport-height
105
double VisualViewport::height() const
106
0
{
107
    // 1. If the visual viewport’s associated document is not fully active, return 0.
108
0
    if (!m_document->is_fully_active())
109
0
        return 0;
110
111
    // 2. Otherwise, return the height of the visual viewport
112
    //    FIXME: excluding the height of any rendered vertical classic scrollbar that is fixed to the visual viewport.
113
0
    VERIFY(m_document->navigable());
114
0
    return m_document->viewport_rect().height().to_double();
115
0
}
116
117
// https://drafts.csswg.org/cssom-view/#dom-visualviewport-scale
118
double VisualViewport::scale() const
119
0
{
120
    // FIXME: Implement.
121
0
    return 1;
122
0
}
123
124
void VisualViewport::set_onresize(WebIDL::CallbackType* event_handler)
125
0
{
126
0
    set_event_handler_attribute(HTML::EventNames::resize, event_handler);
127
0
}
128
129
WebIDL::CallbackType* VisualViewport::onresize()
130
0
{
131
0
    return event_handler_attribute(HTML::EventNames::resize);
132
0
}
133
134
void VisualViewport::set_onscroll(WebIDL::CallbackType* event_handler)
135
0
{
136
0
    set_event_handler_attribute(HTML::EventNames::scroll, event_handler);
137
0
}
138
139
WebIDL::CallbackType* VisualViewport::onscroll()
140
0
{
141
0
    return event_handler_attribute(HTML::EventNames::scroll);
142
0
}
143
144
void VisualViewport::set_onscrollend(WebIDL::CallbackType* event_handler)
145
0
{
146
0
    set_event_handler_attribute(HTML::EventNames::scrollend, event_handler);
147
0
}
148
149
WebIDL::CallbackType* VisualViewport::onscrollend()
150
0
{
151
0
    return event_handler_attribute(HTML::EventNames::scrollend);
152
0
}
153
154
}