Skip to content

Commit

Permalink
LibWeb: Add Document::get_style_sheet_source()
Browse files Browse the repository at this point in the history
This returns the source text of the specified style sheet. StyleComputer
now exposes user agent style sheets so that these can also be requested.

(cherry picked from commit 49b2eb5f51be2ddfd2c6009e7d8064b58d665af3)
  • Loading branch information
AtkinsSJ authored and nico committed Nov 16, 2024
1 parent 2e4c8aa commit 4cf75e5
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 4 deletions.
20 changes: 19 additions & 1 deletion Userland/Libraries/LibWeb/CSS/StyleComputer.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* Copyright (c) 2018-2023, Andreas Kling <[email protected]>
* Copyright (c) 2021, the SerenityOS developers.
* Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
* Copyright (c) 2021-2024, Sam Atkins <sam@ladybird.org>
* Copyright (c) 2024, Matthew Olsson <[email protected]>
*
* SPDX-License-Identifier: BSD-2-Clause
Expand Down Expand Up @@ -260,6 +260,24 @@ static CSSStyleSheet& svg_stylesheet(DOM::Document const& document)
return *sheet;
}

Optional<String> StyleComputer::user_agent_style_sheet_source(StringView name)
{
extern String default_stylesheet_source;
extern String quirks_mode_stylesheet_source;
extern String mathml_stylesheet_source;
extern String svg_stylesheet_source;

if (name == "CSS/Default.css"sv)
return default_stylesheet_source;
if (name == "CSS/QuirksMode.css"sv)
return quirks_mode_stylesheet_source;
if (name == "MathML/Default.css"sv)
return mathml_stylesheet_source;
if (name == "SVG/Default.css"sv)
return svg_stylesheet_source;
return {};
}

template<typename Callback>
void StyleComputer::for_each_stylesheet(CascadeOrigin cascade_origin, Callback callback) const
{
Expand Down
4 changes: 3 additions & 1 deletion Userland/Libraries/LibWeb/CSS/StyleComputer.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright (c) 2018-2024, Andreas Kling <[email protected]>
* Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
* Copyright (c) 2021-2024, Sam Atkins <sam@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
Expand Down Expand Up @@ -117,6 +117,8 @@ class StyleComputer {
static void set_property_expanding_shorthands(StyleProperties&, PropertyID, CSSStyleValue const&, CSS::CSSStyleDeclaration const*, StyleProperties const& style_for_revert, StyleProperties const& style_for_revert_layer, Important = Important::No);
static NonnullRefPtr<CSSStyleValue const> get_inherit_value(JS::Realm& initial_value_context_realm, CSS::PropertyID, DOM::Element const*, Optional<CSS::Selector::PseudoElement::Type> = {});

static Optional<String> user_agent_style_sheet_source(StringView name);

explicit StyleComputer(DOM::Document&);
~StyleComputer();

Expand Down
76 changes: 74 additions & 2 deletions Userland/Libraries/LibWeb/DOM/Document.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Copyright (c) 2018-2024, Andreas Kling <[email protected]>
* Copyright (c) 2021-2023, Linus Groh <[email protected]>
* Copyright (c) 2021-2023, Luke Wilde <[email protected]>
* Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
* Copyright (c) 2021-2024, Sam Atkins <sam@ladybird.org>
* Copyright (c) 2024, Matthew Olsson <[email protected]>
*
* SPDX-License-Identifier: BSD-2-Clause
Expand All @@ -26,10 +26,12 @@
#include <LibWeb/Bindings/MainThreadVM.h>
#include <LibWeb/CSS/AnimationEvent.h>
#include <LibWeb/CSS/CSSAnimation.h>
#include <LibWeb/CSS/CSSImportRule.h>
#include <LibWeb/CSS/FontFaceSet.h>
#include <LibWeb/CSS/MediaQueryList.h>
#include <LibWeb/CSS/MediaQueryListEvent.h>
#include <LibWeb/CSS/StyleComputer.h>
#include <LibWeb/CSS/StyleSheetIdentifier.h>
#include <LibWeb/CSS/SystemColor.h>
#include <LibWeb/CSS/VisualViewport.h>
#include <LibWeb/Cookie/ParsedCookie.h>
Expand Down Expand Up @@ -84,6 +86,7 @@
#include <LibWeb/HTML/HTMLLinkElement.h>
#include <LibWeb/HTML/HTMLObjectElement.h>
#include <LibWeb/HTML/HTMLScriptElement.h>
#include <LibWeb/HTML/HTMLStyleElement.h>
#include <LibWeb/HTML/HTMLTitleElement.h>
#include <LibWeb/HTML/HashChangeEvent.h>
#include <LibWeb/HTML/ListOfAvailableImages.h>
Expand Down Expand Up @@ -119,8 +122,8 @@
#include <LibWeb/ResizeObserver/ResizeObserverEntry.h>
#include <LibWeb/SVG/SVGDecodedImageData.h>
#include <LibWeb/SVG/SVGElement.h>
#include <LibWeb/SVG/SVGStyleElement.h>
#include <LibWeb/SVG/SVGTitleElement.h>
#include <LibWeb/SVG/TagNames.h>
#include <LibWeb/Selection/Selection.h>
#include <LibWeb/UIEvents/CompositionEvent.h>
#include <LibWeb/UIEvents/EventNames.h>
Expand Down Expand Up @@ -5168,6 +5171,75 @@ void Document::for_each_active_css_style_sheet(Function<void(CSS::CSSStyleSheet&
}
}

static Optional<CSS::CSSStyleSheet&> find_style_sheet_with_url(String const& url, CSS::CSSStyleSheet& style_sheet)
{
if (style_sheet.location() == url)
return style_sheet;

for (auto& import_rule : style_sheet.import_rules()) {
if (import_rule->loaded_style_sheet()) {
if (auto match = find_style_sheet_with_url(url, *import_rule->loaded_style_sheet()); match.has_value())
return match;
}
}

return {};
}

Optional<String> Document::get_style_sheet_source(CSS::StyleSheetIdentifier const& identifier) const
{
switch (identifier.type) {
case CSS::StyleSheetIdentifier::Type::StyleElement:
if (identifier.dom_element_unique_id.has_value()) {
if (auto* node = Node::from_unique_id(*identifier.dom_element_unique_id)) {
if (node->is_html_style_element()) {
if (auto* sheet = verify_cast<HTML::HTMLStyleElement>(*node).sheet())
return sheet->source_text({});
}
if (node->is_svg_style_element()) {
if (auto* sheet = verify_cast<SVG::SVGStyleElement>(*node).sheet())
return sheet->source_text({});
}
}
}
return {};
case CSS::StyleSheetIdentifier::Type::LinkElement:
case CSS::StyleSheetIdentifier::Type::ImportRule: {
if (!identifier.url.has_value()) {
dbgln("Attempting to get link or imported style-sheet with no url; giving up");
return {};
}

if (m_style_sheets) {
for (auto& style_sheet : m_style_sheets->sheets()) {
if (auto match = find_style_sheet_with_url(identifier.url.value(), style_sheet); match.has_value())
return match->source_text({});
}
}

if (m_adopted_style_sheets) {
Optional<String> result;
m_adopted_style_sheets->for_each<CSS::CSSStyleSheet>([&](auto& style_sheet) {
if (result.has_value())
return;

if (auto match = find_style_sheet_with_url(identifier.url.value(), style_sheet); match.has_value())
result = match->source_text({});
});
return result;
}

return {};
}
case CSS::StyleSheetIdentifier::Type::UserAgent:
return CSS::StyleComputer::user_agent_style_sheet_source(identifier.url.value());
case CSS::StyleSheetIdentifier::Type::UserStyle:
return page().user_style();
}

return {};
}

void Document::register_shadow_root(Badge<DOM::ShadowRoot>, DOM::ShadowRoot& shadow_root)
{
m_shadow_roots.append(shadow_root);
Expand Down
2 changes: 2 additions & 0 deletions Userland/Libraries/LibWeb/DOM/Document.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ class Document

CSS::StyleSheetList* style_sheets_for_bindings() { return &style_sheets(); }

Optional<String> get_style_sheet_source(CSS::StyleSheetIdentifier const&) const;

virtual FlyString node_name() const override { return "#document"_fly_string; }

void set_hovered_node(Node*);
Expand Down

0 comments on commit 4cf75e5

Please sign in to comment.