1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-25 15:29:32 -05:00

fix(ext/node): fix whatwg url formatting (#19146)

This commit is contained in:
Yoshiya Hinosawa 2023-05-17 14:51:51 +09:00 committed by GitHub
parent f59b1d8a30
commit 243d9c846d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 157 additions and 12 deletions

View file

@ -615,6 +615,7 @@
"test-url-domain-ascii-unicode.js",
"test-url-fileurltopath.js",
"test-url-format-invalid-input.js",
"test-url-format-whatwg.js",
"test-url-format.js",
"test-url-parse-invalid-input.js",
"test-url-parse-query.js",

View file

@ -0,0 +1,149 @@
// deno-fmt-ignore-file
// deno-lint-ignore-file
// Copyright Joyent and Node contributors. All rights reserved. MIT license.
// Taken from Node 18.12.1
// This file is automatically generated by "node/_tools/setup.ts". Do not modify this file manually
'use strict';
const common = require('../common');
if (!common.hasIntl)
common.skip('missing Intl');
const assert = require('assert');
const url = require('url');
const myURL = new URL('http://user:pass@xn--lck1c3crb1723bpq4a.com/a?a=b#c');
assert.strictEqual(
url.format(myURL),
'http://user:pass@xn--lck1c3crb1723bpq4a.com/a?a=b#c'
);
assert.strictEqual(
url.format(myURL, {}),
'http://user:pass@xn--lck1c3crb1723bpq4a.com/a?a=b#c'
);
{
[true, 1, 'test', Infinity].forEach((value) => {
assert.throws(
() => url.format(myURL, value),
{
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
message: 'The "options" argument must be of type object.' +
common.invalidArgTypeHelper(value)
}
);
});
}
// Any falsy value other than undefined will be treated as false.
// Any truthy value will be treated as true.
assert.strictEqual(
url.format(myURL, { auth: false }),
'http://xn--lck1c3crb1723bpq4a.com/a?a=b#c'
);
assert.strictEqual(
url.format(myURL, { auth: '' }),
'http://xn--lck1c3crb1723bpq4a.com/a?a=b#c'
);
assert.strictEqual(
url.format(myURL, { auth: 0 }),
'http://xn--lck1c3crb1723bpq4a.com/a?a=b#c'
);
assert.strictEqual(
url.format(myURL, { auth: 1 }),
'http://user:pass@xn--lck1c3crb1723bpq4a.com/a?a=b#c'
);
assert.strictEqual(
url.format(myURL, { auth: {} }),
'http://user:pass@xn--lck1c3crb1723bpq4a.com/a?a=b#c'
);
assert.strictEqual(
url.format(myURL, { fragment: false }),
'http://user:pass@xn--lck1c3crb1723bpq4a.com/a?a=b'
);
assert.strictEqual(
url.format(myURL, { fragment: '' }),
'http://user:pass@xn--lck1c3crb1723bpq4a.com/a?a=b'
);
assert.strictEqual(
url.format(myURL, { fragment: 0 }),
'http://user:pass@xn--lck1c3crb1723bpq4a.com/a?a=b'
);
assert.strictEqual(
url.format(myURL, { fragment: 1 }),
'http://user:pass@xn--lck1c3crb1723bpq4a.com/a?a=b#c'
);
assert.strictEqual(
url.format(myURL, { fragment: {} }),
'http://user:pass@xn--lck1c3crb1723bpq4a.com/a?a=b#c'
);
assert.strictEqual(
url.format(myURL, { search: false }),
'http://user:pass@xn--lck1c3crb1723bpq4a.com/a#c'
);
assert.strictEqual(
url.format(myURL, { search: '' }),
'http://user:pass@xn--lck1c3crb1723bpq4a.com/a#c'
);
assert.strictEqual(
url.format(myURL, { search: 0 }),
'http://user:pass@xn--lck1c3crb1723bpq4a.com/a#c'
);
assert.strictEqual(
url.format(myURL, { search: 1 }),
'http://user:pass@xn--lck1c3crb1723bpq4a.com/a?a=b#c'
);
assert.strictEqual(
url.format(myURL, { search: {} }),
'http://user:pass@xn--lck1c3crb1723bpq4a.com/a?a=b#c'
);
assert.strictEqual(
url.format(myURL, { unicode: true }),
'http://user:pass@理容ナカムラ.com/a?a=b#c'
);
assert.strictEqual(
url.format(myURL, { unicode: 1 }),
'http://user:pass@理容ナカムラ.com/a?a=b#c'
);
assert.strictEqual(
url.format(myURL, { unicode: {} }),
'http://user:pass@理容ナカムラ.com/a?a=b#c'
);
assert.strictEqual(
url.format(myURL, { unicode: false }),
'http://user:pass@xn--lck1c3crb1723bpq4a.com/a?a=b#c'
);
assert.strictEqual(
url.format(myURL, { unicode: 0 }),
'http://user:pass@xn--lck1c3crb1723bpq4a.com/a?a=b#c'
);
assert.strictEqual(
url.format(new URL('http://user:pass@xn--0zwm56d.com:8080/path'), { unicode: true }),
'http://user:pass@测试.com:8080/path'
);

View file

@ -939,14 +939,11 @@ export function format(
["Object", "string"],
urlObject,
);
} else if (!(urlObject instanceof Url)) {
if (urlObject instanceof URL) {
return formatWhatwg(urlObject, options);
}
return Url.prototype.format.call(urlObject);
} else if (urlObject instanceof URL) {
return formatWhatwg(urlObject, options);
}
return (urlObject as Url).format();
return Url.prototype.format.call(urlObject);
}
/**
@ -1002,10 +999,9 @@ function formatWhatwg(
}
ret += "@";
}
// TODO(wafuwfu13): Support unicode option
// ret += options.unicode ?
// domainToUnicode(urlObject.host) : urlObject.host;
ret += urlObject.host;
ret += options.unicode
? domainToUnicode(urlObject.hostname)
: urlObject.hostname;
if (urlObject.port) {
ret += `:${urlObject.port}`;
}

View file

@ -3,7 +3,7 @@
NOTE: This file should not be manually edited. Please edit 'cli/tests/node_compat/config.json' and run 'tools/node_compat/setup.ts' instead.
Total: 2935
Total: 2934
- [abort/test-abort-backtrace.js](https://github.com/nodejs/node/tree/v18.12.1/test/abort/test-abort-backtrace.js)
- [abort/test-abort-fatal-error.js](https://github.com/nodejs/node/tree/v18.12.1/test/abort/test-abort-fatal-error.js)
@ -2372,7 +2372,6 @@ Total: 2935
- [parallel/test-ttywrap-stack.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-ttywrap-stack.js)
- [parallel/test-unhandled-exception-rethrow-error.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-unhandled-exception-rethrow-error.js)
- [parallel/test-unicode-node-options.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-unicode-node-options.js)
- [parallel/test-url-format-whatwg.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-url-format-whatwg.js)
- [parallel/test-url-null-char.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-url-null-char.js)
- [parallel/test-url-parse-format.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-url-parse-format.js)
- [parallel/test-utf8-scripts.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-utf8-scripts.js)