absolutize.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. "use strict";
  2. var findRelation = require("./findRelation");
  3. var objUtils = require("../util/object");
  4. var pathUtils = require("../util/path");
  5. function absolutize(urlObj, siteUrlObj, options)
  6. {
  7. findRelation.upToPath(urlObj, siteUrlObj, options);
  8. // Fill in relative URLs
  9. if (urlObj.extra.relation.minimumScheme) urlObj.scheme = siteUrlObj.scheme;
  10. if (urlObj.extra.relation.minimumAuth) urlObj.auth = siteUrlObj.auth;
  11. if (urlObj.extra.relation.minimumHost) urlObj.host = objUtils.clone(siteUrlObj.host);
  12. if (urlObj.extra.relation.minimumPort) copyPort(urlObj, siteUrlObj);
  13. if (urlObj.extra.relation.minimumScheme) copyPath(urlObj, siteUrlObj);
  14. // Check remaining relativeness now that path has been copied and/or resolved
  15. findRelation.pathOn(urlObj, siteUrlObj, options);
  16. // Fill in relative URLs
  17. if (urlObj.extra.relation.minimumResource) copyResource(urlObj, siteUrlObj);
  18. if (urlObj.extra.relation.minimumQuery) urlObj.query = objUtils.clone(siteUrlObj.query);
  19. if (urlObj.extra.relation.minimumHash) urlObj.hash = siteUrlObj.hash;
  20. }
  21. /*
  22. Get an absolute path that's relative to site url.
  23. */
  24. function copyPath(urlObj, siteUrlObj)
  25. {
  26. if (urlObj.extra.relation.maximumHost || !urlObj.extra.hrefInfo.minimumResourceOnly)
  27. {
  28. var pathArray = urlObj.path.absolute.array;
  29. var pathString = "/";
  30. // If not erroneous URL
  31. if (pathArray)
  32. {
  33. // If is relative path
  34. if (urlObj.extra.hrefInfo.minimumPathOnly && urlObj.path.absolute.string.indexOf("/")!==0)
  35. {
  36. // Append path to site path
  37. pathArray = siteUrlObj.path.absolute.array.concat(pathArray);
  38. }
  39. pathArray = pathUtils.resolveDotSegments(pathArray);
  40. pathString += pathUtils.join(pathArray);
  41. }
  42. else
  43. {
  44. pathArray = [];
  45. }
  46. urlObj.path.absolute.array = pathArray;
  47. urlObj.path.absolute.string = pathString;
  48. }
  49. else
  50. {
  51. // Resource-, query- or hash-only or empty
  52. urlObj.path = objUtils.clone(siteUrlObj.path);
  53. }
  54. }
  55. function copyPort(urlObj, siteUrlObj)
  56. {
  57. urlObj.port = siteUrlObj.port;
  58. urlObj.extra.portIsDefault = siteUrlObj.extra.portIsDefault;
  59. }
  60. function copyResource(urlObj, siteUrlObj)
  61. {
  62. urlObj.resource = siteUrlObj.resource;
  63. urlObj.extra.resourceIsIndex = siteUrlObj.extra.resourceIsIndex;
  64. }
  65. module.exports = absolutize;