Debian Codegraph @

mixmark-io-domino/lib/URL.js

raw symbols SCIP
1 "use strict";
2 module.exports = URL;
3
4 function URL(url) {
5 if (!url) return Object.create(URL.prototype);
6 // Can't use String.trim() since it defines whitespace differently than HTML
7 this.url = url.replace(/^[ \t\n\r\f]+|[ \t\n\r\f]+$/g, "");
8
9 // See http://tools.ietf.org/html/rfc3986#appendix-B
10 // and https://url.spec.whatwg.org/#parsing
11 var match = URL.pattern.exec(this.url);
12 if (match) {
13 if (match[2]) this.scheme = match[2];
14 if (match[4]) {
15 // parse username/password
16 var userinfo = match[4].match(URL.userinfoPattern);
17 if (userinfo) {
18 this.username = userinfo[1];
19 this.password = userinfo[3];
20 match[4] = match[4].substring(userinfo[0].length);
21 }
22 if (match[4].match(URL.portPattern)) {
23 var pos = match[4].lastIndexOf(':');
24 this.host = match[4].substring(0, pos);
25 this.port = match[4].substring(pos+1);
26 }
27 else {
28 this.host = match[4];
29 }
30 }
31 if (match[5]) this.path = match[5];
32 if (match[6]) this.query = match[7];
33 if (match[8]) this.fragment = match[9];
34 }
35 }
36
37 URL.pattern = /^(([^:\/?#]+):)?(\/\/([^\/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?$/;
38 URL.userinfoPattern = /^([^@:]*)(:([^@]*))?@/;
39 URL.portPattern = /:\d+$/;
40 URL.authorityPattern = /^[^:\/?#]+:\/\//;
41 URL.hierarchyPattern = /^[^:\/?#]+:\//;
42
43 // Return a percentEncoded version of s.
44 // S should be a single-character string
45 // XXX: needs to do utf-8 encoding?
46 URL.percentEncode = function percentEncode(s) {
47 var c = s.charCodeAt(0);
48 if (c < 256) return "%" + c.toString(16);
49 else throw Error("can't percent-encode codepoints > 255 yet");
50 };
51
52 URL.prototype = {
53 constructor: URL,
54
55 // XXX: not sure if this is the precise definition of absolute
56 isAbsolute: function() { return !!this.scheme; },
57 isAuthorityBased: function() {
58 return URL.authorityPattern.test(this.url);
59 },
60 isHierarchical: function() {
61 return URL.hierarchyPattern.test(this.url);
62 },
63
64 toString: function() {
65 var s = "";
66 if (this.scheme !== undefined) s += this.scheme + ":";
67 if (this.isAbsolute()) {
68 s += '//';
69 if (this.username || this.password) {
70 s += this.username || '';
71 if (this.password) {
72 s += ':' + this.password;
73 }
74 s += '@';
75 }
76 if (this.host) {
77 s += this.host;
78 }
79 }
80 if (this.port !== undefined) s += ":" + this.port;
81 if (this.path !== undefined) s += this.path;
82 if (this.query !== undefined) s += "?" + this.query;
83 if (this.fragment !== undefined) s += "#" + this.fragment;
84 return s;
85 },
86
87 // See: http://tools.ietf.org/html/rfc3986#section-5.2
88 // and https://url.spec.whatwg.org/#constructors
89 resolve: function(relative) {
90 var base = this; // The base url we're resolving against
91 var r = new URL(relative); // The relative reference url to resolve
92 var t = new URL(); // The absolute target url we will return
93
94 if (r.scheme !== undefined) {
95 t.scheme = r.scheme;
96 t.username = r.username;
97 t.password = r.password;
98 t.host = r.host;
99 t.port = r.port;
100 t.path = remove_dot_segments(r.path);
101 t.query = r.query;
102 }
103 else {
104 t.scheme = base.scheme;
105 if (r.host !== undefined) {
106 t.username = r.username;
107 t.password = r.password;
108 t.host = r.host;
109 t.port = r.port;
110 t.path = remove_dot_segments(r.path);
111 t.query = r.query;
112 }
113 else {
114 t.username = base.username;
115 t.password = base.password;
116 t.host = base.host;
117 t.port = base.port;
118 if (!r.path) { // undefined or empty
119 t.path = base.path;
120 if (r.query !== undefined)
121 t.query = r.query;
122 else
123 t.query = base.query;
124 }
125 else {
126 if (r.path.charAt(0) === "/") {
127 t.path = remove_dot_segments(r.path);
128 }
129 else {
130 t.path = merge(base.path, r.path);
131 t.path = remove_dot_segments(t.path);
132 }
133 t.query = r.query;
134 }
135 }
136 }
137 t.fragment = r.fragment;
138
139 return t.toString();
140
141
142 function merge(basepath, refpath) {
143 if (base.host !== undefined && !base.path)
144 return "/" + refpath;
145
146 var lastslash = basepath.lastIndexOf("/");
147 if (lastslash === -1)
148 return refpath;
149 else
150 return basepath.substring(0, lastslash+1) + refpath;
151 }
152
153 function remove_dot_segments(path) {
154 if (!path) return path; // For "" or undefined
155
156 var output = "";
157 while(path.length > 0) {
158 if (path === "." || path === "..") {
159 path = "";
160 break;
161 }
162
163 var twochars = path.substring(0,2);
164 var threechars = path.substring(0,3);
165 var fourchars = path.substring(0,4);
166 if (threechars === "../") {
167 path = path.substring(3);
168 }
169 else if (twochars === "./") {
170 path = path.substring(2);
171 }
172 else if (threechars === "/./") {
173 path = "/" + path.substring(3);
174 }
175 else if (twochars === "/." && path.length === 2) {
176 path = "/";
177 }
178 else if (fourchars === "/../" ||
179 (threechars === "/.." && path.length === 3)) {
180 path = "/" + path.substring(4);
181
182 output = output.replace(/\/?[^\/]*$/, "");
183 }
184 else {
185 var segment = path.match(/(\/?([^\/]*))/)[0];
186 output += segment;
187 path = path.substring(segment.length);
188 }
189 }
190
191 return output;
192 }
193 },
194 };