diff --git a/Userland/Libraries/LibWeb/CMakeLists.txt b/Userland/Libraries/LibWeb/CMakeLists.txt index 795c833c16..93df3612e3 100644 --- a/Userland/Libraries/LibWeb/CMakeLists.txt +++ b/Userland/Libraries/LibWeb/CMakeLists.txt @@ -280,6 +280,9 @@ function(libweb_js_wrapper class) add_custom_target(generate_${basename}Prototype.cpp DEPENDS Bindings/${class}Prototype.cpp) endfunction() +libweb_js_wrapper(CSS/CSSStyleSheet) +libweb_js_wrapper(CSS/StyleSheet) +libweb_js_wrapper(CSS/StyleSheetList) libweb_js_wrapper(DOM/CharacterData) libweb_js_wrapper(DOM/Comment) libweb_js_wrapper(DOM/Document) diff --git a/Userland/Libraries/LibWeb/CSS/CSSStyleSheet.h b/Userland/Libraries/LibWeb/CSS/CSSStyleSheet.h index a1b3e9f784..11b9cc6a99 100644 --- a/Userland/Libraries/LibWeb/CSS/CSSStyleSheet.h +++ b/Userland/Libraries/LibWeb/CSS/CSSStyleSheet.h @@ -37,6 +37,8 @@ namespace Web::CSS { class CSSStyleSheet final : public StyleSheet { public: + using WrapperType = Bindings::CSSStyleSheetWrapper; + static NonnullRefPtr create(NonnullRefPtrVector rules) { return adopt(*new CSSStyleSheet(move(rules))); @@ -86,3 +88,9 @@ private: }; } + +namespace Web::Bindings { + +CSSStyleSheetWrapper* wrap(JS::GlobalObject&, CSS::CSSStyleSheet&); + +} diff --git a/Userland/Libraries/LibWeb/CSS/CSSStyleSheet.idl b/Userland/Libraries/LibWeb/CSS/CSSStyleSheet.idl new file mode 100644 index 0000000000..bf02535dc4 --- /dev/null +++ b/Userland/Libraries/LibWeb/CSS/CSSStyleSheet.idl @@ -0,0 +1,6 @@ +interface CSSStyleSheet : StyleSheet { + // readonly attribute CSSRule? ownerRule; + // [SameObject] readonly attribute CSSRuleList cssRules; + // unsigned long insertRule(CSSOMString rule, optional unsigned long index = 0); + // undefined deleteRule(unsigned long index); +}; diff --git a/Userland/Libraries/LibWeb/CSS/StyleSheet.h b/Userland/Libraries/LibWeb/CSS/StyleSheet.h index 1bca84210c..df6fb735ff 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleSheet.h +++ b/Userland/Libraries/LibWeb/CSS/StyleSheet.h @@ -28,11 +28,17 @@ #pragma once #include +#include +#include namespace Web::CSS { -class StyleSheet : public RefCounted { +class StyleSheet + : public RefCounted + , public Bindings::Wrappable { public: + using WrapperType = Bindings::StyleSheetWrapper; + virtual ~StyleSheet() = default; protected: diff --git a/Userland/Libraries/LibWeb/CSS/StyleSheet.idl b/Userland/Libraries/LibWeb/CSS/StyleSheet.idl new file mode 100644 index 0000000000..3972052b36 --- /dev/null +++ b/Userland/Libraries/LibWeb/CSS/StyleSheet.idl @@ -0,0 +1,9 @@ +interface StyleSheet { + // readonly attribute CSSOMString type; + // readonly attribute USVString? href; + // readonly attribute (Element or ProcessingInstruction)? ownerNode; + // readonly attribute CSSStyleSheet? parentStyleSheet; + // readonly attribute DOMString? title; + // [SameObject, PutForwards=mediaText] readonly attribute MediaList media; + // attribute boolean disabled; +}; diff --git a/Userland/Libraries/LibWeb/CSS/StyleSheetList.cpp b/Userland/Libraries/LibWeb/CSS/StyleSheetList.cpp index addca5598d..ef1b9b005e 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleSheetList.cpp +++ b/Userland/Libraries/LibWeb/CSS/StyleSheetList.cpp @@ -28,7 +28,7 @@ namespace Web::CSS { -void StyleSheetList::add_sheet(NonnullRefPtr sheet) +void StyleSheetList::add_sheet(NonnullRefPtr sheet) { m_sheets.append(move(sheet)); } diff --git a/Userland/Libraries/LibWeb/CSS/StyleSheetList.h b/Userland/Libraries/LibWeb/CSS/StyleSheetList.h index df2b993812..51b8a7e2e6 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleSheetList.h +++ b/Userland/Libraries/LibWeb/CSS/StyleSheetList.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020, Andreas Kling + * Copyright (c) 2020-2021, Andreas Kling * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -28,27 +28,46 @@ #include #include -#include +#include +#include #include namespace Web::CSS { -class StyleSheetList : public RefCounted { +class StyleSheetList + : public RefCounted + , public Bindings::Wrappable { public: + using WrapperType = Bindings::StyleSheetListWrapper; + static NonnullRefPtr create(DOM::Document& document) { return adopt(*new StyleSheetList(document)); } - void add_sheet(NonnullRefPtr); + void add_sheet(NonnullRefPtr); + const NonnullRefPtrVector& sheets() const { return m_sheets; } - const NonnullRefPtrVector& sheets() const { return m_sheets; } + RefPtr item(size_t index) const + { + if (index >= m_sheets.size()) + return {}; + return m_sheets[index]; + } + + size_t length() const { return m_sheets.size(); } private: explicit StyleSheetList(DOM::Document&); DOM::Document& m_document; - NonnullRefPtrVector m_sheets; + NonnullRefPtrVector m_sheets; }; } + +namespace Web::Bindings { + +StyleSheetListWrapper* wrap(JS::GlobalObject&, CSS::StyleSheetList&); + +} diff --git a/Userland/Libraries/LibWeb/CSS/StyleSheetList.idl b/Userland/Libraries/LibWeb/CSS/StyleSheetList.idl new file mode 100644 index 0000000000..27460affa4 --- /dev/null +++ b/Userland/Libraries/LibWeb/CSS/StyleSheetList.idl @@ -0,0 +1,5 @@ +interface StyleSheetList { + // FIXME: item() should be a WebIDL "getter" + CSSStyleSheet? item(unsigned long index); + readonly attribute unsigned long length; +}; diff --git a/Userland/Libraries/LibWeb/CodeGenerators/WrapperGenerator.cpp b/Userland/Libraries/LibWeb/CodeGenerators/WrapperGenerator.cpp index 0b7b167d9f..b9a6da33f6 100644 --- a/Userland/Libraries/LibWeb/CodeGenerators/WrapperGenerator.cpp +++ b/Userland/Libraries/LibWeb/CodeGenerators/WrapperGenerator.cpp @@ -408,7 +408,7 @@ int main(int argc, char** argv) return 1; } - if (namespace_.is_one_of("DOM", "HTML", "UIEvents", "HighResolutionTime", "NavigationTiming", "SVG", "XHR")) { + if (namespace_.is_one_of("CSS", "DOM", "HTML", "UIEvents", "HighResolutionTime", "NavigationTiming", "SVG", "XHR")) { StringBuilder builder; builder.append(namespace_); builder.append("::"); @@ -652,7 +652,9 @@ static void generate_header(const IDL::Interface& interface) #include // FIXME: This is very strange. -#if __has_include() +#if __has_include() +# include +#elif __has_include() # include #elif __has_include() # include @@ -768,6 +770,7 @@ void generate_implementation(const IDL::Interface& interface) #include // FIXME: This is a total hack until we can figure out the namespace for a given type somehow. +using namespace Web::CSS; using namespace Web::DOM; using namespace Web::HTML; @@ -881,7 +884,9 @@ void generate_constructor_implementation(const IDL::Interface& interface) #include #include #include -#if __has_include() +#if __has_include() +# include +#elif __has_include() # include #elif __has_include() # include @@ -898,6 +903,7 @@ void generate_constructor_implementation(const IDL::Interface& interface) #endif // FIXME: This is a total hack until we can figure out the namespace for a given type somehow. +using namespace Web::CSS; using namespace Web::DOM; using namespace Web::HTML; @@ -1079,6 +1085,7 @@ void generate_prototype_implementation(const IDL::Interface& interface) #include #include #include +#include #include #include #include @@ -1094,6 +1101,7 @@ void generate_prototype_implementation(const IDL::Interface& interface) #include #include #include +#include #include #include #include @@ -1108,7 +1116,9 @@ void generate_prototype_implementation(const IDL::Interface& interface) #if __has_include() # include #endif -#if __has_include() +#if __has_include() +# include +#elif __has_include() # include #elif __has_include() # include @@ -1125,6 +1135,7 @@ void generate_prototype_implementation(const IDL::Interface& interface) #endif // FIXME: This is a total hack until we can figure out the namespace for a given type somehow. +using namespace Web::CSS; using namespace Web::DOM; using namespace Web::HTML; using namespace Web::NavigationTiming; diff --git a/Userland/Libraries/LibWeb/DOM/Document.h b/Userland/Libraries/LibWeb/DOM/Document.h index 47f097075b..d056f14135 100644 --- a/Userland/Libraries/LibWeb/DOM/Document.h +++ b/Userland/Libraries/LibWeb/DOM/Document.h @@ -92,6 +92,8 @@ public: CSS::StyleSheetList& style_sheets() { return *m_style_sheets; } const CSS::StyleSheetList& style_sheets() const { return *m_style_sheets; } + NonnullRefPtr style_sheets_for_bindings() { return *m_style_sheets; } + virtual FlyString node_name() const override { return "#document"; } void set_hovered_node(Node*); diff --git a/Userland/Libraries/LibWeb/DOM/Document.idl b/Userland/Libraries/LibWeb/DOM/Document.idl index 824949e323..a0c37c8a15 100644 --- a/Userland/Libraries/LibWeb/DOM/Document.idl +++ b/Userland/Libraries/LibWeb/DOM/Document.idl @@ -29,6 +29,8 @@ interface Document : Node { Comment createComment(DOMString data); Range createRange(); + [ImplementedAs=style_sheets_for_bindings] readonly attribute StyleSheetList styleSheets; + readonly attribute DOMString compatMode; readonly attribute DocumentType? doctype; diff --git a/Userland/Libraries/LibWeb/Forward.h b/Userland/Libraries/LibWeb/Forward.h index 20585682e8..5a90a41bb0 100644 --- a/Userland/Libraries/LibWeb/Forward.h +++ b/Userland/Libraries/LibWeb/Forward.h @@ -203,7 +203,7 @@ class XMLHttpRequestEventTarget; } namespace Web::Bindings { - +class CSSStyleSheetWrapper; class CanvasRenderingContext2DWrapper; class CharacterDataWrapper; class CommentWrapper; @@ -301,6 +301,8 @@ class SVGGeometryElementWrapper; class SVGGraphicsElementWrapper; class SVGPathElementWrapper; class SVGSVGElementWrapper; +class StyleSheetWrapper; +class StyleSheetListWrapper; class TextWrapper; class UIEventWrapper; class WindowObject;