diff --git a/Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp b/Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp index 4be37611e7..b3980803f9 100644 --- a/Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp +++ b/Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp @@ -47,6 +47,7 @@ static bool is_platform_object(Type const& type) "Instance"sv, "IntersectionObserverEntry"sv, "KeyframeEffect"sv, + "MediaList"sv, "Memory"sv, "MessagePort"sv, "Module"sv, diff --git a/Tests/LibWeb/Text/expected/css/CSSStyleSheet-constructor.txt b/Tests/LibWeb/Text/expected/css/CSSStyleSheet-constructor.txt new file mode 100644 index 0000000000..a12a6c0d90 --- /dev/null +++ b/Tests/LibWeb/Text/expected/css/CSSStyleSheet-constructor.txt @@ -0,0 +1,10 @@ +Empty sheet ownerNode: null +Empty sheet ownerRule: null +Empty sheet title is empty string: true +Empty sheet cssRules is empty: true +Empty sheet is disabled by default: false +cssRules length after insertRule(): 1 +cssRules text: * { font-size: 16px; } +rules and cssRules are the same object: true +cssRules length after deleteRule(): 0 +Disabled sheet is disabled: true diff --git a/Tests/LibWeb/Text/input/css/CSSStyleSheet-constructor.html b/Tests/LibWeb/Text/input/css/CSSStyleSheet-constructor.html new file mode 100644 index 0000000000..70f5c59647 --- /dev/null +++ b/Tests/LibWeb/Text/input/css/CSSStyleSheet-constructor.html @@ -0,0 +1,25 @@ + + + diff --git a/Userland/Libraries/LibWeb/CSS/CSSStyleSheet.cpp b/Userland/Libraries/LibWeb/CSS/CSSStyleSheet.cpp index d970b6fa78..6fd3c26f2b 100644 --- a/Userland/Libraries/LibWeb/CSS/CSSStyleSheet.cpp +++ b/Userland/Libraries/LibWeb/CSS/CSSStyleSheet.cpp @@ -12,6 +12,7 @@ #include #include #include +#include #include namespace Web::CSS { @@ -23,6 +24,72 @@ JS::NonnullGCPtr CSSStyleSheet::create(JS::Realm& realm, CSSRuleL return realm.heap().allocate(realm, realm, rules, media, move(location)); } +// https://drafts.csswg.org/cssom/#dom-cssstylesheet-cssstylesheet +WebIDL::ExceptionOr> CSSStyleSheet::construct_impl(JS::Realm& realm, Optional const& options) +{ + // 1. Construct a new CSSStyleSheet object sheet. + auto sheet = create(realm, CSSRuleList::create_empty(realm), CSS::MediaList::create(realm, {}), {}); + + // 2. Set sheet’s location to the base URL of the associated Document for the current global object. + auto associated_document = sheet->global_object().document(); + sheet->set_location(MUST(associated_document->base_url().to_string())); + + // 3. Set sheet’s stylesheet base URL to the baseURL attribute value from options. + if (options.has_value() && options->base_url.has_value()) { + Optional sheet_location_url; + if (sheet->location().has_value()) + sheet_location_url = sheet->location().release_value(); + + // AD-HOC: This isn't explicitly mentioned in the specification, but multiple modern browsers do this. + AK::URL url = sheet->location().has_value() ? sheet_location_url->complete_url(options->base_url.value()) : options->base_url.value(); + if (!url.is_valid()) + return WebIDL::NotAllowedError::create(realm, "Constructed style sheets must have a valid base URL"_fly_string); + + sheet->set_base_url(url); + } + + // 4. Set sheet’s parent CSS style sheet to null. + sheet->set_parent_css_style_sheet(nullptr); + + // 5. Set sheet’s owner node to null. + sheet->set_owner_node(nullptr); + + // 6. Set sheet’s owner CSS rule to null. + sheet->set_owner_css_rule(nullptr); + + // 7. Set sheet’s title to the the empty string. + sheet->set_title(String {}); + + // 8. Unset sheet’s alternate flag. + sheet->set_alternate(false); + + // 9. Set sheet’s origin-clean flag. + sheet->set_origin_clean(true); + + // 10. Set sheet’s constructed flag. + sheet->set_constructed(true); + + // 11. Set sheet’s Constructor document to the associated Document for the current global object. + sheet->set_constructor_document(associated_document); + + // 12. If the media attribute of options is a string, create a MediaList object from the string and assign it as sheet’s media. + // Otherwise, serialize a media query list from the attribute and then create a MediaList object from the resulting string and set it as sheet’s media. + if (options.has_value()) { + if (options->media.has()) { + sheet->set_media(options->media.get()); + } else { + sheet->m_media = *options->media.get>(); + } + } + + // 13. If the disabled attribute of options is true, set sheet’s disabled flag. + if (options.has_value() && options->disabled) + sheet->set_disabled(true); + + // 14. Return sheet + return sheet; +} + CSSStyleSheet::CSSStyleSheet(JS::Realm& realm, CSSRuleList& rules, MediaList& media, Optional location) : StyleSheet(realm, media) , m_rules(&rules) @@ -53,6 +120,7 @@ void CSSStyleSheet::visit_edges(Cell::Visitor& visitor) visitor.visit(m_rules); visitor.visit(m_owner_css_rule); visitor.visit(m_default_namespace_rule); + visitor.visit(m_constructor_document); for (auto& [key, namespace_rule] : m_namespace_rules) visitor.visit(namespace_rule); } diff --git a/Userland/Libraries/LibWeb/CSS/CSSStyleSheet.h b/Userland/Libraries/LibWeb/CSS/CSSStyleSheet.h index ae1601bfc6..77db67db9c 100644 --- a/Userland/Libraries/LibWeb/CSS/CSSStyleSheet.h +++ b/Userland/Libraries/LibWeb/CSS/CSSStyleSheet.h @@ -18,6 +18,12 @@ namespace Web::CSS { class CSSImportRule; +struct CSSStyleSheetInit { + Optional base_url {}; + Variant, String> media { String {} }; + bool disabled { false }; +}; + class CSSStyleSheet final : public StyleSheet , public Weakable { @@ -26,6 +32,7 @@ class CSSStyleSheet final public: [[nodiscard]] static JS::NonnullGCPtr create(JS::Realm&, CSSRuleList&, MediaList&, Optional location); + static WebIDL::ExceptionOr> construct_impl(JS::Realm&, Optional const& options = {}); virtual ~CSSStyleSheet() override = default; @@ -56,6 +63,14 @@ public: Optional default_namespace() const; Optional namespace_uri(StringView namespace_prefix) const; + Optional base_url() const { return m_base_url; } + void set_base_url(Optional base_url) { m_base_url = move(base_url); } + + bool constructed() const { return m_constructed; } + + JS::GCPtr constructor_document() const { return m_constructor_document; } + void set_constructor_document(JS::GCPtr constructor_document) { m_constructor_document = constructor_document; } + private: CSSStyleSheet(JS::Realm&, CSSRuleList&, MediaList&, Optional location); @@ -64,12 +79,18 @@ private: void recalculate_namespaces(); + void set_constructed(bool constructed) { m_constructed = constructed; } + JS::GCPtr m_rules; JS::GCPtr m_default_namespace_rule; HashMap> m_namespace_rules; JS::GCPtr m_style_sheet_list; JS::GCPtr m_owner_css_rule; + + Optional m_base_url; + JS::GCPtr m_constructor_document; + bool m_constructed { false }; }; } diff --git a/Userland/Libraries/LibWeb/CSS/CSSStyleSheet.idl b/Userland/Libraries/LibWeb/CSS/CSSStyleSheet.idl index 7daae69684..07605d2992 100644 --- a/Userland/Libraries/LibWeb/CSS/CSSStyleSheet.idl +++ b/Userland/Libraries/LibWeb/CSS/CSSStyleSheet.idl @@ -1,11 +1,12 @@ #import #import +#import #import // https://drafts.csswg.org/cssom/#cssstylesheet [Exposed=Window] interface CSSStyleSheet : StyleSheet { - // FIXME: constructor(optional CSSStyleSheetInit options = {}); + constructor(optional CSSStyleSheetInit options = {}); readonly attribute CSSRule? ownerRule; [SameObject] readonly attribute CSSRuleList cssRules; diff --git a/Userland/Libraries/LibWeb/CSS/MediaList.h b/Userland/Libraries/LibWeb/CSS/MediaList.h index 7916a32226..9cac1e0e29 100644 --- a/Userland/Libraries/LibWeb/CSS/MediaList.h +++ b/Userland/Libraries/LibWeb/CSS/MediaList.h @@ -22,7 +22,7 @@ class MediaList final : public Bindings::PlatformObject { public: [[nodiscard]] static JS::NonnullGCPtr create(JS::Realm&, Vector>&&); - ~MediaList() = default; + virtual ~MediaList() override = default; String media_text() const; void set_media_text(StringView); diff --git a/Userland/Libraries/LibWeb/Forward.h b/Userland/Libraries/LibWeb/Forward.h index 17b67200b0..5452bebf6b 100644 --- a/Userland/Libraries/LibWeb/Forward.h +++ b/Userland/Libraries/LibWeb/Forward.h @@ -105,6 +105,7 @@ class CSSRuleList; class CSSStyleDeclaration; class CSSStyleRule; class CSSStyleSheet; +struct CSSStyleSheetInit; class CSSSupportsRule; class CalculatedStyleValue; class Clip;