mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-22 07:30:19 +00:00
LibWeb: Add TextEncoder encodeInto
This commit is contained in:
parent
f1ead552ce
commit
0104225d9b
Notes:
sideshowbarker
2024-07-17 11:34:34 +09:00
Author: https://github.com/bplaat Commit: https://github.com/SerenityOS/serenity/commit/0104225d9b Pull-request: https://github.com/SerenityOS/serenity/pull/21351
9 changed files with 123 additions and 10 deletions
|
@ -87,6 +87,7 @@ static bool is_javascript_builtin(Type const& type)
|
|||
"ArrayBuffer"sv,
|
||||
"Float32Array"sv,
|
||||
"Float64Array"sv,
|
||||
"Uint8Array"sv
|
||||
};
|
||||
|
||||
return types.span().contains_slow(type.name());
|
||||
|
@ -602,7 +603,7 @@ static void generate_to_cpp(SourceGenerator& generator, ParameterType& parameter
|
|||
auto @cpp_name@ = JS::make_handle(TRY(@js_name@@js_suffix@.to_object(vm)));
|
||||
)~~~");
|
||||
}
|
||||
} else if (parameter.type->name() == "BufferSource" || parameter.type->name() == "Float32Array" || parameter.type->name() == "Float64Array") {
|
||||
} else if (parameter.type->name() == "BufferSource" || parameter.type->name() == "Float32Array" || parameter.type->name() == "Float64Array" || parameter.type->name() == "Uint8Array") {
|
||||
if (optional) {
|
||||
scoped_generator.append(R"~~~(
|
||||
Optional<JS::Handle<JS::Object>> @cpp_name@;
|
||||
|
|
|
@ -0,0 +1,4 @@
|
|||
1. {"0":87,"1":101,"2":108,"3":108,"4":32,"5":72,"6":101,"7":108,"8":108,"9":111,"10":32,"11":70,"12":114,"13":105,"14":101,"15":110,"16":100,"17":115,"18":32,"19":240,"20":159,"21":152,"22":128,"23":33}
|
||||
2. {"0":87,"1":101,"2":108,"3":108,"4":32,"5":72,"6":101,"7":108,"8":108,"9":111,"10":32,"11":70,"12":114,"13":105,"14":101,"15":110,"16":100,"17":115,"18":32,"19":240,"20":159,"21":152,"22":128,"23":33,"24":0,"25":0,"26":0,"27":0,"28":0,"29":0,"30":0,"31":0}
|
||||
3. 22
|
||||
4. 24
|
33
Tests/LibWeb/Text/input/Encoding/TextEncoder-encode.html
Normal file
33
Tests/LibWeb/Text/input/Encoding/TextEncoder-encode.html
Normal file
|
@ -0,0 +1,33 @@
|
|||
<script src="../include.js"></script>
|
||||
<script>
|
||||
test(() => {
|
||||
let testCounter = 1;
|
||||
function testPart(part) {
|
||||
println(`${testCounter++}. ${JSON.stringify(part())}`);
|
||||
}
|
||||
|
||||
// 1. TextEncoder encode
|
||||
testPart(() => new TextEncoder().encode('Well Hello Friends 😀!'));
|
||||
|
||||
// 2. TextEncoder encodeInto
|
||||
testPart(() => {
|
||||
const buffer = new Uint8Array(32);
|
||||
new TextEncoder().encodeInto('Well Hello Friends 😀!', buffer);
|
||||
return buffer
|
||||
});
|
||||
|
||||
// 3. TextEncoder encodeInto result read
|
||||
testPart(() => {
|
||||
const buffer = new Uint8Array(32);
|
||||
const result = new TextEncoder().encodeInto('Well Hello Friends 😀!', buffer);
|
||||
return result.read;
|
||||
});
|
||||
|
||||
// 3. TextEncoder encodeInto result written
|
||||
testPart(() => {
|
||||
const buffer = new Uint8Array(32);
|
||||
const result = new TextEncoder().encodeInto('Well Hello Friends 😀!', buffer);
|
||||
return result.written;
|
||||
});
|
||||
});
|
||||
</script>
|
|
@ -30,10 +30,9 @@ void TextEncoder::initialize(JS::Realm& realm)
|
|||
}
|
||||
|
||||
// https://encoding.spec.whatwg.org/#dom-textencoder-encode
|
||||
JS::Uint8Array* TextEncoder::encode(String const& input) const
|
||||
JS::NonnullGCPtr<JS::Uint8Array> TextEncoder::encode(String const& input) const
|
||||
{
|
||||
// NOTE: The AK::DeprecatedString returned from PrimitiveString::string() is always UTF-8, regardless of the internal string type, so most of these steps are no-ops.
|
||||
|
||||
// NOTE: The AK::String is always UTF-8, 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:
|
||||
|
@ -48,10 +47,67 @@ JS::Uint8Array* TextEncoder::encode(String const& input) const
|
|||
return JS::Uint8Array::create(realm(), array_length, *array_buffer);
|
||||
}
|
||||
|
||||
// https://encoding.spec.whatwg.org/#dom-textencoder-encodeinto
|
||||
TextEncoderEncodeIntoResult TextEncoder::encode_into(String const& source, JS::Handle<JS::Object> const& destination) const
|
||||
{
|
||||
auto& destination_array = static_cast<JS::Uint8Array&>(*destination);
|
||||
auto data = destination_array.data();
|
||||
|
||||
// 1. Let read be 0.
|
||||
unsigned long long read = 0;
|
||||
// 2. Let written be 0.
|
||||
unsigned long long written = 0;
|
||||
|
||||
// NOTE: The AK::String is always UTF-8, so most of these steps are no-ops.
|
||||
// 3. Let encoder be an instance of the UTF-8 encoder.
|
||||
// 4. Let unused be the I/O queue of scalar values « end-of-queue ».
|
||||
// 5. Convert source to an I/O queue of scalar values.
|
||||
auto code_points = source.code_points();
|
||||
auto it = code_points.begin();
|
||||
|
||||
// 6. While true:
|
||||
while (true) {
|
||||
// 6.1. Let item be the result of reading from source.
|
||||
// 6.2. Let result be the result of running encoder’s handler on unused and item.
|
||||
// 6.3. If result is finished, then break.
|
||||
if (it.done())
|
||||
break;
|
||||
auto item = *it;
|
||||
auto result = it.underlying_code_point_bytes();
|
||||
|
||||
// 6.4. Otherwise:
|
||||
// 6.4.1. If destination’s byte length − written is greater than or equal to the number of bytes in result, then:
|
||||
if (data.size() - written >= result.size()) {
|
||||
// 6.4.1.1. If item is greater than U+FFFF, then increment read by 2.
|
||||
if (item > 0xffff) {
|
||||
read += 2;
|
||||
}
|
||||
// 6.4.1.2. Otherwise, increment read by 1.
|
||||
else {
|
||||
read++;
|
||||
}
|
||||
|
||||
// 6.4.1.3. Write the bytes in result into destination, with startingOffset set to written.
|
||||
// 6.4.1.4. Increment written by the number of bytes in result.
|
||||
for (auto byte : result)
|
||||
data[written++] = byte;
|
||||
}
|
||||
// 6.4.2. Otherwise, break.
|
||||
else {
|
||||
break;
|
||||
}
|
||||
|
||||
++it;
|
||||
}
|
||||
|
||||
// 7. Return «[ "read" → read, "written" → written ]».
|
||||
return { read, written };
|
||||
}
|
||||
|
||||
// https://encoding.spec.whatwg.org/#dom-textencoder-encoding
|
||||
FlyString const& TextEncoder::encoding()
|
||||
{
|
||||
static const FlyString encoding = "utf-8"_fly_string;
|
||||
static FlyString const encoding = "utf-8"_fly_string;
|
||||
return encoding;
|
||||
}
|
||||
|
||||
|
|
|
@ -15,6 +15,12 @@
|
|||
|
||||
namespace Web::Encoding {
|
||||
|
||||
// https://encoding.spec.whatwg.org/#dictdef-textencoderencodeintoresult
|
||||
struct TextEncoderEncodeIntoResult {
|
||||
unsigned long long read;
|
||||
unsigned long long written;
|
||||
};
|
||||
|
||||
// https://encoding.spec.whatwg.org/#textencoder
|
||||
class TextEncoder final : public Bindings::PlatformObject {
|
||||
WEB_PLATFORM_OBJECT(TextEncoder, Bindings::PlatformObject);
|
||||
|
@ -24,7 +30,8 @@ public:
|
|||
|
||||
virtual ~TextEncoder() override;
|
||||
|
||||
JS::Uint8Array* encode(String const& input) const;
|
||||
JS::NonnullGCPtr<JS::Uint8Array> encode(String const& input) const;
|
||||
TextEncoderEncodeIntoResult encode_into(String const& source, JS::Handle<JS::Object> const& destination) const;
|
||||
|
||||
static FlyString const& encoding();
|
||||
|
||||
|
|
|
@ -1,9 +1,20 @@
|
|||
[Exposed=(Window,Worker)]
|
||||
// https://encoding.spec.whatwg.org/#textencodercommon
|
||||
interface mixin TextEncoderCommon {
|
||||
readonly attribute DOMString encoding;
|
||||
};
|
||||
|
||||
// https://encoding.spec.whatwg.org/#dictdef-textencoderencodeintoresult
|
||||
dictionary TextEncoderEncodeIntoResult {
|
||||
unsigned long long read;
|
||||
unsigned long long written;
|
||||
};
|
||||
|
||||
// https://encoding.spec.whatwg.org/#textencoder
|
||||
[Exposed=*]
|
||||
interface TextEncoder {
|
||||
constructor();
|
||||
|
||||
[NewObject] Uint8Array encode(optional USVString input = "");
|
||||
// TextEncoderEncodeIntoResult encodeInto(USVString source, [AllowShared] Uint8Array destination);
|
||||
|
||||
readonly attribute DOMString encoding;
|
||||
TextEncoderEncodeIntoResult encodeInto(USVString source, [AllowShared] Uint8Array destination);
|
||||
};
|
||||
TextEncoder includes TextEncoderCommon;
|
||||
|
|
|
@ -258,6 +258,7 @@ struct TextDecodeOptions;
|
|||
class TextDecoder;
|
||||
struct TextDecoderOptions;
|
||||
class TextEncoder;
|
||||
struct TextEncoderEncodeIntoResult;
|
||||
}
|
||||
|
||||
namespace Web::Fetch {
|
||||
|
|
Loading…
Reference in a new issue