LibWeb: Add method for listing all style sheets on a page

This will be used by the inspector, for showing style sheet contents.

Identifying a specific style sheet is a bit tricky. Depending on where
it came from, a style sheet may have a URL, it might be associated with
a DOM element, both, or neither. This varied information is wrapped in
a new StyleSheetIdentifier struct.
This commit is contained in:
Sam Atkins 2024-08-23 10:42:35 +01:00 committed by Sam Atkins
commit 51a426cc05
Notes: github-actions[bot] 2024-09-03 09:13:10 +00:00
10 changed files with 214 additions and 1 deletions

View file

@ -10,9 +10,12 @@
#include <LibJS/Console.h>
#include <LibJS/Runtime/ConsoleObject.h>
#include <LibWeb/Bindings/MainThreadVM.h>
#include <LibWeb/CSS/CSSImportRule.h>
#include <LibWeb/Cookie/ParsedCookie.h>
#include <LibWeb/DOM/Attr.h>
#include <LibWeb/DOM/NamedNodeMap.h>
#include <LibWeb/HTML/HTMLLinkElement.h>
#include <LibWeb/HTML/HTMLStyleElement.h>
#include <LibWeb/HTML/Scripting/ClassicScript.h>
#include <LibWeb/HTML/TraversableNavigable.h>
#include <LibWeb/Layout/Viewport.h>
@ -739,6 +742,88 @@ void PageClient::did_get_js_console_messages(i32 start_index, Vector<ByteString>
client().async_did_get_js_console_messages(m_id, start_index, move(message_types), move(messages));
}
static void gather_style_sheets(Vector<Web::CSS::StyleSheetIdentifier>& results, Web::CSS::CSSStyleSheet& sheet)
{
Web::CSS::StyleSheetIdentifier identifier {};
bool valid = true;
if (sheet.owner_rule()) {
identifier.type = Web::CSS::StyleSheetIdentifier::Type::ImportRule;
} else if (auto* node = sheet.owner_node()) {
if (node->is_html_style_element() || node->is_svg_style_element()) {
identifier.type = Web::CSS::StyleSheetIdentifier::Type::StyleElement;
} else if (is<Web::HTML::HTMLLinkElement>(node)) {
identifier.type = Web::CSS::StyleSheetIdentifier::Type::LinkElement;
} else {
dbgln("Can't identify where style sheet came from; owner node is {}", node->debug_description());
identifier.type = Web::CSS::StyleSheetIdentifier::Type::StyleElement;
}
identifier.dom_element_unique_id = node->unique_id();
} else {
dbgln("Style sheet has no owner rule or owner node; skipping");
valid = false;
}
if (valid) {
if (auto location = sheet.location(); location.has_value())
identifier.url = location.release_value();
results.append(move(identifier));
}
for (auto& import_rule : sheet.import_rules()) {
if (import_rule->loaded_style_sheet()) {
gather_style_sheets(results, *import_rule->loaded_style_sheet());
} else {
// We can gather this anyway, and hope it loads later
results.append({ .type = Web::CSS::StyleSheetIdentifier::Type::ImportRule,
.url = MUST(import_rule->url().to_string()) });
}
}
}
Vector<Web::CSS::StyleSheetIdentifier> PageClient::list_style_sheets() const
{
Vector<Web::CSS::StyleSheetIdentifier> results;
auto const* document = page().top_level_browsing_context().active_document();
if (document) {
for (auto& sheet : document->style_sheets().sheets()) {
gather_style_sheets(results, sheet);
}
}
// User style
if (page().user_style().has_value()) {
results.append({
.type = Web::CSS::StyleSheetIdentifier::Type::UserStyle,
});
}
// User-agent
results.append({
.type = Web::CSS::StyleSheetIdentifier::Type::UserAgent,
.url = "CSS/Default.css"_string,
});
if (document && document->in_quirks_mode()) {
results.append({
.type = Web::CSS::StyleSheetIdentifier::Type::UserAgent,
.url = "CSS/QuirksMode.css"_string,
});
}
results.append({
.type = Web::CSS::StyleSheetIdentifier::Type::UserAgent,
.url = "MathML/Default.css"_string,
});
results.append({
.type = Web::CSS::StyleSheetIdentifier::Type::UserAgent,
.url = "SVG/Default.css"_string,
});
return results;
}
Web::DisplayListPlayerType PageClient::display_list_player_type() const
{
switch (s_use_skia_painter) {