1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 10:27:35 +00:00

LibWeb: Add support for "User" CascadeOrigin

User styles are applied after the UserAgent's built-in styles, and
before the Author styles that are part of the web page.

Because they're neither part of the page, but can still be modified
while the page is open, caching is a little tricky. The approach here
is to piggy-back on the StyleComputer's rule caches, which already get
rebuilt whenever the styles change. This is not the smartest approach,
since it means re-parsing the style sheet more often than is necessary,
but it's simple and works. :^)
This commit is contained in:
Sam Atkins 2023-08-21 15:50:01 +01:00 committed by Andreas Kling
parent 7bc5949e35
commit 6dcd8d4a2c
4 changed files with 46 additions and 5 deletions

View file

@ -9,6 +9,7 @@
#include <AK/SourceLocation.h>
#include <LibIPC/Decoder.h>
#include <LibIPC/Encoder.h>
#include <LibWeb/CSS/StyleComputer.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/HTML/BrowsingContext.h>
#include <LibWeb/HTML/EventLoop/EventLoop.h>
@ -372,6 +373,14 @@ JS::GCPtr<HTML::HTMLMediaElement> Page::media_context_menu_element()
return static_cast<HTML::HTMLMediaElement*>(dom_node);
}
void Page::set_user_style(String source)
{
m_user_style_sheet_source = source;
if (top_level_browsing_context_is_initialized() && top_level_browsing_context().active_document()) {
top_level_browsing_context().active_document()->style_computer().invalidate_rule_cache();
}
}
}
template<>

View file

@ -135,6 +135,9 @@ public:
WebIDL::ExceptionOr<void> toggle_media_loop_state();
WebIDL::ExceptionOr<void> toggle_media_controls_state();
Optional<String> const& user_style() const { return m_user_style_sheet_source; }
void set_user_style(String source);
bool pdf_viewer_supported() const { return m_pdf_viewer_supported; }
private:
@ -167,6 +170,8 @@ private:
Optional<int> m_media_context_menu_element_id;
Optional<String> m_user_style_sheet_source;
// https://html.spec.whatwg.org/multipage/system-state.html#pdf-viewer-supported
// Each user agent has a PDF viewer supported boolean, whose value is implementation-defined (and might vary according to user preferences).
// Spec Note: This value also impacts the navigation processing model.