Procházet zdrojové kódy

LibWeb: Implement TextEncoder.prototype.encode()

Linus Groh před 3 roky
rodič
revize
f37d00c07b

+ 1 - 1
Meta/Lagom/Tools/CodeGenerators/LibWeb/WrapperGenerator.cpp

@@ -1330,7 +1330,7 @@ static void generate_wrap_statement(SourceGenerator& generator, String const& va
         scoped_generator.append(R"~~~(
     @result_expression@ JS::Value((i32)@value@);
 )~~~");
-    } else if (type.name == "Location" || type.name == "Promise" || type.name == "Uint8ClampedArray" || type.name == "any") {
+    } else if (type.name == "Location" || type.name == "Promise" || type.name == "Uint8Array" || type.name == "Uint8ClampedArray" || type.name == "any") {
         scoped_generator.append(R"~~~(
     @result_expression@ @value@;
 )~~~");

+ 1 - 0
Userland/Libraries/LibWeb/CMakeLists.txt

@@ -92,6 +92,7 @@ set(SOURCES
     DOMParsing/InnerHTML.cpp
     DOMTreeModel.cpp
     Dump.cpp
+    Encoding/TextEncoder.cpp
     FontCache.cpp
     HTML/AttributeNames.cpp
     HTML/BrowsingContext.cpp

+ 36 - 0
Userland/Libraries/LibWeb/Encoding/TextEncoder.cpp

@@ -0,0 +1,36 @@
+/*
+ * Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <LibJS/Runtime/TypedArray.h>
+#include <LibWeb/Bindings/Wrapper.h>
+#include <LibWeb/Encoding/TextEncoder.h>
+
+namespace Web::Encoding {
+
+// https://encoding.spec.whatwg.org/#dom-textencoder-encode
+JS::Uint8Array* TextEncoder::encode(String const& input) const
+{
+    auto& global_object = wrapper()->global_object();
+
+    // NOTE: The AK::String returned from PrimitiveString::string() is always UTF-8, regardless of the internal string type, so most of these steps are no-ops.
+
+    // 1. Convert input to an I/O queue of scalar values.
+    // 2. Let output be the I/O queue of bytes « end-of-queue ».
+    // 3. While true:
+    //     1. Let item be the result of reading from input.
+    //     2. Let result be the result of processing an item with item, an instance of the UTF-8 encoder, input, output, and "fatal".
+    //     3. Assert: result is not an error.
+    //     4. If result is finished, then convert output into a byte sequence and return a Uint8Array object wrapping an ArrayBuffer containing output.
+
+    auto byte_buffer = input.to_byte_buffer();
+
+    // FIXME: Support `TypedArray::create()` with existing `ArrayBuffer`, so that we don't have to allocate two `ByteBuffer`s.
+    auto* typed_array = JS::Uint8Array::create(global_object, byte_buffer.size());
+    typed_array->viewed_array_buffer()->buffer() = move(byte_buffer);
+    return typed_array;
+}
+
+}

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

@@ -32,6 +32,8 @@ public:
         return TextEncoder::create();
     }
 
+    JS::Uint8Array* encode(String const& input) const;
+
 protected:
     // https://encoding.spec.whatwg.org/#dom-textencoder
     TextEncoder() = default;

+ 1 - 1
Userland/Libraries/LibWeb/Encoding/TextEncoder.idl

@@ -2,7 +2,7 @@
 interface TextEncoder {
     constructor();
 
-    // [NewObject] Uint8Array encode(optional USVString input = "");
+    [NewObject] Uint8Array encode(optional USVString input = "");
     // TextEncoderEncodeIntoResult encodeInto(USVString source, [AllowShared] Uint8Array destination);
 
     // readonly attribute DOMString encoding;

+ 28 - 0
Userland/Libraries/LibWeb/Tests/Encoding/TextEncoder.prototype.encode.js

@@ -0,0 +1,28 @@
+describe("normal behavior", () => {
+    loadLocalPage("/res/html/misc/blank.html");
+
+    afterInitialPageLoad(page => {
+        test("Basic functionality", () => {
+            const textEncoder = new page.TextEncoder();
+
+            {
+                const typedArray = textEncoder.encode("");
+                expect(typedArray).toHaveLength(0);
+            }
+
+            {
+                const typedArray = textEncoder.encode("abc");
+                expect(typedArray).toHaveLength(3);
+                expect(Array.from(typedArray)).toEqual([97, 98, 99]);
+            }
+
+            {
+                const typedArray = textEncoder.encode("€");
+                expect(typedArray).toHaveLength(3);
+                expect(Array.from(typedArray)).toEqual([226, 130, 172]);
+                // [255, 254, 172, 32] in UTF-16, but TextEncoder always converts JS UTF-16 strings to UTF-8
+            }
+        });
+    });
+    waitForPageToLoad();
+});