mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 14:42:44 +00:00 
			
		
		
		
	LibWeb: Add CSSStyleSheet constructor binding
This commit is contained in:
		
							parent
							
								
									f303905875
								
							
						
					
					
						commit
						b0f57a2785
					
				
					 8 changed files with 129 additions and 2 deletions
				
			
		|  | @ -47,6 +47,7 @@ static bool is_platform_object(Type const& type) | ||||||
|         "Instance"sv, |         "Instance"sv, | ||||||
|         "IntersectionObserverEntry"sv, |         "IntersectionObserverEntry"sv, | ||||||
|         "KeyframeEffect"sv, |         "KeyframeEffect"sv, | ||||||
|  |         "MediaList"sv, | ||||||
|         "Memory"sv, |         "Memory"sv, | ||||||
|         "MessagePort"sv, |         "MessagePort"sv, | ||||||
|         "Module"sv, |         "Module"sv, | ||||||
|  |  | ||||||
							
								
								
									
										10
									
								
								Tests/LibWeb/Text/expected/css/CSSStyleSheet-constructor.txt
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								Tests/LibWeb/Text/expected/css/CSSStyleSheet-constructor.txt
									
										
									
									
									
										Normal 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 | ||||||
							
								
								
									
										25
									
								
								Tests/LibWeb/Text/input/css/CSSStyleSheet-constructor.html
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								Tests/LibWeb/Text/input/css/CSSStyleSheet-constructor.html
									
										
									
									
									
										Normal 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> | ||||||
|  | @ -12,6 +12,7 @@ | ||||||
| #include <LibWeb/CSS/StyleComputer.h> | #include <LibWeb/CSS/StyleComputer.h> | ||||||
| #include <LibWeb/CSS/StyleSheetList.h> | #include <LibWeb/CSS/StyleSheetList.h> | ||||||
| #include <LibWeb/DOM/Document.h> | #include <LibWeb/DOM/Document.h> | ||||||
|  | #include <LibWeb/HTML/Window.h> | ||||||
| #include <LibWeb/WebIDL/ExceptionOr.h> | #include <LibWeb/WebIDL/ExceptionOr.h> | ||||||
| 
 | 
 | ||||||
| namespace Web::CSS { | 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)); |     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 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<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 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<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 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<AK::URL> location) | CSSStyleSheet::CSSStyleSheet(JS::Realm& realm, CSSRuleList& rules, MediaList& media, Optional<AK::URL> location) | ||||||
|     : StyleSheet(realm, media) |     : StyleSheet(realm, media) | ||||||
|     , m_rules(&rules) |     , m_rules(&rules) | ||||||
|  | @ -53,6 +120,7 @@ void CSSStyleSheet::visit_edges(Cell::Visitor& visitor) | ||||||
|     visitor.visit(m_rules); |     visitor.visit(m_rules); | ||||||
|     visitor.visit(m_owner_css_rule); |     visitor.visit(m_owner_css_rule); | ||||||
|     visitor.visit(m_default_namespace_rule); |     visitor.visit(m_default_namespace_rule); | ||||||
|  |     visitor.visit(m_constructor_document); | ||||||
|     for (auto& [key, namespace_rule] : m_namespace_rules) |     for (auto& [key, namespace_rule] : m_namespace_rules) | ||||||
|         visitor.visit(namespace_rule); |         visitor.visit(namespace_rule); | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -18,6 +18,12 @@ namespace Web::CSS { | ||||||
| 
 | 
 | ||||||
| class CSSImportRule; | class CSSImportRule; | ||||||
| 
 | 
 | ||||||
|  | struct CSSStyleSheetInit { | ||||||
|  |     Optional<String> base_url {}; | ||||||
|  |     Variant<JS::Handle<MediaList>, String> media { String {} }; | ||||||
|  |     bool disabled { false }; | ||||||
|  | }; | ||||||
|  | 
 | ||||||
| class CSSStyleSheet final | class CSSStyleSheet final | ||||||
|     : public StyleSheet |     : public StyleSheet | ||||||
|     , public Weakable<CSSStyleSheet> { |     , public Weakable<CSSStyleSheet> { | ||||||
|  | @ -26,6 +32,7 @@ class CSSStyleSheet final | ||||||
| 
 | 
 | ||||||
| public: | public: | ||||||
|     [[nodiscard]] static JS::NonnullGCPtr<CSSStyleSheet> create(JS::Realm&, CSSRuleList&, MediaList&, Optional<AK::URL> location); |     [[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; |     virtual ~CSSStyleSheet() override = default; | ||||||
| 
 | 
 | ||||||
|  | @ -56,6 +63,14 @@ public: | ||||||
|     Optional<FlyString> default_namespace() const; |     Optional<FlyString> default_namespace() const; | ||||||
|     Optional<FlyString> namespace_uri(StringView namespace_prefix) 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: | private: | ||||||
|     CSSStyleSheet(JS::Realm&, CSSRuleList&, MediaList&, Optional<AK::URL> location); |     CSSStyleSheet(JS::Realm&, CSSRuleList&, MediaList&, Optional<AK::URL> location); | ||||||
| 
 | 
 | ||||||
|  | @ -64,12 +79,18 @@ private: | ||||||
| 
 | 
 | ||||||
|     void recalculate_namespaces(); |     void recalculate_namespaces(); | ||||||
| 
 | 
 | ||||||
|  |     void set_constructed(bool constructed) { m_constructed = constructed; } | ||||||
|  | 
 | ||||||
|     JS::GCPtr<CSSRuleList> m_rules; |     JS::GCPtr<CSSRuleList> m_rules; | ||||||
|     JS::GCPtr<CSSNamespaceRule> m_default_namespace_rule; |     JS::GCPtr<CSSNamespaceRule> m_default_namespace_rule; | ||||||
|     HashMap<FlyString, JS::GCPtr<CSSNamespaceRule>> m_namespace_rules; |     HashMap<FlyString, JS::GCPtr<CSSNamespaceRule>> m_namespace_rules; | ||||||
| 
 | 
 | ||||||
|     JS::GCPtr<StyleSheetList> m_style_sheet_list; |     JS::GCPtr<StyleSheetList> m_style_sheet_list; | ||||||
|     JS::GCPtr<CSSRule> m_owner_css_rule; |     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 }; | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -1,11 +1,12 @@ | ||||||
| #import <CSS/CSSRule.idl> | #import <CSS/CSSRule.idl> | ||||||
| #import <CSS/CSSRuleList.idl> | #import <CSS/CSSRuleList.idl> | ||||||
|  | #import <CSS/MediaList.idl> | ||||||
| #import <CSS/StyleSheet.idl> | #import <CSS/StyleSheet.idl> | ||||||
| 
 | 
 | ||||||
| // https://drafts.csswg.org/cssom/#cssstylesheet | // https://drafts.csswg.org/cssom/#cssstylesheet | ||||||
| [Exposed=Window] | [Exposed=Window] | ||||||
| interface CSSStyleSheet : StyleSheet { | interface CSSStyleSheet : StyleSheet { | ||||||
|     // FIXME: constructor(optional CSSStyleSheetInit options = {}); |     constructor(optional CSSStyleSheetInit options = {}); | ||||||
| 
 | 
 | ||||||
|     readonly attribute CSSRule? ownerRule; |     readonly attribute CSSRule? ownerRule; | ||||||
|     [SameObject] readonly attribute CSSRuleList cssRules; |     [SameObject] readonly attribute CSSRuleList cssRules; | ||||||
|  |  | ||||||
|  | @ -22,7 +22,7 @@ class MediaList final : public Bindings::PlatformObject { | ||||||
| 
 | 
 | ||||||
| public: | public: | ||||||
|     [[nodiscard]] static JS::NonnullGCPtr<MediaList> create(JS::Realm&, Vector<NonnullRefPtr<MediaQuery>>&&); |     [[nodiscard]] static JS::NonnullGCPtr<MediaList> create(JS::Realm&, Vector<NonnullRefPtr<MediaQuery>>&&); | ||||||
|     ~MediaList() = default; |     virtual ~MediaList() override = default; | ||||||
| 
 | 
 | ||||||
|     String media_text() const; |     String media_text() const; | ||||||
|     void set_media_text(StringView); |     void set_media_text(StringView); | ||||||
|  |  | ||||||
|  | @ -105,6 +105,7 @@ class CSSRuleList; | ||||||
| class CSSStyleDeclaration; | class CSSStyleDeclaration; | ||||||
| class CSSStyleRule; | class CSSStyleRule; | ||||||
| class CSSStyleSheet; | class CSSStyleSheet; | ||||||
|  | struct CSSStyleSheetInit; | ||||||
| class CSSSupportsRule; | class CSSSupportsRule; | ||||||
| class CalculatedStyleValue; | class CalculatedStyleValue; | ||||||
| class Clip; | class Clip; | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Tim Ledbetter
						Tim Ledbetter