You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
1.8 KiB

4 years ago
  1. "use strict";
  2. var constants = require("./constants");
  3. var formatUrl = require("./format");
  4. var getOptions = require("./options");
  5. var objUtils = require("./util/object");
  6. var parseUrl = require("./parse");
  7. var relateUrl = require("./relate");
  8. function RelateUrl(from, options)
  9. {
  10. this.options = getOptions(options,
  11. {
  12. defaultPorts: {ftp:21, http:80, https:443},
  13. directoryIndexes: ["index.html"],
  14. ignore_www: false,
  15. output: RelateUrl.SHORTEST,
  16. rejectedSchemes: ["data","javascript","mailto"],
  17. removeAuth: false,
  18. removeDirectoryIndexes: true,
  19. removeEmptyQueries: false,
  20. removeRootTrailingSlash: true,
  21. schemeRelative: true,
  22. site: undefined,
  23. slashesDenoteHost: true
  24. });
  25. this.from = parseUrl.from(from, this.options, null);
  26. }
  27. /*
  28. Usage: instance=new RelateUrl(); instance.relate();
  29. */
  30. RelateUrl.prototype.relate = function(from, to, options)
  31. {
  32. // relate(to,options)
  33. if ( objUtils.isPlainObject(to) )
  34. {
  35. options = to;
  36. to = from;
  37. from = null;
  38. }
  39. // relate(to)
  40. else if (!to)
  41. {
  42. to = from;
  43. from = null;
  44. }
  45. options = getOptions(options, this.options);
  46. from = from || options.site;
  47. from = parseUrl.from(from, options, this.from);
  48. if (!from || !from.href)
  49. {
  50. throw new Error("from value not defined.");
  51. }
  52. else if (from.extra.hrefInfo.minimumPathOnly)
  53. {
  54. throw new Error("from value supplied is not absolute: "+from.href);
  55. }
  56. to = parseUrl.to(to, options);
  57. if (to.valid===false) return to.href;
  58. to = relateUrl(from, to, options);
  59. to = formatUrl(to, options);
  60. return to;
  61. }
  62. /*
  63. Usage: RelateUrl.relate();
  64. */
  65. RelateUrl.relate = function(from, to, options)
  66. {
  67. return new RelateUrl().relate(from, to, options);
  68. }
  69. // Make constants accessible from API
  70. objUtils.shallowMerge(RelateUrl, constants);
  71. module.exports = RelateUrl;