mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-09-07 10:06:03 +00:00
Everywhere: Hoist the Libraries folder to the top-level
This commit is contained in:
parent
950e819ee7
commit
93712b24bf
Notes:
github-actions[bot]
2024-11-10 11:51:52 +00:00
Author: https://github.com/trflynn89
Commit: 93712b24bf
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/2256
Reviewed-by: https://github.com/sideshowbarker
4547 changed files with 104 additions and 113 deletions
85
Libraries/LibWeb/Encoding/TextDecoder.cpp
Normal file
85
Libraries/LibWeb/Encoding/TextDecoder.cpp
Normal file
|
@ -0,0 +1,85 @@
|
|||
/*
|
||||
* Copyright (c) 2022, Ali Mohammad Pur <mpfard@serenityos.org>
|
||||
* Copyright (c) 2024, Simon Wanner <simon@skyrising.xyz>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <AK/FlyString.h>
|
||||
#include <LibJS/Runtime/TypedArray.h>
|
||||
#include <LibWeb/Bindings/Intrinsics.h>
|
||||
#include <LibWeb/Bindings/TextDecoderPrototype.h>
|
||||
#include <LibWeb/Encoding/TextDecoder.h>
|
||||
#include <LibWeb/WebIDL/AbstractOperations.h>
|
||||
#include <LibWeb/WebIDL/Buffers.h>
|
||||
|
||||
namespace Web::Encoding {
|
||||
|
||||
JS_DEFINE_ALLOCATOR(TextDecoder);
|
||||
|
||||
// https://encoding.spec.whatwg.org/#dom-textdecoder
|
||||
WebIDL::ExceptionOr<JS::NonnullGCPtr<TextDecoder>> TextDecoder::construct_impl(JS::Realm& realm, FlyString label, Optional<TextDecoderOptions> const& options)
|
||||
{
|
||||
auto& vm = realm.vm();
|
||||
|
||||
// 1. Let encoding be the result of getting an encoding from label.
|
||||
auto encoding = TextCodec::get_standardized_encoding(label);
|
||||
|
||||
// 2. If encoding is failure or replacement, then throw a RangeError.
|
||||
if (!encoding.has_value() || encoding->equals_ignoring_ascii_case("replacement"sv))
|
||||
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::RangeError, TRY_OR_THROW_OOM(vm, String::formatted("Invalid encoding {}", label)) };
|
||||
|
||||
// 3. Set this’s encoding to encoding.
|
||||
// https://encoding.spec.whatwg.org/#dom-textdecoder-encoding
|
||||
// The encoding getter steps are to return this’s encoding’s name, ASCII lowercased.
|
||||
auto lowercase_encoding_name = MUST(String::from_byte_string(encoding.value().to_lowercase_string()));
|
||||
|
||||
// 4. If options["fatal"] is true, then set this’s error mode to "fatal".
|
||||
auto fatal = options.value_or({}).fatal;
|
||||
|
||||
// 5. Set this’s ignore BOM to options["ignoreBOM"].
|
||||
auto ignore_bom = options.value_or({}).ignore_bom;
|
||||
|
||||
// NOTE: This should happen in decode(), but we don't support streaming yet and share decoders across calls.
|
||||
auto decoder = TextCodec::decoder_for_exact_name(encoding.value());
|
||||
VERIFY(decoder.has_value());
|
||||
|
||||
return realm.heap().allocate<TextDecoder>(realm, realm, *decoder, lowercase_encoding_name, fatal, ignore_bom);
|
||||
}
|
||||
|
||||
// https://encoding.spec.whatwg.org/#dom-textdecoder
|
||||
TextDecoder::TextDecoder(JS::Realm& realm, TextCodec::Decoder& decoder, FlyString encoding, bool fatal, bool ignore_bom)
|
||||
: PlatformObject(realm)
|
||||
, m_decoder(decoder)
|
||||
, m_encoding(move(encoding))
|
||||
, m_fatal(fatal)
|
||||
, m_ignore_bom(ignore_bom)
|
||||
{
|
||||
}
|
||||
|
||||
TextDecoder::~TextDecoder() = default;
|
||||
|
||||
void TextDecoder::initialize(JS::Realm& realm)
|
||||
{
|
||||
Base::initialize(realm);
|
||||
WEB_SET_PROTOTYPE_FOR_INTERFACE(TextDecoder);
|
||||
}
|
||||
|
||||
// https://encoding.spec.whatwg.org/#dom-textdecoder-decode
|
||||
WebIDL::ExceptionOr<String> TextDecoder::decode(Optional<JS::Handle<WebIDL::BufferSource>> const& input, Optional<TextDecodeOptions> const&) const
|
||||
{
|
||||
if (!input.has_value())
|
||||
return TRY_OR_THROW_OOM(vm(), m_decoder.to_utf8({}));
|
||||
|
||||
// FIXME: Implement the streaming stuff.
|
||||
auto data_buffer_or_error = WebIDL::get_buffer_source_copy(*input.value()->raw_object());
|
||||
if (data_buffer_or_error.is_error())
|
||||
return WebIDL::OperationError::create(realm(), "Failed to copy bytes from ArrayBuffer"_string);
|
||||
auto& data_buffer = data_buffer_or_error.value();
|
||||
auto result = TRY_OR_THROW_OOM(vm(), m_decoder.to_utf8({ data_buffer.data(), data_buffer.size() }));
|
||||
if (this->fatal() && result.contains(0xfffd))
|
||||
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Decoding failed"sv };
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
57
Libraries/LibWeb/Encoding/TextDecoder.h
Normal file
57
Libraries/LibWeb/Encoding/TextDecoder.h
Normal file
|
@ -0,0 +1,57 @@
|
|||
/*
|
||||
* Copyright (c) 2022, Ali Mohammad Pur <mpfard@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/Forward.h>
|
||||
#include <AK/NonnullRefPtr.h>
|
||||
#include <LibJS/Forward.h>
|
||||
#include <LibTextCodec/Decoder.h>
|
||||
#include <LibWeb/Bindings/PlatformObject.h>
|
||||
#include <LibWeb/Forward.h>
|
||||
#include <LibWeb/WebIDL/ExceptionOr.h>
|
||||
|
||||
namespace Web::Encoding {
|
||||
|
||||
// https://encoding.spec.whatwg.org/#textdecoderoptions
|
||||
struct TextDecoderOptions {
|
||||
bool fatal = false;
|
||||
bool ignore_bom = false;
|
||||
};
|
||||
|
||||
// https://encoding.spec.whatwg.org/#textdecodeoptions
|
||||
struct TextDecodeOptions {
|
||||
bool stream = false;
|
||||
};
|
||||
|
||||
// https://encoding.spec.whatwg.org/#textdecoder
|
||||
class TextDecoder : public Bindings::PlatformObject {
|
||||
WEB_PLATFORM_OBJECT(TextDecoder, Bindings::PlatformObject);
|
||||
JS_DECLARE_ALLOCATOR(TextDecoder);
|
||||
|
||||
public:
|
||||
static WebIDL::ExceptionOr<JS::NonnullGCPtr<TextDecoder>> construct_impl(JS::Realm&, FlyString encoding, Optional<TextDecoderOptions> const& options = {});
|
||||
|
||||
virtual ~TextDecoder() override;
|
||||
|
||||
WebIDL::ExceptionOr<String> decode(Optional<JS::Handle<WebIDL::BufferSource>> const&, Optional<TextDecodeOptions> const& options = {}) const;
|
||||
|
||||
FlyString const& encoding() const { return m_encoding; }
|
||||
bool fatal() const { return m_fatal; }
|
||||
bool ignore_bom() const { return m_ignore_bom; }
|
||||
|
||||
private:
|
||||
TextDecoder(JS::Realm&, TextCodec::Decoder&, FlyString encoding, bool fatal, bool ignore_bom);
|
||||
|
||||
virtual void initialize(JS::Realm&) override;
|
||||
|
||||
TextCodec::Decoder& m_decoder;
|
||||
FlyString m_encoding;
|
||||
bool m_fatal { false };
|
||||
bool m_ignore_bom { false };
|
||||
};
|
||||
|
||||
}
|
27
Libraries/LibWeb/Encoding/TextDecoder.idl
Normal file
27
Libraries/LibWeb/Encoding/TextDecoder.idl
Normal file
|
@ -0,0 +1,27 @@
|
|||
// https://encoding.spec.whatwg.org/#textdecodercommon
|
||||
interface mixin TextDecoderCommon {
|
||||
readonly attribute DOMString encoding;
|
||||
readonly attribute boolean fatal;
|
||||
readonly attribute boolean ignoreBOM;
|
||||
};
|
||||
|
||||
// https://encoding.spec.whatwg.org/#textdecoderoptions
|
||||
dictionary TextDecoderOptions {
|
||||
boolean fatal = false;
|
||||
boolean ignoreBOM = false;
|
||||
};
|
||||
|
||||
// https://encoding.spec.whatwg.org/#textdecodeoptions
|
||||
dictionary TextDecodeOptions {
|
||||
boolean stream = false;
|
||||
};
|
||||
|
||||
// https://encoding.spec.whatwg.org/#textdecoder
|
||||
[Exposed=*]
|
||||
interface TextDecoder {
|
||||
constructor(optional DOMString label = "utf-8", optional TextDecoderOptions options = {});
|
||||
|
||||
// FIXME: BufferSource is really a AllowSharedBufferSource
|
||||
USVString decode(optional BufferSource input, optional TextDecodeOptions options = {});
|
||||
};
|
||||
TextDecoder includes TextDecoderCommon;
|
116
Libraries/LibWeb/Encoding/TextEncoder.cpp
Normal file
116
Libraries/LibWeb/Encoding/TextEncoder.cpp
Normal file
|
@ -0,0 +1,116 @@
|
|||
/*
|
||||
* Copyright (c) 2021-2022, Linus Groh <linusg@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <LibJS/Runtime/TypedArray.h>
|
||||
#include <LibWeb/Bindings/Intrinsics.h>
|
||||
#include <LibWeb/Bindings/TextEncoderPrototype.h>
|
||||
#include <LibWeb/Encoding/TextEncoder.h>
|
||||
#include <LibWeb/WebIDL/ExceptionOr.h>
|
||||
|
||||
namespace Web::Encoding {
|
||||
|
||||
JS_DEFINE_ALLOCATOR(TextEncoder);
|
||||
|
||||
WebIDL::ExceptionOr<JS::NonnullGCPtr<TextEncoder>> TextEncoder::construct_impl(JS::Realm& realm)
|
||||
{
|
||||
return realm.heap().allocate<TextEncoder>(realm, realm);
|
||||
}
|
||||
|
||||
TextEncoder::TextEncoder(JS::Realm& realm)
|
||||
: PlatformObject(realm)
|
||||
{
|
||||
}
|
||||
|
||||
TextEncoder::~TextEncoder() = default;
|
||||
|
||||
void TextEncoder::initialize(JS::Realm& realm)
|
||||
{
|
||||
Base::initialize(realm);
|
||||
WEB_SET_PROTOTYPE_FOR_INTERFACE(TextEncoder);
|
||||
}
|
||||
|
||||
// https://encoding.spec.whatwg.org/#dom-textencoder-encode
|
||||
JS::NonnullGCPtr<JS::Uint8Array> TextEncoder::encode(String const& input) const
|
||||
{
|
||||
// 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:
|
||||
// 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 = MUST(ByteBuffer::copy(input.bytes()));
|
||||
auto array_length = byte_buffer.size();
|
||||
auto array_buffer = JS::ArrayBuffer::create(realm(), move(byte_buffer));
|
||||
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<WebIDL::BufferSource> const& destination) const
|
||||
{
|
||||
auto& data = destination->viewed_array_buffer()->buffer();
|
||||
|
||||
// 1. Let read be 0.
|
||||
WebIDL::UnsignedLongLong read = 0;
|
||||
// 2. Let written be 0.
|
||||
WebIDL::UnsignedLongLong 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 FlyString const encoding = "utf-8"_fly_string;
|
||||
return encoding;
|
||||
}
|
||||
|
||||
}
|
48
Libraries/LibWeb/Encoding/TextEncoder.h
Normal file
48
Libraries/LibWeb/Encoding/TextEncoder.h
Normal file
|
@ -0,0 +1,48 @@
|
|||
/*
|
||||
* 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/PlatformObject.h>
|
||||
#include <LibWeb/Forward.h>
|
||||
#include <LibWeb/WebIDL/Buffers.h>
|
||||
#include <LibWeb/WebIDL/Types.h>
|
||||
|
||||
namespace Web::Encoding {
|
||||
|
||||
// https://encoding.spec.whatwg.org/#dictdef-textencoderencodeintoresult
|
||||
struct TextEncoderEncodeIntoResult {
|
||||
WebIDL::UnsignedLongLong read;
|
||||
WebIDL::UnsignedLongLong written;
|
||||
};
|
||||
|
||||
// https://encoding.spec.whatwg.org/#textencoder
|
||||
class TextEncoder final : public Bindings::PlatformObject {
|
||||
WEB_PLATFORM_OBJECT(TextEncoder, Bindings::PlatformObject);
|
||||
JS_DECLARE_ALLOCATOR(TextEncoder);
|
||||
|
||||
public:
|
||||
static WebIDL::ExceptionOr<JS::NonnullGCPtr<TextEncoder>> construct_impl(JS::Realm&);
|
||||
|
||||
virtual ~TextEncoder() override;
|
||||
|
||||
JS::NonnullGCPtr<JS::Uint8Array> encode(String const& input) const;
|
||||
TextEncoderEncodeIntoResult encode_into(String const& source, JS::Handle<WebIDL::BufferSource> const& destination) const;
|
||||
|
||||
static FlyString const& encoding();
|
||||
|
||||
protected:
|
||||
// https://encoding.spec.whatwg.org/#dom-textencoder
|
||||
explicit TextEncoder(JS::Realm&);
|
||||
|
||||
virtual void initialize(JS::Realm&) override;
|
||||
};
|
||||
|
||||
}
|
20
Libraries/LibWeb/Encoding/TextEncoder.idl
Normal file
20
Libraries/LibWeb/Encoding/TextEncoder.idl
Normal file
|
@ -0,0 +1,20 @@
|
|||
// 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);
|
||||
};
|
||||
TextEncoder includes TextEncoderCommon;
|
Loading…
Add table
Add a link
Reference in a new issue