web.url.can-parse.js 1.0 KB

123456789101112131415161718192021222324252627282930
  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. // `URL.canParse` method
  15. // https://url.spec.whatwg.org/#dom-url-canparse
  16. $({ target: 'URL', stat: true, forced: !THROWS_WITHOUT_ARGUMENTS }, {
  17. canParse: function canParse(url) {
  18. var length = validateArgumentsLength(arguments.length, 1);
  19. var urlString = toString(url);
  20. var base = length < 2 || arguments[1] === undefined ? undefined : toString(arguments[1]);
  21. try {
  22. return !!new URL(urlString, base);
  23. } catch (error) {
  24. return false;
  25. }
  26. }
  27. });