diff --git a/Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp b/Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp index 2b3db2a6054..b1e9c8b3433 100644 --- a/Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp +++ b/Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp @@ -644,13 +644,28 @@ static void generate_to_cpp(SourceGenerator& generator, ParameterType& parameter )~~~"); } } else if (parameter.type->name() == "BufferSource") { + if (optional) { + scoped_generator.append(R"~~~( + Optional> @cpp_name@; + if (!@js_name@@js_suffix@.is_undefined()) { +)~~~"); + } else { + scoped_generator.append(R"~~~( + JS::Handle @cpp_name@; +)~~~"); + } scoped_generator.append(R"~~~( if (!@js_name@@js_suffix@.is_object() || !(is(@js_name@@js_suffix@.as_object()) || is(@js_name@@js_suffix@.as_object()) || is(@js_name@@js_suffix@.as_object()))) return vm.throw_completion(JS::ErrorType::NotAnObjectOfType, "@parameter.type.name@"); // TODO: Should we make this a Variant? - auto @cpp_name@ = JS::make_handle(&@js_name@@js_suffix@.as_object()); + @cpp_name@ = JS::make_handle(&@js_name@@js_suffix@.as_object()); )~~~"); + if (optional) { + scoped_generator.append(R"~~~( + } +)~~~"); + } } else if (parameter.type->name() == "any") { if (variadic) { scoped_generator.append(R"~~~( diff --git a/Tests/LibWeb/Text/expected/TextDecoder/TextDecoder_decode.txt b/Tests/LibWeb/Text/expected/TextDecoder/TextDecoder_decode.txt new file mode 100644 index 00000000000..8fc16ea51f2 --- /dev/null +++ b/Tests/LibWeb/Text/expected/TextDecoder/TextDecoder_decode.txt @@ -0,0 +1,2 @@ +[ABC] +[] diff --git a/Tests/LibWeb/Text/input/TextDecoder/TextDecoder_decode.html b/Tests/LibWeb/Text/input/TextDecoder/TextDecoder_decode.html new file mode 100644 index 00000000000..4a2273770ce --- /dev/null +++ b/Tests/LibWeb/Text/input/TextDecoder/TextDecoder_decode.html @@ -0,0 +1,12 @@ + + diff --git a/Userland/Libraries/LibWeb/Encoding/TextDecoder.cpp b/Userland/Libraries/LibWeb/Encoding/TextDecoder.cpp index d975b7f8b58..eb02aa78221 100644 --- a/Userland/Libraries/LibWeb/Encoding/TextDecoder.cpp +++ b/Userland/Libraries/LibWeb/Encoding/TextDecoder.cpp @@ -44,11 +44,14 @@ JS::ThrowCompletionOr TextDecoder::initialize(JS::Realm& realm) } // https://encoding.spec.whatwg.org/#dom-textdecoder-decode -WebIDL::ExceptionOr TextDecoder::decode(JS::Handle const& input) const +WebIDL::ExceptionOr TextDecoder::decode(Optional> const& input) 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.cell()); + auto data_buffer_or_error = WebIDL::get_buffer_source_copy(*input->cell()); if (data_buffer_or_error.is_error()) return WebIDL::OperationError::create(realm(), "Failed to copy bytes from ArrayBuffer"); auto& data_buffer = data_buffer_or_error.value(); diff --git a/Userland/Libraries/LibWeb/Encoding/TextDecoder.h b/Userland/Libraries/LibWeb/Encoding/TextDecoder.h index 02210830ae7..e5e7c72f9ba 100644 --- a/Userland/Libraries/LibWeb/Encoding/TextDecoder.h +++ b/Userland/Libraries/LibWeb/Encoding/TextDecoder.h @@ -25,7 +25,7 @@ public: virtual ~TextDecoder() override; - WebIDL::ExceptionOr decode(JS::Handle const&) const; + WebIDL::ExceptionOr decode(Optional> const&) const; DeprecatedFlyString const& encoding() const { return m_encoding; } bool fatal() const { return m_fatal; }