summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKenta Moriuchi <moriken@kimamass.com>2023-11-07 06:18:28 +0900
committerGitHub <noreply@github.com>2023-11-06 22:18:28 +0100
commit90189dd9974be01596ec776e63a4eceef1a036e0 (patch)
tree5ef3171dfc4181e1682d86ccd4daa5c874bc261a
parent7eb34c7a36ac065d169e3645fc8a3da3e71431d7 (diff)
fix(ext): use `String#toWellFormed` in ext/webidl and ext/node (#21054)
Fixes #18802 This PR adds `util.toUSVString` to node:util: ```js import util from "node:util"; util.toUSVString("string\ud801"); // => "string\ufffd" ```
-rw-r--r--cli/tests/node_compat/test/parallel/test-util.js3
-rw-r--r--cli/tests/unit_node/util_test.ts8
-rw-r--r--ext/node/polyfills/util.ts10
-rw-r--r--ext/webidl/00_webidl.js26
4 files changed, 21 insertions, 26 deletions
diff --git a/cli/tests/node_compat/test/parallel/test-util.js b/cli/tests/node_compat/test/parallel/test-util.js
index 5d1ed9b30..eaffc7f6d 100644
--- a/cli/tests/node_compat/test/parallel/test-util.js
+++ b/cli/tests/node_compat/test/parallel/test-util.js
@@ -161,8 +161,7 @@ assert.strictEqual(util.isFunction(function() {}), true);
assert.strictEqual(util.isFunction(), false);
assert.strictEqual(util.isFunction('string'), false);
-// TODO(wafuwafu13): Enable this when `toUSVString` is ready.
-// assert.strictEqual(util.toUSVString('string\ud801'), 'string\ufffd');
+assert.strictEqual(util.toUSVString('string\ud801'), 'string\ufffd');
{
assert.strictEqual(util.types.isNativeError(new Error()), true);
diff --git a/cli/tests/unit_node/util_test.ts b/cli/tests/unit_node/util_test.ts
index 64020141a..e90eed831 100644
--- a/cli/tests/unit_node/util_test.ts
+++ b/cli/tests/unit_node/util_test.ts
@@ -209,6 +209,14 @@ Deno.test({
});
Deno.test({
+ name: "[util] toUSVString",
+ fn() {
+ assertEquals(util.toUSVString("foo"), "foo");
+ assertEquals(util.toUSVString("bar\ud801"), "bar\ufffd");
+ },
+});
+
+Deno.test({
name: "[util] isDate",
fn() {
// Test verifies the method is exposed. See _util/_util_types_test for details
diff --git a/ext/node/polyfills/util.ts b/ext/node/polyfills/util.ts
index 1b611b1fd..2e25746ef 100644
--- a/ext/node/polyfills/util.ts
+++ b/ext/node/polyfills/util.ts
@@ -37,7 +37,9 @@ const {
SafeSet,
SetPrototypeAdd,
SetPrototypeHas,
+ StringPrototypeIsWellFormed,
StringPrototypePadStart,
+ StringPrototypeToWellFormed,
} = primordials;
export {
@@ -187,6 +189,13 @@ export const TextDecoder = _TextDecoder;
export type TextEncoder = import("./_utils.ts")._TextEncoder;
export const TextEncoder = _TextEncoder;
+export function toUSVString(str: string): string {
+ if (StringPrototypeIsWellFormed(str)) {
+ return str;
+ }
+ return StringPrototypeToWellFormed(str);
+}
+
function pad(n: number) {
return StringPrototypePadStart(NumberPrototypeToString(n), 2, "0");
}
@@ -309,6 +318,7 @@ export default {
stripVTControlCharacters,
TextDecoder,
TextEncoder,
+ toUSVString,
log,
debuglog,
isDeepStrictEqual,
diff --git a/ext/webidl/00_webidl.js b/ext/webidl/00_webidl.js
index c3b00286f..956def267 100644
--- a/ext/webidl/00_webidl.js
+++ b/ext/webidl/00_webidl.js
@@ -70,8 +70,8 @@ const {
// TODO(lucacasonato): add SharedArrayBuffer to primordials
// SharedArrayBufferPrototype
String,
- StringFromCodePoint,
StringPrototypeCharCodeAt,
+ StringPrototypeToWellFormed,
Symbol,
SymbolIterator,
SymbolToStringTag,
@@ -425,29 +425,7 @@ converters.ByteString = (V, prefix, context, opts) => {
converters.USVString = (V, prefix, context, opts) => {
const S = converters.DOMString(V, prefix, context, opts);
- const n = S.length;
- let U = "";
- for (let i = 0; i < n; ++i) {
- const c = StringPrototypeCharCodeAt(S, i);
- if (c < 0xd800 || c > 0xdfff) {
- U += StringFromCodePoint(c);
- } else if (0xdc00 <= c && c <= 0xdfff) {
- U += StringFromCodePoint(0xfffd);
- } else if (i === n - 1) {
- U += StringFromCodePoint(0xfffd);
- } else {
- const d = StringPrototypeCharCodeAt(S, i + 1);
- if (0xdc00 <= d && d <= 0xdfff) {
- const a = c & 0x3ff;
- const b = d & 0x3ff;
- U += StringFromCodePoint((2 << 15) + (2 << 9) * a + b);
- ++i;
- } else {
- U += StringFromCodePoint(0xfffd);
- }
- }
- }
- return U;
+ return StringPrototypeToWellFormed(S);
};
converters.object = (V, prefix, context, _opts) => {