1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 12:48:10 +00:00

LibWeb: Add CSSStyleSheet constructor binding

This commit is contained in:
Tim Ledbetter 2024-02-24 07:46:59 +00:00 committed by Andreas Kling
parent f303905875
commit b0f57a2785
8 changed files with 129 additions and 2 deletions

View file

@ -12,6 +12,7 @@
#include <LibWeb/CSS/StyleComputer.h>
#include <LibWeb/CSS/StyleSheetList.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/HTML/Window.h>
#include <LibWeb/WebIDL/ExceptionOr.h>
namespace Web::CSS {
@ -23,6 +24,72 @@ JS::NonnullGCPtr<CSSStyleSheet> CSSStyleSheet::create(JS::Realm& realm, CSSRuleL
return realm.heap().allocate<CSSStyleSheet>(realm, realm, rules, media, move(location));
}
// https://drafts.csswg.org/cssom/#dom-cssstylesheet-cssstylesheet
WebIDL::ExceptionOr<JS::NonnullGCPtr<CSSStyleSheet>> CSSStyleSheet::construct_impl(JS::Realm& realm, Optional<CSSStyleSheetInit> const& options)
{
// 1. Construct a new CSSStyleSheet object sheet.
auto sheet = create(realm, CSSRuleList::create_empty(realm), CSS::MediaList::create(realm, {}), {});
// 2. Set sheets 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 sheets stylesheet base URL to the baseURL attribute value from options.
if (options.has_value() && options->base_url.has_value()) {
Optional<AK::URL> 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 sheets parent CSS style sheet to null.
sheet->set_parent_css_style_sheet(nullptr);
// 5. Set sheets owner node to null.
sheet->set_owner_node(nullptr);
// 6. Set sheets owner CSS rule to null.
sheet->set_owner_css_rule(nullptr);
// 7. Set sheets title to the the empty string.
sheet->set_title(String {});
// 8. Unset sheets alternate flag.
sheet->set_alternate(false);
// 9. Set sheets origin-clean flag.
sheet->set_origin_clean(true);
// 10. Set sheets constructed flag.
sheet->set_constructed(true);
// 11. Set sheets 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 sheets media.
// Otherwise, serialize a media query list from the attribute and then create a MediaList object from the resulting string and set it as sheets media.
if (options.has_value()) {
if (options->media.has<String>()) {
sheet->set_media(options->media.get<String>());
} else {
sheet->m_media = *options->media.get<JS::Handle<MediaList>>();
}
}
// 13. If the disabled attribute of options is true, set sheets 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<AK::URL> 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);
}