Skip to content

Commit 0fd80a8

Browse files
trflynn89gmta
authored andcommitted
LibTextCodec+LibWeb: Move isomorphic coders to LibTextCodec
This will be used outside of LibWeb.
1 parent 0480934 commit 0fd80a8

File tree

17 files changed

+65
-64
lines changed

17 files changed

+65
-64
lines changed

Libraries/LibTextCodec/Decoder.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1299,4 +1299,18 @@ ErrorOr<void> ReplacementDecoder::process(StringView input, Function<ErrorOr<voi
12991299
return {};
13001300
}
13011301

1302+
// https://infra.spec.whatwg.org/#isomorphic-decode
1303+
String isomorphic_decode(StringView input)
1304+
{
1305+
// To isomorphic decode a byte sequence input, return a string whose code point length is equal to input’s length
1306+
// and whose code points have the same values as the values of input’s bytes, in the same order.
1307+
// NB: This is essentially spec-speak for "Decode as ISO-8859-1 / Latin-1".
1308+
StringBuilder builder(input.length());
1309+
1310+
for (auto byte : input.bytes())
1311+
builder.append_code_point(byte);
1312+
1313+
return builder.to_string_without_validation();
1314+
}
1315+
13021316
}

Libraries/LibTextCodec/Decoder.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,4 +135,6 @@ TEXTCODEC_API ErrorOr<String> convert_input_to_utf8_using_given_decoder_unless_t
135135

136136
TEXTCODEC_API StringView get_output_encoding(StringView encoding);
137137

138+
TEXTCODEC_API String isomorphic_decode(StringView);
139+
138140
}

Libraries/LibTextCodec/Encoder.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -667,4 +667,23 @@ ErrorOr<void> SingleByteEncoder<ArrayType>::process(Utf8View input, Function<Err
667667
return {};
668668
}
669669

670+
// https://infra.spec.whatwg.org/#isomorphic-encode
671+
ByteString isomorphic_encode(StringView input)
672+
{
673+
// To isomorphic encode an isomorphic string input: return a byte sequence whose length is equal to input’s code
674+
// point length and whose bytes have the same values as the values of input’s code points, in the same order.
675+
// NB: This is essentially spec-speak for "Encode as ISO-8859-1 / Latin-1".
676+
StringBuilder builder(input.length());
677+
678+
for (auto code_point : Utf8View { input }) {
679+
// VERIFY(code_point <= 0xFF);
680+
if (code_point > 0xFF)
681+
dbgln("FIXME: Trying to isomorphic encode a string with code points > U+00FF.");
682+
683+
builder.append(static_cast<u8>(code_point));
684+
}
685+
686+
return builder.to_byte_string();
687+
}
688+
670689
}

Libraries/LibTextCodec/Encoder.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,6 @@ class SingleByteEncoder final : public Encoder {
9090
TEXTCODEC_API Optional<Encoder&> encoder_for_exact_name(StringView encoding);
9191
TEXTCODEC_API Optional<Encoder&> encoder_for(StringView label);
9292

93+
TEXTCODEC_API ByteString isomorphic_encode(StringView);
94+
9395
}

Libraries/LibWeb/ContentSecurityPolicy/Policy.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include <AK/GenericLexer.h>
88
#include <AK/String.h>
9+
#include <LibTextCodec/Decoder.h>
910
#include <LibWeb/ContentSecurityPolicy/Directives/DirectiveFactory.h>
1011
#include <LibWeb/ContentSecurityPolicy/Directives/SerializedDirective.h>
1112
#include <LibWeb/ContentSecurityPolicy/Policy.h>
@@ -14,7 +15,6 @@
1415
#include <LibWeb/Fetch/Infrastructure/HTTP/Headers.h>
1516
#include <LibWeb/Fetch/Infrastructure/HTTP/Responses.h>
1617
#include <LibWeb/Infra/CharacterTypes.h>
17-
#include <LibWeb/Infra/Strings.h>
1818

1919
namespace Web::ContentSecurityPolicy {
2020

@@ -31,7 +31,7 @@ GC::Ref<Policy> Policy::parse_a_serialized_csp(GC::Heap& heap, Variant<ByteStrin
3131
// 1. If serialized is a byte sequence, then set serialized to be the result of isomorphic decoding serialized.
3232
auto serialized_string = serialized.has<String>()
3333
? serialized.get<String>()
34-
: Infra::isomorphic_decode(serialized.get<ByteString>());
34+
: TextCodec::isomorphic_decode(serialized.get<ByteString>());
3535

3636
// 2. Let policy be a new policy with an empty directive set, a source of source, and a disposition of disposition.
3737
auto policy = heap.allocate<Policy>();

Libraries/LibWeb/DOM/Document.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <LibJS/Runtime/Array.h>
2424
#include <LibJS/Runtime/FunctionObject.h>
2525
#include <LibJS/Runtime/NativeFunction.h>
26+
#include <LibTextCodec/Decoder.h>
2627
#include <LibURL/Origin.h>
2728
#include <LibURL/Parser.h>
2829
#include <LibUnicode/Segmenter.h>
@@ -425,7 +426,7 @@ WebIDL::ExceptionOr<GC::Ref<Document>> Document::create_and_initialize(Type type
425426
// 15. If navigationParams's response has a `Refresh` header, then:
426427
if (auto maybe_refresh = navigation_params.response->header_list()->get("Refresh"sv); maybe_refresh.has_value()) {
427428
// 1. Let value be the isomorphic decoding of the value of the header.
428-
auto value = Infra::isomorphic_decode(maybe_refresh.value());
429+
auto value = TextCodec::isomorphic_decode(maybe_refresh.value());
429430

430431
// 2. Run the shared declarative refresh steps with document and value.
431432
document->shared_declarative_refresh_steps(value, nullptr);

Libraries/LibWeb/Fetch/Body.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
#include <LibWeb/FileAPI/File.h>
2525
#include <LibWeb/HTML/Scripting/TemporaryExecutionContext.h>
2626
#include <LibWeb/Infra/JSON.h>
27-
#include <LibWeb/Infra/Strings.h>
2827
#include <LibWeb/MimeSniff/MimeType.h>
2928
#include <LibWeb/Streams/ReadableStream.h>
3029
#include <LibWeb/WebIDL/Promise.h>
@@ -353,7 +352,7 @@ static MultipartParsingErrorOr<MultiPartFormDataHeader> parse_multipart_form_dat
353352
header_value = header_value.trim(Infrastructure::HTTP_TAB_OR_SPACE, TrimMode::Right);
354353

355354
// 3. Set contentType to the isomorphic decoding of header value.
356-
header.content_type = Infra::isomorphic_decode(header_value.bytes());
355+
header.content_type = TextCodec::isomorphic_decode(header_value);
357356
}
358357
// -> Otherwise
359358
else {

Libraries/LibWeb/Fetch/Fetching/Fetching.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <AK/ScopeGuard.h>
1515
#include <LibJS/Runtime/Completion.h>
1616
#include <LibRequests/RequestTimingInfo.h>
17+
#include <LibTextCodec/Encoder.h>
1718
#include <LibWeb/Bindings/MainThreadVM.h>
1819
#include <LibWeb/Bindings/PrincipalHostDefined.h>
1920
#include <LibWeb/ContentSecurityPolicy/BlockingAlgorithms.h>
@@ -51,7 +52,6 @@
5152
#include <LibWeb/HTML/Window.h>
5253
#include <LibWeb/HTML/WorkerGlobalScope.h>
5354
#include <LibWeb/HighResolutionTime/TimeOrigin.h>
54-
#include <LibWeb/Infra/Strings.h>
5555
#include <LibWeb/Loader/LoadRequest.h>
5656
#include <LibWeb/Loader/ResourceLoader.h>
5757
#include <LibWeb/MixedContent/AbstractOperations.h>
@@ -1831,7 +1831,7 @@ GC::Ref<PendingResponse> http_network_or_cache_fetch(JS::Realm& realm, Infrastru
18311831
// 11. If httpRequest’s referrer is a URL, then:
18321832
if (auto const* referrer_url = http_request->referrer().get_pointer<URL::URL>()) {
18331833
// 1. Let referrerValue be httpRequest’s referrer, serialized and isomorphic encoded.
1834-
auto referrer_value = Infra::isomorphic_encode(referrer_url->serialize());
1834+
auto referrer_value = TextCodec::isomorphic_encode(referrer_url->serialize());
18351835

18361836
// 2. Append (`Referer`, referrerValue) to httpRequest’s header list.
18371837
http_request->header_list()->append({ "Referer"sv, move(referrer_value) });

Libraries/LibWeb/Fetch/Headers.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66

77
#include <LibJS/Runtime/Completion.h>
88
#include <LibJS/Runtime/VM.h>
9+
#include <LibTextCodec/Decoder.h>
910
#include <LibWeb/Bindings/HeadersPrototype.h>
1011
#include <LibWeb/Bindings/Intrinsics.h>
1112
#include <LibWeb/Fetch/Headers.h>
12-
#include <LibWeb/Infra/Strings.h>
1313

1414
namespace Web::Fetch {
1515

@@ -102,7 +102,7 @@ WebIDL::ExceptionOr<Optional<String>> Headers::get(String const& name)
102102

103103
// 2. Return the result of getting name from this’s header list.
104104
auto byte_buffer = m_header_list->get(name);
105-
return byte_buffer.has_value() ? Infra::isomorphic_decode(*byte_buffer) : Optional<String> {};
105+
return byte_buffer.has_value() ? TextCodec::isomorphic_decode(*byte_buffer) : Optional<String> {};
106106
}
107107

108108
// https://fetch.spec.whatwg.org/#dom-headers-getsetcookie
@@ -119,7 +119,7 @@ Vector<String> Headers::get_set_cookie()
119119
// `Set-Cookie`, in order.
120120
for (auto const& header : *m_header_list) {
121121
if (header.name.equals_ignoring_ascii_case("Set-Cookie"sv))
122-
values.append(Infra::isomorphic_decode(header.value));
122+
values.append(TextCodec::isomorphic_decode(header.value));
123123
}
124124
return values;
125125
}
@@ -187,7 +187,7 @@ JS::ThrowCompletionOr<void> Headers::for_each(ForEachCallback callback)
187187
auto const& pair = pairs[i];
188188

189189
// 2. Invoke idlCallback with « pair’s value, pair’s key, idlObject » and with thisArg as the callback this value.
190-
TRY(callback(Infra::isomorphic_decode(pair.name), Infra::isomorphic_decode(pair.value)));
190+
TRY(callback(TextCodec::isomorphic_decode(pair.name), TextCodec::isomorphic_decode(pair.value)));
191191

192192
// 3. Set pairs to idlObject’s current list of value pairs to iterate over. (It might have changed.)
193193
pairs = value_pairs_to_iterate_over();

Libraries/LibWeb/Fetch/HeadersIterator.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66

77
#include <LibJS/Runtime/Array.h>
88
#include <LibJS/Runtime/Iterator.h>
9+
#include <LibTextCodec/Decoder.h>
910
#include <LibWeb/Bindings/HeadersIteratorPrototype.h>
1011
#include <LibWeb/Bindings/Intrinsics.h>
1112
#include <LibWeb/Fetch/HeadersIterator.h>
12-
#include <LibWeb/Infra/Strings.h>
1313

1414
namespace Web::Bindings {
1515

@@ -66,8 +66,8 @@ GC::Ref<JS::Object> HeadersIterator::next()
6666
return create_iterator_result_object(vm(), JS::js_undefined(), true);
6767

6868
auto const& pair = pairs[m_index++];
69-
auto pair_name = Infra::isomorphic_decode(pair.name);
70-
auto pair_value = Infra::isomorphic_decode(pair.value);
69+
auto pair_name = TextCodec::isomorphic_decode(pair.name);
70+
auto pair_value = TextCodec::isomorphic_decode(pair.value);
7171

7272
switch (m_iteration_kind) {
7373
case JS::Object::PropertyKind::Key:

0 commit comments

Comments
 (0)