Add explicit IgnoreBOM and ErrorMode options to LibTextCodec decoders, and thread them through TextDecoder and TextDecoderStream. This lets Web-facing decoder APIs preserve BOMs when requested and use fatal error handling without post-processing decoded output. NB: RemoveBOM was renamed to IgnoreBOM as "RemoveBOM" is the name used by encoding_rs and was previously an implementation detail. The new name matches what is used by the encoding standard as it is now also used in LibWeb.
42 lines
1.2 KiB
C++
42 lines
1.2 KiB
C++
/*
|
|
* 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/Bindings/TextDecoder.h>
|
|
#include <LibWeb/Encoding/TextDecoderCommon.h>
|
|
#include <LibWeb/Forward.h>
|
|
#include <LibWeb/WebIDL/Buffers.h>
|
|
#include <LibWeb/WebIDL/ExceptionOr.h>
|
|
|
|
namespace Web::Encoding {
|
|
|
|
// https://encoding.spec.whatwg.org/#textdecoder
|
|
class TextDecoder
|
|
: public Bindings::PlatformObject
|
|
, public TextDecoderCommonMixin {
|
|
WEB_PLATFORM_OBJECT(TextDecoder, Bindings::PlatformObject);
|
|
GC_DECLARE_ALLOCATOR(TextDecoder);
|
|
|
|
public:
|
|
static WebIDL::ExceptionOr<GC::Ref<TextDecoder>> construct_impl(JS::Realm&, StringView label, Bindings::TextDecoderOptions const&);
|
|
|
|
virtual ~TextDecoder() override;
|
|
|
|
WebIDL::ExceptionOr<String> decode(Optional<WebIDL::BufferSourceVariant>, Optional<Bindings::TextDecodeOptions> const& options = {}) const;
|
|
|
|
private:
|
|
TextDecoder(JS::Realm&, TextCodec::Decoder&, FlyString encoding, TextCodec::ErrorMode error_mode, bool ignore_bom);
|
|
|
|
virtual void initialize(JS::Realm&) override;
|
|
};
|
|
|
|
}
|