Skip to content

Commit

Permalink
add a fix for a NodeJS 19.9.0 URL.canParse bug
Browse files Browse the repository at this point in the history
  • Loading branch information
zloirock committed Apr 11, 2023
1 parent e0de0dc commit 1029176
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## Changelog
##### Unreleased
- Added a fix for a NodeJS 19.9.0 `URL.canParse` [bug](https://github.com/nodejs/node/issues/47505)
- Compat data improvements:
- [`JSON.parse` source text access proposal](https://github.com/tc39/proposal-json-parse-with-source) features marked as [supported](https://chromestatus.com/feature/5121582673428480) from V8 ~ Chrome 114
- [`ArrayBuffer.prototype.transfer` and friends proposal](https://github.com/tc39/proposal-arraybuffer-transfer) features marked as [supported](https://chromestatus.com/feature/5073244152922112) from V8 ~ Chrome 114
Expand Down
9 changes: 8 additions & 1 deletion packages/core-js/modules/web.url.can-parse.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
var $ = require('../internals/export');
var getBuiltIn = require('../internals/get-built-in');
var fails = require('../internals/fails');
var validateArgumentsLength = require('../internals/validate-arguments-length');
var toString = require('../internals/to-string');
var USE_NATIVE_URL = require('../internals/url-constructor-detection');

var URL = getBuiltIn('URL');

// https://github.com/nodejs/node/issues/47505
var THROWS_WITHOUT_ARGUMENTS = USE_NATIVE_URL && fails(function () {
URL.canParse();
});

// `URL.canParse` method
// https://url.spec.whatwg.org/#dom-url-canparse
$({ target: 'URL', stat: true }, {
$({ target: 'URL', stat: true, forced: !THROWS_WITHOUT_ARGUMENTS }, {
canParse: function canParse(url) {
var length = validateArgumentsLength(arguments.length, 1);
var urlString = toString(url);
Expand Down
6 changes: 5 additions & 1 deletion tests/compat/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -1937,7 +1937,11 @@ GLOBAL.tests = {
'web.timers': TIMERS,
'web.url.constructor': URL_AND_URL_SEARCH_PARAMS_SUPPORT,
'web.url.can-parse': [URL_AND_URL_SEARCH_PARAMS_SUPPORT, function () {
return URL.canParse;
try {
URL.canParse();
} catch (error) {
return URL.canParse;
}
}],
'web.url.to-json': [URL_AND_URL_SEARCH_PARAMS_SUPPORT, function () {
return URL.prototype.toJSON;
Expand Down
4 changes: 3 additions & 1 deletion tests/unit-global/web.url.can-parse.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { NODE } from '../helpers/constants';

QUnit.test('URL.canParse', assert => {
const { canParse } = URL;

assert.isFunction(canParse);
assert.arity(canParse, 1);
assert.name(canParse, 'canParse');
assert.looksNative(canParse);
if (!NODE) assert.looksNative(canParse);

assert.false(canParse(undefined), 'undefined');
assert.false(canParse(undefined, undefined), 'undefined, undefined');
Expand Down

0 comments on commit 1029176

Please sign in to comment.