LibWeb: Add constructor options to TextDecoder

This commit is contained in:
Bastiaan van der Plaat 2023-10-06 07:02:56 +02:00 committed by Andreas Kling
parent 9ed8c0b183
commit f1ead552ce
5 changed files with 47 additions and 15 deletions

View file

@ -3227,6 +3227,7 @@ void generate_namespace_implementation(IDL::Interface const& interface, StringBu
using namespace Web::CSS;
using namespace Web::DOM;
using namespace Web::DOMParsing;
using namespace Web::Encoding;
using namespace Web::Fetch;
using namespace Web::FileAPI;
using namespace Web::Geometry;
@ -3444,6 +3445,7 @@ void generate_constructor_implementation(IDL::Interface const& interface, String
using namespace Web::CSS;
using namespace Web::DOM;
using namespace Web::DOMParsing;
using namespace Web::Encoding;
using namespace Web::Fetch;
using namespace Web::FileAPI;
using namespace Web::Geometry;
@ -3829,6 +3831,7 @@ using namespace Web::Crypto;
using namespace Web::CSS;
using namespace Web::DOM;
using namespace Web::DOMParsing;
using namespace Web::Encoding;
using namespace Web::Fetch;
using namespace Web::FileAPI;
using namespace Web::Geometry;
@ -3976,6 +3979,7 @@ void generate_iterator_prototype_implementation(IDL::Interface const& interface,
using namespace Web::CSS;
using namespace Web::DOM;
using namespace Web::DOMParsing;
using namespace Web::Encoding;
using namespace Web::Fetch;
using namespace Web::FileAPI;
using namespace Web::Geometry;
@ -4108,6 +4112,7 @@ using namespace Web::Crypto;
using namespace Web::CSS;
using namespace Web::DOM;
using namespace Web::DOMParsing;
using namespace Web::Encoding;
using namespace Web::Fetch;
using namespace Web::FileAPI;
using namespace Web::Geometry;

View file

@ -12,7 +12,7 @@
namespace Web::Encoding {
WebIDL::ExceptionOr<JS::NonnullGCPtr<TextDecoder>> TextDecoder::construct_impl(JS::Realm& realm, FlyString encoding)
WebIDL::ExceptionOr<JS::NonnullGCPtr<TextDecoder>> TextDecoder::construct_impl(JS::Realm& realm, FlyString encoding, Optional<TextDecoderOptions> const& options)
{
auto& vm = realm.vm();
@ -20,7 +20,7 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<TextDecoder>> TextDecoder::construct_impl(J
if (!decoder.has_value())
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, TRY_OR_THROW_OOM(vm, String::formatted("Invalid encoding {}", encoding)) };
return realm.heap().allocate<TextDecoder>(realm, realm, *decoder, move(encoding), false, false);
return realm.heap().allocate<TextDecoder>(realm, realm, *decoder, move(encoding), options.value_or({}).fatal, options.value_or({}).ignore_bom);
}
// https://encoding.spec.whatwg.org/#dom-textdecoder
@ -42,7 +42,7 @@ void TextDecoder::initialize(JS::Realm& realm)
}
// https://encoding.spec.whatwg.org/#dom-textdecoder-decode
WebIDL::ExceptionOr<String> TextDecoder::decode(Optional<JS::Handle<JS::Object>> const& input) const
WebIDL::ExceptionOr<String> TextDecoder::decode(Optional<JS::Handle<JS::Object>> const& input, Optional<TextDecodeOptions> const&) const
{
if (!input.has_value())
return TRY_OR_THROW_OOM(vm(), m_decoder.to_utf8({}));

View file

@ -16,23 +16,33 @@
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);
public:
static WebIDL::ExceptionOr<JS::NonnullGCPtr<TextDecoder>> construct_impl(JS::Realm&, FlyString encoding);
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<JS::Object>> const&) const;
WebIDL::ExceptionOr<String> decode(Optional<JS::Handle<JS::Object>> 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:
// https://encoding.spec.whatwg.org/#dom-textdecoder
TextDecoder(JS::Realm&, TextCodec::Decoder&, FlyString encoding, bool fatal, bool ignore_bom);
virtual void initialize(JS::Realm&) override;

View file

@ -1,13 +1,27 @@
[Exposed=(Window,Worker)]
interface TextDecoder {
// FIXME: 'optional TextDecoderOptions options = {}'
constructor(optional DOMString label = "utf-8");
// FIXME: [AllowShared] on the first parameter.
// FIXME: 'optional TextDecodeOptions options = {}'
USVString decode(optional BufferSource input);
// 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;

View file

@ -254,6 +254,9 @@ class XMLSerializer;
}
namespace Web::Encoding {
struct TextDecodeOptions;
class TextDecoder;
struct TextDecoderOptions;
class TextEncoder;
}