Skip to content

Commit

Permalink
Use correct replacement character for lone surrogates in `TextEncoder…
Browse files Browse the repository at this point in the history
…#encode()`
  • Loading branch information
TooTallNate committed Apr 3, 2024
1 parent 4648806 commit 1cbfc49
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .changeset/gorgeous-pants-train.md
@@ -0,0 +1,5 @@
---
"nxjs-runtime": patch
---

Use correct replacement character for lone surrogates in `TextEncoder#encode()`
23 changes: 9 additions & 14 deletions packages/runtime/src/polyfills/text-encoder.ts
Expand Up @@ -40,22 +40,17 @@ export class TextEncoder implements globalThis.TextEncoder {
if ((extra & 0xfc00) === 0xdc00) {
++pos;
value = ((value & 0x3ff) << 10) + (extra & 0x3ff) + 0x10000;
} else {
// Lone high surrogate, encode as replacement character
value = 0xfffd;
}
} else {
// Lone high surrogate at the end of the string, encode as replacement character
value = 0xfffd;
}
if (value >= 0xd800 && value <= 0xdbff) {
continue; // drop lone surrogate
}
}

// expand the buffer if we couldn't write 4 bytes
if (at + 4 > target.length) {
tlen += 8; // minimum extra
tlen *= 1.0 + (pos / input.length) * 2; // take 2x the remaining
tlen = (tlen >>> 3) << 3; // 8 byte offset

var update = new Uint8Array(tlen);
update.set(target);
target = update;
} else if (value >= 0xdc00 && value <= 0xdfff) {
// Lone low surrogate, encode as replacement character
value = 0xfffd;
}

if ((value & 0xffffff80) === 0) {
Expand Down

0 comments on commit 1cbfc49

Please sign in to comment.