findRelation.js 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. "use strict";
  2. function findRelation_upToPath(urlObj, siteUrlObj, options)
  3. {
  4. // Path- or root-relative URL
  5. var pathOnly = urlObj.extra.hrefInfo.minimumPathOnly;
  6. // Matching scheme, scheme-relative or path-only
  7. var minimumScheme = (urlObj.scheme===siteUrlObj.scheme || !urlObj.scheme);
  8. // Matching auth, ignoring auth or path-only
  9. var minimumAuth = minimumScheme && (urlObj.auth===siteUrlObj.auth || options.removeAuth || pathOnly);
  10. // Matching host or path-only
  11. var www = options.ignore_www ? "stripped" : "full";
  12. var minimumHost = minimumAuth && (urlObj.host[www]===siteUrlObj.host[www] || pathOnly);
  13. // Matching port or path-only
  14. var minimumPort = minimumHost && (urlObj.port===siteUrlObj.port || pathOnly);
  15. urlObj.extra.relation.minimumScheme = minimumScheme;
  16. urlObj.extra.relation.minimumAuth = minimumAuth;
  17. urlObj.extra.relation.minimumHost = minimumHost;
  18. urlObj.extra.relation.minimumPort = minimumPort;
  19. urlObj.extra.relation.maximumScheme = !minimumScheme || minimumScheme && !minimumAuth;
  20. urlObj.extra.relation.maximumAuth = !minimumScheme || minimumScheme && !minimumHost;
  21. urlObj.extra.relation.maximumHost = !minimumScheme || minimumScheme && !minimumPort;
  22. }
  23. function findRelation_pathOn(urlObj, siteUrlObj, options)
  24. {
  25. var queryOnly = urlObj.extra.hrefInfo.minimumQueryOnly;
  26. var hashOnly = urlObj.extra.hrefInfo.minimumHashOnly;
  27. var empty = urlObj.extra.hrefInfo.empty; // not required, but self-documenting
  28. // From upToPath()
  29. var minimumPort = urlObj.extra.relation.minimumPort;
  30. var minimumScheme = urlObj.extra.relation.minimumScheme;
  31. // Matching port and path
  32. var minimumPath = minimumPort && urlObj.path.absolute.string===siteUrlObj.path.absolute.string;
  33. // Matching resource or query/hash-only or empty
  34. var matchingResource = (urlObj.resource===siteUrlObj.resource || !urlObj.resource && siteUrlObj.extra.resourceIsIndex) || (options.removeDirectoryIndexes && urlObj.extra.resourceIsIndex && !siteUrlObj.resource);
  35. var minimumResource = minimumPath && (matchingResource || queryOnly || hashOnly || empty);
  36. // Matching query or hash-only/empty
  37. var query = options.removeEmptyQueries ? "stripped" : "full";
  38. var urlQuery = urlObj.query.string[query];
  39. var siteUrlQuery = siteUrlObj.query.string[query];
  40. var minimumQuery = (minimumResource && !!urlQuery && urlQuery===siteUrlQuery) || ((hashOnly || empty) && !urlObj.extra.hrefInfo.separatorOnlyQuery);
  41. var minimumHash = minimumQuery && urlObj.hash===siteUrlObj.hash;
  42. urlObj.extra.relation.minimumPath = minimumPath;
  43. urlObj.extra.relation.minimumResource = minimumResource;
  44. urlObj.extra.relation.minimumQuery = minimumQuery;
  45. urlObj.extra.relation.minimumHash = minimumHash;
  46. urlObj.extra.relation.maximumPort = !minimumScheme || minimumScheme && !minimumPath;
  47. urlObj.extra.relation.maximumPath = !minimumScheme || minimumScheme && !minimumResource;
  48. urlObj.extra.relation.maximumResource = !minimumScheme || minimumScheme && !minimumQuery;
  49. urlObj.extra.relation.maximumQuery = !minimumScheme || minimumScheme && !minimumHash;
  50. urlObj.extra.relation.maximumHash = !minimumScheme || minimumScheme && !minimumHash; // there's nothing after hash, so it's the same as maximumQuery
  51. // Matching path and/or resource with existing but non-matching site query
  52. urlObj.extra.relation.overridesQuery = minimumPath && urlObj.extra.relation.maximumResource && !minimumQuery && !!siteUrlQuery;
  53. }
  54. module.exports =
  55. {
  56. pathOn: findRelation_pathOn,
  57. upToPath: findRelation_upToPath
  58. };