diff options
author | Andreas Kling <kling@serenityos.org> | 2022-09-19 17:17:20 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2022-09-20 10:32:13 +0200 |
commit | 42b8656db34d795b8153689481c3858bcf328fb2 (patch) | |
tree | 12c39d742991f07df37953aa51e0da55f8e4a3f4 /Userland | |
parent | 5a500827b87bc34bb346153dc5f7e326e4ab72e7 (diff) | |
download | serenity-42b8656db34d795b8153689481c3858bcf328fb2.zip |
LibWeb: Flesh out "document visibility" state a bit more
We can now "update the visibility state", which also causes
`visibilitychange` events to fire on the document.
This still needs GUI integration work at the BrowsingContext level.
Diffstat (limited to 'Userland')
-rw-r--r-- | Userland/Libraries/LibWeb/DOM/Document.cpp | 22 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/DOM/Document.h | 6 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/HTML/EventNames.h | 3 |
3 files changed, 28 insertions, 3 deletions
diff --git a/Userland/Libraries/LibWeb/DOM/Document.cpp b/Userland/Libraries/LibWeb/DOM/Document.cpp index 64fbe989af..3ae99605a0 100644 --- a/Userland/Libraries/LibWeb/DOM/Document.cpp +++ b/Userland/Libraries/LibWeb/DOM/Document.cpp @@ -1578,13 +1578,31 @@ Bindings::LocationObject* Document::location() // https://html.spec.whatwg.org/multipage/interaction.html#dom-document-hidden bool Document::hidden() const { - return false; + return visibility_state() == "hidden"; } // https://html.spec.whatwg.org/multipage/interaction.html#dom-document-visibilitystate String Document::visibility_state() const { - return hidden() ? "hidden" : "visible"; + return m_visibility_state; +} + +// https://html.spec.whatwg.org/multipage/interaction.html#update-the-visibility-state +void Document::update_the_visibility_state(String visibility_state) +{ + // 1. If document's visibility state equals visibilityState, then return. + if (m_visibility_state == visibility_state) + return; + + // 2. Set document's visibility state to visibilityState. + m_visibility_state = visibility_state; + + // FIXME: 3. Run any page visibility change steps which may be defined in other specifications, with visibility state and document. + + // 4. Fire an event named visibilitychange at document, with its bubbles attribute initialized to true. + auto event = DOM::Event::create(window(), HTML::EventNames::visibilitychange); + event->set_bubbles(true); + dispatch_event(event); } // https://drafts.csswg.org/cssom-view/#run-the-resize-steps diff --git a/Userland/Libraries/LibWeb/DOM/Document.h b/Userland/Libraries/LibWeb/DOM/Document.h index b75c00a624..7b280fb2e2 100644 --- a/Userland/Libraries/LibWeb/DOM/Document.h +++ b/Userland/Libraries/LibWeb/DOM/Document.h @@ -317,6 +317,9 @@ public: bool hidden() const; String visibility_state() const; + // https://html.spec.whatwg.org/multipage/interaction.html#update-the-visibility-state + void update_the_visibility_state(String visibility_state); + void run_the_resize_steps(); void run_the_scroll_steps(); @@ -502,6 +505,9 @@ private: // https://html.spec.whatwg.org/#completely-loaded-time Optional<AK::Time> m_completely_loaded_time; + + // https://html.spec.whatwg.org/multipage/interaction.html#visibility-state + String m_visibility_state { "hidden" }; }; } diff --git a/Userland/Libraries/LibWeb/HTML/EventNames.h b/Userland/Libraries/LibWeb/HTML/EventNames.h index 1aaa6eb91d..05fb2bf4d5 100644 --- a/Userland/Libraries/LibWeb/HTML/EventNames.h +++ b/Userland/Libraries/LibWeb/HTML/EventNames.h @@ -57,7 +57,8 @@ namespace Web::HTML::EventNames { __ENUMERATE_HTML_EVENT(submit) \ __ENUMERATE_HTML_EVENT(toggle) \ __ENUMERATE_HTML_EVENT(unhandledrejection) \ - __ENUMERATE_HTML_EVENT(unload) + __ENUMERATE_HTML_EVENT(unload) \ + __ENUMERATE_HTML_EVENT(visibilitychange) #define __ENUMERATE_HTML_EVENT(name) extern FlyString name; ENUMERATE_HTML_EVENTS |