LibWeb: Start exposing CSS style sheets to JavaScript :^)

This patch adds bindings for the following objects:

- StyleSheet
- StyleSheetList
- CSSStyleSheet

You can get to a document's style sheets via Document.styleSheets
and iterate through them using StyleSheetList's item() and length().

That's it in terms of functionality at this point, but still neat. :^)
This commit is contained in:
Andreas Kling 2021-03-08 11:22:18 +01:00
parent 0d515dea5d
commit a9830d9a55
12 changed files with 86 additions and 13 deletions

View file

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

View file

@ -37,6 +37,8 @@ namespace Web::CSS {
class CSSStyleSheet final : public StyleSheet {
public:
using WrapperType = Bindings::CSSStyleSheetWrapper;
static NonnullRefPtr<CSSStyleSheet> create(NonnullRefPtrVector<CSSRule> rules)
{
return adopt(*new CSSStyleSheet(move(rules)));
@ -86,3 +88,9 @@ private:
};
}
namespace Web::Bindings {
CSSStyleSheetWrapper* wrap(JS::GlobalObject&, CSS::CSSStyleSheet&);
}

View file

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

View file

@ -28,11 +28,17 @@
#pragma once
#include <AK/RefCounted.h>
#include <LibWeb/Bindings/Wrappable.h>
#include <LibWeb/Forward.h>
namespace Web::CSS {
class StyleSheet : public RefCounted<StyleSheet> {
class StyleSheet
: public RefCounted<StyleSheet>
, public Bindings::Wrappable {
public:
using WrapperType = Bindings::StyleSheetWrapper;
virtual ~StyleSheet() = default;
protected:

View file

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

View file

@ -28,7 +28,7 @@
namespace Web::CSS {
void StyleSheetList::add_sheet(NonnullRefPtr<StyleSheet> sheet)
void StyleSheetList::add_sheet(NonnullRefPtr<CSSStyleSheet> sheet)
{
m_sheets.append(move(sheet));
}

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -28,27 +28,46 @@
#include <AK/NonnullRefPtrVector.h>
#include <AK/RefCounted.h>
#include <LibWeb/CSS/StyleSheet.h>
#include <LibWeb/Bindings/Wrappable.h>
#include <LibWeb/CSS/CSSStyleSheet.h>
#include <LibWeb/Forward.h>
namespace Web::CSS {
class StyleSheetList : public RefCounted<StyleSheetList> {
class StyleSheetList
: public RefCounted<StyleSheetList>
, public Bindings::Wrappable {
public:
using WrapperType = Bindings::StyleSheetListWrapper;
static NonnullRefPtr<StyleSheetList> create(DOM::Document& document)
{
return adopt(*new StyleSheetList(document));
}
void add_sheet(NonnullRefPtr<StyleSheet>);
void add_sheet(NonnullRefPtr<CSSStyleSheet>);
const NonnullRefPtrVector<CSSStyleSheet>& sheets() const { return m_sheets; }
const NonnullRefPtrVector<StyleSheet>& sheets() const { return m_sheets; }
RefPtr<CSSStyleSheet> 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<StyleSheet> m_sheets;
NonnullRefPtrVector<CSSStyleSheet> m_sheets;
};
}
namespace Web::Bindings {
StyleSheetListWrapper* wrap(JS::GlobalObject&, CSS::StyleSheetList&);
}

View file

@ -0,0 +1,5 @@
interface StyleSheetList {
// FIXME: item() should be a WebIDL "getter"
CSSStyleSheet? item(unsigned long index);
readonly attribute unsigned long length;
};

View file

@ -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 <LibWeb/Bindings/Wrapper.h>
// FIXME: This is very strange.
#if __has_include(<LibWeb/DOM/@name@.h>)
#if __has_include(<LibWeb/CSS/@name@.h>)
# include <LibWeb/CSS/@name@.h>
#elif __has_include(<LibWeb/DOM/@name@.h>)
# include <LibWeb/DOM/@name@.h>
#elif __has_include(<LibWeb/HTML/@name@.h>)
# include <LibWeb/HTML/@name@.h>
@ -768,6 +770,7 @@ void generate_implementation(const IDL::Interface& interface)
#include <LibWeb/Origin.h>
// 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 <LibWeb/Bindings/@prototype_class@.h>
#include <LibWeb/Bindings/@wrapper_class@.h>
#include <LibWeb/Bindings/WindowObject.h>
#if __has_include(<LibWeb/DOM/@name@.h>)
#if __has_include(<LibWeb/CSS/@name@.h>)
# include <LibWeb/CSS/@name@.h>
#elif __has_include(<LibWeb/DOM/@name@.h>)
# include <LibWeb/DOM/@name@.h>
#elif __has_include(<LibWeb/HTML/@name@.h>)
# include <LibWeb/HTML/@name@.h>
@ -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 <LibJS/Runtime/Uint8ClampedArray.h>
#include <LibWeb/Bindings/@prototype_class@.h>
#include <LibWeb/Bindings/@wrapper_class@.h>
#include <LibWeb/Bindings/CSSStyleSheetWrapper.h>
#include <LibWeb/Bindings/CanvasRenderingContext2DWrapper.h>
#include <LibWeb/Bindings/CommentWrapper.h>
#include <LibWeb/Bindings/DOMImplementationWrapper.h>
@ -1094,6 +1101,7 @@ void generate_prototype_implementation(const IDL::Interface& interface)
#include <LibWeb/Bindings/NodeWrapperFactory.h>
#include <LibWeb/Bindings/PerformanceTimingWrapper.h>
#include <LibWeb/Bindings/RangeWrapper.h>
#include <LibWeb/Bindings/StyleSheetListWrapper.h>
#include <LibWeb/Bindings/TextWrapper.h>
#include <LibWeb/Bindings/WindowObject.h>
#include <LibWeb/DOM/Element.h>
@ -1108,7 +1116,9 @@ void generate_prototype_implementation(const IDL::Interface& interface)
#if __has_include(<LibWeb/Bindings/@prototype_base_class@.h>)
# include <LibWeb/Bindings/@prototype_base_class@.h>
#endif
#if __has_include(<LibWeb/DOM/@name@.h>)
#if __has_include(<LibWeb/CSS/@name@.h>)
# include <LibWeb/CSS/@name@.h>
#elif __has_include(<LibWeb/DOM/@name@.h>)
# include <LibWeb/DOM/@name@.h>
#elif __has_include(<LibWeb/HTML/@name@.h>)
# include <LibWeb/HTML/@name@.h>
@ -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;

View file

@ -92,6 +92,8 @@ public:
CSS::StyleSheetList& style_sheets() { return *m_style_sheets; }
const CSS::StyleSheetList& style_sheets() const { return *m_style_sheets; }
NonnullRefPtr<CSS::StyleSheetList> style_sheets_for_bindings() { return *m_style_sheets; }
virtual FlyString node_name() const override { return "#document"; }
void set_hovered_node(Node*);

View file

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

View file

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