From 35d3a1e77b3f628da225ca4ec5d0ba050082624a Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Sun, 12 Dec 2021 18:03:22 +0000 Subject: [PATCH] LibWeb: Add the TextEncoder interface This is from the Encoding Standard (https://encoding.spec.whatwg.org), and therefore gets its own namespace and subdirectory within LibWeb :^) --- .../LibWeb/WrapperGenerator.cpp | 12 +++++- .../LibWeb/Bindings/WindowObjectHelper.h | 3 ++ Userland/Libraries/LibWeb/CMakeLists.txt | 1 + .../Libraries/LibWeb/Encoding/TextEncoder.h | 40 +++++++++++++++++++ .../Libraries/LibWeb/Encoding/TextEncoder.idl | 9 +++++ Userland/Libraries/LibWeb/Forward.h | 5 +++ 6 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 Userland/Libraries/LibWeb/Encoding/TextEncoder.h create mode 100644 Userland/Libraries/LibWeb/Encoding/TextEncoder.idl diff --git a/Meta/Lagom/Tools/CodeGenerators/LibWeb/WrapperGenerator.cpp b/Meta/Lagom/Tools/CodeGenerators/LibWeb/WrapperGenerator.cpp index 1af9659b30..916ce15a34 100644 --- a/Meta/Lagom/Tools/CodeGenerators/LibWeb/WrapperGenerator.cpp +++ b/Meta/Lagom/Tools/CodeGenerators/LibWeb/WrapperGenerator.cpp @@ -807,7 +807,7 @@ int main(int argc, char** argv) auto interface = IDL::parse_interface(path, data, import_base_path); - if (namespace_.is_one_of("Crypto", "CSS", "DOM", "HTML", "UIEvents", "Geometry", "HighResolutionTime", "IntersectionObserver", "NavigationTiming", "RequestIdleCallback", "ResizeObserver", "SVG", "Selection", "XHR", "URL")) { + if (namespace_.is_one_of("Crypto", "CSS", "DOM", "Encoding", "HTML", "UIEvents", "Geometry", "HighResolutionTime", "IntersectionObserver", "NavigationTiming", "RequestIdleCallback", "ResizeObserver", "SVG", "Selection", "XHR", "URL")) { StringBuilder builder; builder.append(namespace_); builder.append("::"); @@ -1452,6 +1452,8 @@ static void generate_header(IDL::Interface const& interface) # include #elif __has_include() # include +#elif __has_include() +# include #elif __has_include() # include #elif __has_include() @@ -2500,6 +2502,8 @@ void generate_constructor_implementation(IDL::Interface const& interface) # include #elif __has_include() # include +#elif __has_include() +# include #elif __has_include() # include #elif __has_include() @@ -2815,6 +2819,8 @@ void generate_prototype_implementation(IDL::Interface const& interface) # include #elif __has_include() # include +#elif __has_include() +# include #elif __has_include() # include #elif __has_include() @@ -3268,6 +3274,8 @@ static void generate_iterator_header(IDL::Interface const& interface) # include #elif __has_include() # include +#elif __has_include() +# include #elif __has_include() # include #elif __has_include() @@ -3456,6 +3464,8 @@ void generate_iterator_prototype_implementation(IDL::Interface const& interface) # include #elif __has_include() # include +#elif __has_include() +# include #elif __has_include() # include #elif __has_include() diff --git a/Userland/Libraries/LibWeb/Bindings/WindowObjectHelper.h b/Userland/Libraries/LibWeb/Bindings/WindowObjectHelper.h index 7988bc1966..3faa49a98e 100644 --- a/Userland/Libraries/LibWeb/Bindings/WindowObjectHelper.h +++ b/Userland/Libraries/LibWeb/Bindings/WindowObjectHelper.h @@ -262,6 +262,8 @@ #include #include #include +#include +#include #include #include #include @@ -416,6 +418,7 @@ ADD_WINDOW_OBJECT_INTERFACE(SVGPathElement) \ ADD_WINDOW_OBJECT_INTERFACE(SVGSVGElement) \ ADD_WINDOW_OBJECT_INTERFACE(Text) \ + ADD_WINDOW_OBJECT_INTERFACE(TextEncoder) \ ADD_WINDOW_OBJECT_INTERFACE(UIEvent) \ ADD_WINDOW_OBJECT_INTERFACE(URLSearchParams) \ ADD_WINDOW_OBJECT_INTERFACE(URL) \ diff --git a/Userland/Libraries/LibWeb/CMakeLists.txt b/Userland/Libraries/LibWeb/CMakeLists.txt index 81dbfa84aa..cd37ce8d66 100644 --- a/Userland/Libraries/LibWeb/CMakeLists.txt +++ b/Userland/Libraries/LibWeb/CMakeLists.txt @@ -396,6 +396,7 @@ libweb_js_wrapper(DOM/ProcessingInstruction) libweb_js_wrapper(DOM/Range) libweb_js_wrapper(DOM/ShadowRoot) libweb_js_wrapper(DOM/Text) +libweb_js_wrapper(Encoding/TextEncoder) libweb_js_wrapper(Geometry/DOMRect) libweb_js_wrapper(Geometry/DOMRectReadOnly) libweb_js_wrapper(HTML/CanvasRenderingContext2D) diff --git a/Userland/Libraries/LibWeb/Encoding/TextEncoder.h b/Userland/Libraries/LibWeb/Encoding/TextEncoder.h new file mode 100644 index 0000000000..b30d9264ce --- /dev/null +++ b/Userland/Libraries/LibWeb/Encoding/TextEncoder.h @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2021, Linus Groh + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include +#include +#include +#include +#include +#include + +namespace Web::Encoding { + +// https://encoding.spec.whatwg.org/#textencoder +class TextEncoder + : public RefCounted + , public Bindings::Wrappable { +public: + using WrapperType = Bindings::TextEncoderWrapper; + + static NonnullRefPtr create() + { + return adopt_ref(*new TextEncoder()); + } + + static NonnullRefPtr create_with_global_object(Bindings::WindowObject&) + { + return TextEncoder::create(); + } + +protected: + // https://encoding.spec.whatwg.org/#dom-textencoder + TextEncoder() = default; +}; + +} diff --git a/Userland/Libraries/LibWeb/Encoding/TextEncoder.idl b/Userland/Libraries/LibWeb/Encoding/TextEncoder.idl new file mode 100644 index 0000000000..09713478b7 --- /dev/null +++ b/Userland/Libraries/LibWeb/Encoding/TextEncoder.idl @@ -0,0 +1,9 @@ +[Exposed=(Window,Worker)] +interface TextEncoder { + constructor(); + + // [NewObject] Uint8Array encode(optional USVString input = ""); + // TextEncoderEncodeIntoResult encodeInto(USVString source, [AllowShared] Uint8Array destination); + + // readonly attribute DOMString encoding; +}; diff --git a/Userland/Libraries/LibWeb/Forward.h b/Userland/Libraries/LibWeb/Forward.h index 7205762ba7..618a7cf1f8 100644 --- a/Userland/Libraries/LibWeb/Forward.h +++ b/Userland/Libraries/LibWeb/Forward.h @@ -109,6 +109,10 @@ template class ExceptionOr; } +namespace Web::Encoding { +class TextEncoder; +} + namespace Web::Geometry { class DOMRect; class DOMRectReadOnly; @@ -432,6 +436,7 @@ class SVGGeometryElementWrapper; class SVGGraphicsElementWrapper; class SVGPathElementWrapper; class SVGSVGElementWrapper; +class TextEncoderWrapper; class TextWrapper; class UIEventWrapper; class URLConstructor;