web.url.can-parse.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536
  1. 'use strict';
  2. var $ = require('../internals/export');
  3. var getBuiltIn = require('../internals/get-built-in');
  4. var fails = require('../internals/fails');
  5. var validateArgumentsLength = require('../internals/validate-arguments-length');
  6. var toString = require('../internals/to-string');
  7. var USE_NATIVE_URL = require('../internals/url-constructor-detection');
  8. var URL = getBuiltIn('URL');
  9. // https://github.com/nodejs/node/issues/47505
  10. // https://github.com/denoland/deno/issues/18893
  11. var THROWS_WITHOUT_ARGUMENTS = USE_NATIVE_URL && fails(function () {
  12. URL.canParse();
  13. });
  14. // Bun ~ 1.0.30 bug
  15. // https://github.com/oven-sh/bun/issues/9250
  16. var WRONG_ARITY = fails(function () {
  17. return URL.canParse.length !== 1;
  18. });
  19. // `URL.canParse` method
  20. // https://url.spec.whatwg.org/#dom-url-canparse
  21. $({ target: 'URL', stat: true, forced: !THROWS_WITHOUT_ARGUMENTS || WRONG_ARITY }, {
  22. canParse: function canParse(url) {
  23. var length = validateArgumentsLength(arguments.length, 1);
  24. var urlString = toString(url);
  25. var base = length < 2 || arguments[1] === undefined ? undefined : toString(arguments[1]);
  26. try {
  27. return !!new URL(urlString, base);
  28. } catch (error) {
  29. return false;
  30. }
  31. }
  32. });