TextEncoder.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * Copyright (c) 2021-2022, Linus Groh <linusg@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Runtime/TypedArray.h>
  7. #include <LibWeb/Bindings/Intrinsics.h>
  8. #include <LibWeb/Encoding/TextEncoder.h>
  9. #include <LibWeb/WebIDL/ExceptionOr.h>
  10. namespace Web::Encoding {
  11. JS_DEFINE_ALLOCATOR(TextEncoder);
  12. WebIDL::ExceptionOr<JS::NonnullGCPtr<TextEncoder>> TextEncoder::construct_impl(JS::Realm& realm)
  13. {
  14. return realm.heap().allocate<TextEncoder>(realm, realm);
  15. }
  16. TextEncoder::TextEncoder(JS::Realm& realm)
  17. : PlatformObject(realm)
  18. {
  19. }
  20. TextEncoder::~TextEncoder() = default;
  21. void TextEncoder::initialize(JS::Realm& realm)
  22. {
  23. Base::initialize(realm);
  24. WEB_SET_PROTOTYPE_FOR_INTERFACE(TextEncoder);
  25. }
  26. // https://encoding.spec.whatwg.org/#dom-textencoder-encode
  27. JS::NonnullGCPtr<JS::Uint8Array> TextEncoder::encode(String const& input) const
  28. {
  29. // NOTE: The AK::String is always UTF-8, so most of these steps are no-ops.
  30. // 1. Convert input to an I/O queue of scalar values.
  31. // 2. Let output be the I/O queue of bytes « end-of-queue ».
  32. // 3. While true:
  33. // 1. Let item be the result of reading from input.
  34. // 2. Let result be the result of processing an item with item, an instance of the UTF-8 encoder, input, output, and "fatal".
  35. // 3. Assert: result is not an error.
  36. // 4. If result is finished, then convert output into a byte sequence and return a Uint8Array object wrapping an ArrayBuffer containing output.
  37. auto byte_buffer = MUST(ByteBuffer::copy(input.bytes()));
  38. auto array_length = byte_buffer.size();
  39. auto array_buffer = JS::ArrayBuffer::create(realm(), move(byte_buffer));
  40. return JS::Uint8Array::create(realm(), array_length, *array_buffer);
  41. }
  42. // https://encoding.spec.whatwg.org/#dom-textencoder-encodeinto
  43. TextEncoderEncodeIntoResult TextEncoder::encode_into(String const& source, JS::Handle<WebIDL::BufferSource> const& destination) const
  44. {
  45. auto& data = destination->viewed_array_buffer()->buffer();
  46. // 1. Let read be 0.
  47. WebIDL::UnsignedLongLong read = 0;
  48. // 2. Let written be 0.
  49. WebIDL::UnsignedLongLong written = 0;
  50. // NOTE: The AK::String is always UTF-8, so most of these steps are no-ops.
  51. // 3. Let encoder be an instance of the UTF-8 encoder.
  52. // 4. Let unused be the I/O queue of scalar values « end-of-queue ».
  53. // 5. Convert source to an I/O queue of scalar values.
  54. auto code_points = source.code_points();
  55. auto it = code_points.begin();
  56. // 6. While true:
  57. while (true) {
  58. // 6.1. Let item be the result of reading from source.
  59. // 6.2. Let result be the result of running encoder’s handler on unused and item.
  60. // 6.3. If result is finished, then break.
  61. if (it.done())
  62. break;
  63. auto item = *it;
  64. auto result = it.underlying_code_point_bytes();
  65. // 6.4. Otherwise:
  66. // 6.4.1. If destination’s byte length − written is greater than or equal to the number of bytes in result, then:
  67. if (data.size() - written >= result.size()) {
  68. // 6.4.1.1. If item is greater than U+FFFF, then increment read by 2.
  69. if (item > 0xffff) {
  70. read += 2;
  71. }
  72. // 6.4.1.2. Otherwise, increment read by 1.
  73. else {
  74. read++;
  75. }
  76. // 6.4.1.3. Write the bytes in result into destination, with startingOffset set to written.
  77. // 6.4.1.4. Increment written by the number of bytes in result.
  78. for (auto byte : result)
  79. data[written++] = byte;
  80. }
  81. // 6.4.2. Otherwise, break.
  82. else {
  83. break;
  84. }
  85. ++it;
  86. }
  87. // 7. Return «[ "read" → read, "written" → written ]».
  88. return { read, written };
  89. }
  90. // https://encoding.spec.whatwg.org/#dom-textencoder-encoding
  91. FlyString const& TextEncoder::encoding()
  92. {
  93. static FlyString const encoding = "utf-8"_fly_string;
  94. return encoding;
  95. }
  96. }