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