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
Notes: sideshowbarker 2024-07-17 06:45:52 +09:00
8 changed files with 129 additions and 2 deletions

View file

@ -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,

View file

@ -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

View file

@ -0,0 +1,25 @@
<!DOCTYPE html>
<script src="../include.js"></script>
<script>
test(() => {
const cssRule = "* { font-size: 16px; }";
const sheet = new CSSStyleSheet();
println(`Empty sheet ownerNode: ${sheet.ownerNode}`);
println(`Empty sheet ownerRule: ${sheet.ownerRule}`);
println(`Empty sheet title is empty string: ${sheet.title === ""}`);
println(`Empty sheet cssRules is empty: ${sheet.cssRules.length === 0}`);
println(`Empty sheet is disabled by default: ${sheet.disabled}`);
sheet.insertRule(cssRule);
println(`cssRules length after insertRule(): ${sheet.cssRules.length}`);
println(`cssRules text: ${sheet.cssRules[0].cssText}`);
println(`rules and cssRules are the same object: ${sheet.cssRules === sheet.rules}`);
sheet.deleteRule(0);
println(`cssRules length after deleteRule(): ${sheet.cssRules.length}`);
const disabledSheet = new CSSStyleSheet({ disabled: true });
println(`Disabled sheet is disabled: ${disabledSheet.disabled}`);
});
</script>

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);
}

View file

@ -18,6 +18,12 @@ namespace Web::CSS {
class CSSImportRule;
struct CSSStyleSheetInit {
Optional<String> base_url {};
Variant<JS::Handle<MediaList>, String> media { String {} };
bool disabled { false };
};
class CSSStyleSheet final
: public StyleSheet
, public Weakable<CSSStyleSheet> {
@ -26,6 +32,7 @@ class CSSStyleSheet final
public:
[[nodiscard]] static JS::NonnullGCPtr<CSSStyleSheet> create(JS::Realm&, CSSRuleList&, MediaList&, Optional<AK::URL> location);
static WebIDL::ExceptionOr<JS::NonnullGCPtr<CSSStyleSheet>> construct_impl(JS::Realm&, Optional<CSSStyleSheetInit> const& options = {});
virtual ~CSSStyleSheet() override = default;
@ -56,6 +63,14 @@ public:
Optional<FlyString> default_namespace() const;
Optional<FlyString> namespace_uri(StringView namespace_prefix) const;
Optional<AK::URL> base_url() const { return m_base_url; }
void set_base_url(Optional<AK::URL> base_url) { m_base_url = move(base_url); }
bool constructed() const { return m_constructed; }
JS::GCPtr<DOM::Document const> constructor_document() const { return m_constructor_document; }
void set_constructor_document(JS::GCPtr<DOM::Document const> constructor_document) { m_constructor_document = constructor_document; }
private:
CSSStyleSheet(JS::Realm&, CSSRuleList&, MediaList&, Optional<AK::URL> location);
@ -64,12 +79,18 @@ private:
void recalculate_namespaces();
void set_constructed(bool constructed) { m_constructed = constructed; }
JS::GCPtr<CSSRuleList> m_rules;
JS::GCPtr<CSSNamespaceRule> m_default_namespace_rule;
HashMap<FlyString, JS::GCPtr<CSSNamespaceRule>> m_namespace_rules;
JS::GCPtr<StyleSheetList> m_style_sheet_list;
JS::GCPtr<CSSRule> m_owner_css_rule;
Optional<AK::URL> m_base_url;
JS::GCPtr<DOM::Document const> m_constructor_document;
bool m_constructed { false };
};
}

View file

@ -1,11 +1,12 @@
#import <CSS/CSSRule.idl>
#import <CSS/CSSRuleList.idl>
#import <CSS/MediaList.idl>
#import <CSS/StyleSheet.idl>
// 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;

View file

@ -22,7 +22,7 @@ class MediaList final : public Bindings::PlatformObject {
public:
[[nodiscard]] static JS::NonnullGCPtr<MediaList> create(JS::Realm&, Vector<NonnullRefPtr<MediaQuery>>&&);
~MediaList() = default;
virtual ~MediaList() override = default;
String media_text() const;
void set_media_text(StringView);

View file

@ -105,6 +105,7 @@ class CSSRuleList;
class CSSStyleDeclaration;
class CSSStyleRule;
class CSSStyleSheet;
struct CSSStyleSheetInit;
class CSSSupportsRule;
class CalculatedStyleValue;
class Clip;