mirror of
https://github.com/RGBCube/serenity
synced 2025-07-23 10:47:36 +00:00
LibWeb: Implement TextEncoder.prototype.encode()
This commit is contained in:
parent
35d3a1e77b
commit
f37d00c07b
6 changed files with 69 additions and 2 deletions
|
@ -1330,7 +1330,7 @@ static void generate_wrap_statement(SourceGenerator& generator, String const& va
|
||||||
scoped_generator.append(R"~~~(
|
scoped_generator.append(R"~~~(
|
||||||
@result_expression@ JS::Value((i32)@value@);
|
@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"~~~(
|
scoped_generator.append(R"~~~(
|
||||||
@result_expression@ @value@;
|
@result_expression@ @value@;
|
||||||
)~~~");
|
)~~~");
|
||||||
|
|
|
@ -92,6 +92,7 @@ set(SOURCES
|
||||||
DOMParsing/InnerHTML.cpp
|
DOMParsing/InnerHTML.cpp
|
||||||
DOMTreeModel.cpp
|
DOMTreeModel.cpp
|
||||||
Dump.cpp
|
Dump.cpp
|
||||||
|
Encoding/TextEncoder.cpp
|
||||||
FontCache.cpp
|
FontCache.cpp
|
||||||
HTML/AttributeNames.cpp
|
HTML/AttributeNames.cpp
|
||||||
HTML/BrowsingContext.cpp
|
HTML/BrowsingContext.cpp
|
||||||
|
|
36
Userland/Libraries/LibWeb/Encoding/TextEncoder.cpp
Normal file
36
Userland/Libraries/LibWeb/Encoding/TextEncoder.cpp
Normal file
|
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -32,6 +32,8 @@ public:
|
||||||
return TextEncoder::create();
|
return TextEncoder::create();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
JS::Uint8Array* encode(String const& input) const;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
// https://encoding.spec.whatwg.org/#dom-textencoder
|
// https://encoding.spec.whatwg.org/#dom-textencoder
|
||||||
TextEncoder() = default;
|
TextEncoder() = default;
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
interface TextEncoder {
|
interface TextEncoder {
|
||||||
constructor();
|
constructor();
|
||||||
|
|
||||||
// [NewObject] Uint8Array encode(optional USVString input = "");
|
[NewObject] Uint8Array encode(optional USVString input = "");
|
||||||
// TextEncoderEncodeIntoResult encodeInto(USVString source, [AllowShared] Uint8Array destination);
|
// TextEncoderEncodeIntoResult encodeInto(USVString source, [AllowShared] Uint8Array destination);
|
||||||
|
|
||||||
// readonly attribute DOMString encoding;
|
// readonly attribute DOMString encoding;
|
||||||
|
|
|
@ -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();
|
||||||
|
});
|
Loading…
Add table
Add a link
Reference in a new issue