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