ladybird/Libraries/LibWeb/Encoding/TextDecoderCommon.h
Shannon Booth ef6753a9f9 LibWeb+LibTextCodec: Wire decoder options through TextDecoder
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.
2026-06-23 07:25:11 +02:00

44 lines
1.3 KiB
C++

/*
* Copyright (c) 2026-present, the Ladybird developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/FlyString.h>
#include <LibTextCodec/Decoder.h>
namespace Web::Encoding {
// https://encoding.spec.whatwg.org/#textdecodercommon
class TextDecoderCommonMixin {
public:
virtual ~TextDecoderCommonMixin();
// https://encoding.spec.whatwg.org/#dom-textdecoder-encoding
FlyString const& encoding() const { return m_encoding; }
// https://encoding.spec.whatwg.org/#dom-textdecoder-fatal
bool fatal() const { return m_error_mode == TextCodec::ErrorMode::Fatal; }
// https://encoding.spec.whatwg.org/#dom-textdecoder-ignorebom
bool ignore_bom() const { return m_ignore_bom; }
protected:
TextDecoderCommonMixin(TextCodec::Decoder& decoder, FlyString encoding, TextCodec::ErrorMode error_mode, bool ignore_bom);
// https://encoding.spec.whatwg.org/#textdecodercommon-decoder
TextCodec::Decoder& m_decoder;
// https://encoding.spec.whatwg.org/#textdecoder-encoding
FlyString m_encoding;
// https://encoding.spec.whatwg.org/#textdecoder-error-mode
TextCodec::ErrorMode m_error_mode { TextCodec::ErrorMode::Replacement };
// https://encoding.spec.whatwg.org/#textdecoder-ignore-bom-flag
bool m_ignore_bom { false };
};
}