Ver Fonte

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 :^)
Linus Groh há 3 anos atrás
pai
commit
35d3a1e77b

+ 11 - 1
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 <LibWeb/CSS/@name@.h>
 #elif __has_include(<LibWeb/DOM/@name@.h>)
 #    include <LibWeb/DOM/@name@.h>
+#elif __has_include(<LibWeb/Encoding/@name@.h>)
+#    include <LibWeb/Encoding/@name@.h>
 #elif __has_include(<LibWeb/Geometry/@name@.h>)
 #    include <LibWeb/Geometry/@name@.h>
 #elif __has_include(<LibWeb/HTML/@name@.h>)
@@ -2500,6 +2502,8 @@ void generate_constructor_implementation(IDL::Interface const& interface)
 #    include <LibWeb/CSS/@name@.h>
 #elif __has_include(<LibWeb/DOM/@name@.h>)
 #    include <LibWeb/DOM/@name@.h>
+#elif __has_include(<LibWeb/Encoding/@name@.h>)
+#    include <LibWeb/Encoding/@name@.h>
 #elif __has_include(<LibWeb/Geometry/@name@.h>)
 #    include <LibWeb/Geometry/@name@.h>
 #elif __has_include(<LibWeb/HTML/@name@.h>)
@@ -2815,6 +2819,8 @@ void generate_prototype_implementation(IDL::Interface const& interface)
 #    include <LibWeb/CSS/@name@.h>
 #elif __has_include(<LibWeb/DOM/@name@.h>)
 #    include <LibWeb/DOM/@name@.h>
+#elif __has_include(<LibWeb/Encoding/@name@.h>)
+#    include <LibWeb/Encoding/@name@.h>
 #elif __has_include(<LibWeb/Geometry/@name@.h>)
 #    include <LibWeb/Geometry/@name@.h>
 #elif __has_include(<LibWeb/HTML/@name@.h>)
@@ -3268,6 +3274,8 @@ static void generate_iterator_header(IDL::Interface const& interface)
 #    include <LibWeb/CSS/@name@.h>
 #elif __has_include(<LibWeb/DOM/@name@.h>)
 #    include <LibWeb/DOM/@name@.h>
+#elif __has_include(<LibWeb/Encoding/@name@.h>)
+#    include <LibWeb/Encoding/@name@.h>
 #elif __has_include(<LibWeb/Geometry/@name@.h>)
 #    include <LibWeb/Geometry/@name@.h>
 #elif __has_include(<LibWeb/HTML/@name@.h>)
@@ -3456,6 +3464,8 @@ void generate_iterator_prototype_implementation(IDL::Interface const& interface)
 #    include <LibWeb/CSS/@name@.h>
 #elif __has_include(<LibWeb/DOM/@name@.h>)
 #    include <LibWeb/DOM/@name@.h>
+#elif __has_include(<LibWeb/Encoding/@name@.h>)
+#    include <LibWeb/Encoding/@name@.h>
 #elif __has_include(<LibWeb/Geometry/@name@.h>)
 #    include <LibWeb/Geometry/@name@.h>
 #elif __has_include(<LibWeb/HTML/@name@.h>)

+ 3 - 0
Userland/Libraries/LibWeb/Bindings/WindowObjectHelper.h

@@ -262,6 +262,8 @@
 #include <LibWeb/Bindings/SubmitEventConstructor.h>
 #include <LibWeb/Bindings/SubmitEventPrototype.h>
 #include <LibWeb/Bindings/TextConstructor.h>
+#include <LibWeb/Bindings/TextEncoderConstructor.h>
+#include <LibWeb/Bindings/TextEncoderPrototype.h>
 #include <LibWeb/Bindings/TextPrototype.h>
 #include <LibWeb/Bindings/UIEventConstructor.h>
 #include <LibWeb/Bindings/UIEventPrototype.h>
@@ -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)                       \

+ 1 - 0
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)

+ 40 - 0
Userland/Libraries/LibWeb/Encoding/TextEncoder.h

@@ -0,0 +1,40 @@
+/*
+ * Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <AK/Forward.h>
+#include <AK/NonnullRefPtr.h>
+#include <AK/RefCounted.h>
+#include <LibJS/Forward.h>
+#include <LibWeb/Bindings/Wrappable.h>
+#include <LibWeb/Forward.h>
+
+namespace Web::Encoding {
+
+// https://encoding.spec.whatwg.org/#textencoder
+class TextEncoder
+    : public RefCounted<TextEncoder>
+    , public Bindings::Wrappable {
+public:
+    using WrapperType = Bindings::TextEncoderWrapper;
+
+    static NonnullRefPtr<TextEncoder> create()
+    {
+        return adopt_ref(*new TextEncoder());
+    }
+
+    static NonnullRefPtr<TextEncoder> create_with_global_object(Bindings::WindowObject&)
+    {
+        return TextEncoder::create();
+    }
+
+protected:
+    // https://encoding.spec.whatwg.org/#dom-textencoder
+    TextEncoder() = default;
+};
+
+}

+ 9 - 0
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;
+};

+ 5 - 0
Userland/Libraries/LibWeb/Forward.h

@@ -109,6 +109,10 @@ template<typename ValueType>
 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;