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
Notes: sideshowbarker 2024-07-17 04:21:32 +09:00
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::CSS;
using namespace Web::DOM; using namespace Web::DOM;
using namespace Web::DOMParsing; using namespace Web::DOMParsing;
using namespace Web::Encoding;
using namespace Web::Fetch; using namespace Web::Fetch;
using namespace Web::FileAPI; using namespace Web::FileAPI;
using namespace Web::Geometry; using namespace Web::Geometry;
@ -3444,6 +3445,7 @@ void generate_constructor_implementation(IDL::Interface const& interface, String
using namespace Web::CSS; using namespace Web::CSS;
using namespace Web::DOM; using namespace Web::DOM;
using namespace Web::DOMParsing; using namespace Web::DOMParsing;
using namespace Web::Encoding;
using namespace Web::Fetch; using namespace Web::Fetch;
using namespace Web::FileAPI; using namespace Web::FileAPI;
using namespace Web::Geometry; using namespace Web::Geometry;
@ -3829,6 +3831,7 @@ using namespace Web::Crypto;
using namespace Web::CSS; using namespace Web::CSS;
using namespace Web::DOM; using namespace Web::DOM;
using namespace Web::DOMParsing; using namespace Web::DOMParsing;
using namespace Web::Encoding;
using namespace Web::Fetch; using namespace Web::Fetch;
using namespace Web::FileAPI; using namespace Web::FileAPI;
using namespace Web::Geometry; using namespace Web::Geometry;
@ -3976,6 +3979,7 @@ void generate_iterator_prototype_implementation(IDL::Interface const& interface,
using namespace Web::CSS; using namespace Web::CSS;
using namespace Web::DOM; using namespace Web::DOM;
using namespace Web::DOMParsing; using namespace Web::DOMParsing;
using namespace Web::Encoding;
using namespace Web::Fetch; using namespace Web::Fetch;
using namespace Web::FileAPI; using namespace Web::FileAPI;
using namespace Web::Geometry; using namespace Web::Geometry;
@ -4108,6 +4112,7 @@ using namespace Web::Crypto;
using namespace Web::CSS; using namespace Web::CSS;
using namespace Web::DOM; using namespace Web::DOM;
using namespace Web::DOMParsing; using namespace Web::DOMParsing;
using namespace Web::Encoding;
using namespace Web::Fetch; using namespace Web::Fetch;
using namespace Web::FileAPI; using namespace Web::FileAPI;
using namespace Web::Geometry; using namespace Web::Geometry;

View file

@ -12,7 +12,7 @@
namespace Web::Encoding { 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(); auto& vm = realm.vm();
@ -20,7 +20,7 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<TextDecoder>> TextDecoder::construct_impl(J
if (!decoder.has_value()) if (!decoder.has_value())
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, TRY_OR_THROW_OOM(vm, String::formatted("Invalid encoding {}", encoding)) }; 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 // 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 // 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()) if (!input.has_value())
return TRY_OR_THROW_OOM(vm(), m_decoder.to_utf8({})); return TRY_OR_THROW_OOM(vm(), m_decoder.to_utf8({}));

View file

@ -16,23 +16,33 @@
namespace Web::Encoding { 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 // https://encoding.spec.whatwg.org/#textdecoder
class TextDecoder : public Bindings::PlatformObject { class TextDecoder : public Bindings::PlatformObject {
WEB_PLATFORM_OBJECT(TextDecoder, Bindings::PlatformObject); WEB_PLATFORM_OBJECT(TextDecoder, Bindings::PlatformObject);
public: 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; 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; } FlyString const& encoding() const { return m_encoding; }
bool fatal() const { return m_fatal; } bool fatal() const { return m_fatal; }
bool ignore_bom() const { return m_ignore_bom; } bool ignore_bom() const { return m_ignore_bom; }
private: private:
// https://encoding.spec.whatwg.org/#dom-textdecoder
TextDecoder(JS::Realm&, TextCodec::Decoder&, FlyString encoding, bool fatal, bool ignore_bom); TextDecoder(JS::Realm&, TextCodec::Decoder&, FlyString encoding, bool fatal, bool ignore_bom);
virtual void initialize(JS::Realm&) override; virtual void initialize(JS::Realm&) override;

View file

@ -1,13 +1,27 @@
[Exposed=(Window,Worker)] // https://encoding.spec.whatwg.org/#textdecodercommon
interface TextDecoder { interface mixin TextDecoderCommon {
// 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);
readonly attribute DOMString encoding; readonly attribute DOMString encoding;
readonly attribute boolean fatal; readonly attribute boolean fatal;
readonly attribute boolean ignoreBOM; 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 { namespace Web::Encoding {
struct TextDecodeOptions;
class TextDecoder;
struct TextDecoderOptions;
class TextEncoder; class TextEncoder;
} }