TextEncoder.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import { DEFAULT_ENCODING } from "../encoding/defaultEncoding";
  2. import { encoders } from "../encoding/encoding-factory";
  3. import { getEncoding } from "../encoding/encodings";
  4. import { finished } from "../encoding/finished";
  5. import { end_of_stream } from "../encoding/terminology";
  6. import { stringToCodePoints, ToDictionary } from "../encoding/utilities";
  7. import { getGlobalScope } from "../helper/getGlobalScope";
  8. import { Stream } from "./Stream";
  9. /**
  10. * @constructor
  11. * @param {string=} label The label of the encoding. NONSTANDARD.
  12. * @param {Object=} options NONSTANDARD.
  13. */
  14. export class TextEncoder {
  15. constructor(label, options) {
  16. const optionsMap = ToDictionary(options);
  17. // A TextEncoder object has an associated encoding and encoder.
  18. /** @private */
  19. this._encoding = null;
  20. /** @private @type {?Encoder} */
  21. this._encoder = null;
  22. // Non-standard
  23. /** @private @type {boolean} */
  24. this._do_not_flush = false;
  25. /** @private @type {string} */
  26. this._fatal = Boolean(optionsMap['fatal']) ? 'fatal' : 'replacement';
  27. // 1. Let enc be a new TextEncoder object.
  28. // const enc = this;
  29. // no need to do this as this is a proper class
  30. // now and TSC will handle transpilation to older platforms
  31. // 2. Set enc's encoding to UTF-8's encoder.
  32. if (Boolean(optionsMap['NONSTANDARD_allowLegacyEncoding'])) {
  33. // NONSTANDARD behavior.
  34. label = !!label ? String(label) : DEFAULT_ENCODING;
  35. const encoding = getEncoding(label);
  36. if (encoding === null || encoding.name === 'replacement')
  37. throw RangeError('Unknown encoding: ' + label);
  38. if (!encoders[encoding.name]) {
  39. throw Error('Encoder not present.' +
  40. ' Did you forget to include encoding-indexes.js first?');
  41. }
  42. this._encoding = encoding;
  43. // EXPERIMENTAL_CODE
  44. // } else if (["iso-8859-1", "ISO-8859-1", "latin-1", "latin1", "LATIN-1", "LATIN1"].indexOf(label) !== -1) {
  45. // this._encoding = getEncoding('iso-8859-1');
  46. }
  47. else {
  48. // Standard behavior.
  49. this._encoding = getEncoding('utf-8');
  50. const glo = getGlobalScope() || {};
  51. if (label !== undefined && 'console' in glo) {
  52. console.warn('TextEncoder constructor called with encoding label, '
  53. + 'which is ignored.');
  54. }
  55. }
  56. // For pre-ES5 runtimes:
  57. // if (!Object.defineProperty)
  58. // this.encoding = enc._encoding.name.toLowerCase();
  59. // 3. Return enc.
  60. // return enc;
  61. }
  62. // if(Object.defineProperty) {
  63. // // The encoding attribute's getter must return encoding's name.
  64. // Object.defineProperty(TextEncoder.prototype, 'encoding', {
  65. // /** @this {TextEncoder} */
  66. // get: function () { return this._encoding.name.toLowerCase(); }
  67. // });
  68. // }
  69. get encoding() {
  70. return this._encoding.name.toLowerCase();
  71. }
  72. /**
  73. * @param {string=} opt_string The string to encode.
  74. * @param {Object=} options
  75. * @return {!Uint8Array} Encoded bytes, as a Uint8Array.
  76. */
  77. encode(opt_string, options) {
  78. opt_string = opt_string === undefined ? '' : String(opt_string);
  79. const optionsMap = ToDictionary(options);
  80. // NOTE: This option is nonstandard. None of the encodings
  81. // permitted for encoding (i.e. UTF-8, UTF-16) are stateful when
  82. // the input is a USVString so streaming is not necessary.
  83. if (!this._do_not_flush)
  84. this._encoder = encoders[this._encoding.name]({
  85. fatal: this._fatal === 'fatal'
  86. });
  87. this._do_not_flush = Boolean(optionsMap['stream']);
  88. // 1. Convert input to a stream.
  89. const input = new Stream(stringToCodePoints(opt_string));
  90. // 2. Let output be a new stream
  91. const output = [];
  92. /** @type {?(number|!Array.<number>)} */
  93. let result;
  94. // 3. While true, run these substeps:
  95. while (true) {
  96. // 1. Let token be the result of reading from input.
  97. const token = input.read();
  98. if (token === end_of_stream)
  99. break;
  100. // 2. Let result be the result of processing token for encoder,
  101. // input, output.
  102. result = this._encoder.handler(input, token);
  103. if (result === finished)
  104. break;
  105. if (Array.isArray(result))
  106. output.push.apply(output, /**@type {!Array.<number>}*/ (result));
  107. else
  108. output.push(result);
  109. }
  110. // TODO: Align with spec algorithm.
  111. if (!this._do_not_flush) {
  112. while (true) {
  113. result = this._encoder.handler(input, input.read());
  114. if (result === finished)
  115. break;
  116. if (Array.isArray(result))
  117. output.push.apply(output, /**@type {!Array.<number>}*/ (result));
  118. else
  119. output.push(result);
  120. }
  121. this._encoder = null;
  122. }
  123. // 3. If result is finished, convert output into a byte sequence,
  124. // and then return a Uint8Array object wrapping an ArrayBuffer
  125. // containing output.
  126. return new Uint8Array(output);
  127. }
  128. }
  129. //# sourceMappingURL=TextEncoder.js.map