Debian Codegraph @

mixmark-io-domino/lib/URLUtils.js

raw symbols SCIP
1 "use strict";
2 var URL = require('./URL');
3
4 module.exports = URLUtils;
5
6 // Allow the `x == null` pattern. This is eslint's "null: 'ignore'" option,
7 // but jshint doesn't support this.
8 /* jshint eqeqeq: false */
9
10 // This is an abstract superclass for Location, HTMLAnchorElement and
11 // other types that have the standard complement of "URL decomposition
12 // IDL attributes". This is now standardized as URLUtils, see:
13 // https://url.spec.whatwg.org/#urlutils
14 // Subclasses must define a getter/setter on href.
15 // The getter and setter methods parse and rebuild the URL on each
16 // invocation; there is no attempt to cache the value and be more efficient
17 function URLUtils() {}
18 URLUtils.prototype = Object.create(Object.prototype, {
19
20 _url: { get: function() {
21 // XXX: this should do the "Reinitialize url" steps, and "null" should
22 // be a valid return value.
23 return new URL(this.href);
24 } },
25
26 protocol: {
27 get: function() {
28 var url = this._url;
29 if (url && url.scheme) return url.scheme + ":";
30 else return ":";
31 },
32 set: function(v) {
33 var output = this.href;
34 var url = new URL(output);
35 if (url.isAbsolute()) {
36 v = v.replace(/:+$/, "");
37 v = v.replace(/[^-+\.a-zA-Z0-9]/g, URL.percentEncode);
38 if (v.length > 0) {
39 url.scheme = v;
40 output = url.toString();
41 }
42 }
43 this.href = output;
44 },
45 },
46
47 host: {
48 get: function() {
49 var url = this._url;
50 if (url.isAbsolute() && url.isAuthorityBased())
51 return url.host + (url.port ? (":" + url.port) : "");
52 else
53 return "";
54 },
55 set: function(v) {
56 var output = this.href;
57 var url = new URL(output);
58 if (url.isAbsolute() && url.isAuthorityBased()) {
59 v = v.replace(/[^-+\._~!$&'()*,;:=a-zA-Z0-9]/g, URL.percentEncode);
60 if (v.length > 0) {
61 url.host = v;
62 delete url.port;
63 output = url.toString();
64 }
65 }
66 this.href = output;
67 },
68 },
69
70 hostname: {
71 get: function() {
72 var url = this._url;
73 if (url.isAbsolute() && url.isAuthorityBased())
74 return url.host;
75 else
76 return "";
77 },
78 set: function(v) {
79 var output = this.href;
80 var url = new URL(output);
81 if (url.isAbsolute() && url.isAuthorityBased()) {
82 v = v.replace(/^\/+/, "");
83 v = v.replace(/[^-+\._~!$&'()*,;:=a-zA-Z0-9]/g, URL.percentEncode);
84 if (v.length > 0) {
85 url.host = v;
86 output = url.toString();
87 }
88 }
89 this.href = output;
90 },
91 },
92
93 port: {
94 get: function() {
95 var url = this._url;
96 if (url.isAbsolute() && url.isAuthorityBased() && url.port!==undefined)
97 return url.port;
98 else
99 return "";
100 },
101 set: function(v) {
102 var output = this.href;
103 var url = new URL(output);
104 if (url.isAbsolute() && url.isAuthorityBased()) {
105 v = '' + v;
106 v = v.replace(/[^0-9].*$/, "");
107 v = v.replace(/^0+/, "");
108 if (v.length === 0) v = "0";
109 if (parseInt(v, 10) <= 65535) {
110 url.port = v;
111 output = url.toString();
112 }
113 }
114 this.href = output;
115 },
116 },
117
118 pathname: {
119 get: function() {
120 var url = this._url;
121 if (url.isAbsolute() && url.isHierarchical())
122 return url.path;
123 else
124 return "";
125 },
126 set: function(v) {
127 var output = this.href;
128 var url = new URL(output);
129 if (url.isAbsolute() && url.isHierarchical()) {
130 if (v.charAt(0) !== "/")
131 v = "/" + v;
132 v = v.replace(/[^-+\._~!$&'()*,;:=@\/a-zA-Z0-9]/g, URL.percentEncode);
133 url.path = v;
134 output = url.toString();
135 }
136 this.href = output;
137 },
138 },
139
140 search: {
141 get: function() {
142 var url = this._url;
143 if (url.isAbsolute() && url.isHierarchical() && url.query!==undefined)
144 return "?" + url.query;
145 else
146 return "";
147 },
148 set: function(v) {
149 var output = this.href;
150 var url = new URL(output);
151 if (url.isAbsolute() && url.isHierarchical()) {
152 if (v.charAt(0) === "?") v = v.substring(1);
153 v = v.replace(/[^-+\._~!$&'()*,;:=@\/?a-zA-Z0-9]/g, URL.percentEncode);
154 url.query = v;
155 output = url.toString();
156 }
157 this.href = output;
158 },
159 },
160
161 hash: {
162 get: function() {
163 var url = this._url;
164 if (url == null || url.fragment == null || url.fragment === '') {
165 return "";
166 } else {
167 return "#" + url.fragment;
168 }
169 },
170 set: function(v) {
171 var output = this.href;
172 var url = new URL(output);
173
174 if (v.charAt(0) === "#") v = v.substring(1);
175 v = v.replace(/[^-+\._~!$&'()*,;:=@\/?a-zA-Z0-9]/g, URL.percentEncode);
176 url.fragment = v;
177 output = url.toString();
178
179 this.href = output;
180 },
181 },
182
183 username: {
184 get: function() {
185 var url = this._url;
186 return url.username || '';
187 },
188 set: function(v) {
189 var output = this.href;
190 var url = new URL(output);
191 if (url.isAbsolute()) {
192 v = v.replace(/[\x00-\x1F\x7F-\uFFFF "#<>?`\/@\\:]/g, URL.percentEncode);
193 url.username = v;
194 output = url.toString();
195 }
196 this.href = output;
197 },
198 },
199
200 password: {
201 get: function() {
202 var url = this._url;
203 return url.password || '';
204 },
205 set: function(v) {
206 var output = this.href;
207 var url = new URL(output);
208 if (url.isAbsolute()) {
209 if (v==='') {
210 url.password = null;
211 } else {
212 v = v.replace(/[\x00-\x1F\x7F-\uFFFF "#<>?`\/@\\]/g, URL.percentEncode);
213 url.password = v;
214 }
215 output = url.toString();
216 }
217 this.href = output;
218 },
219 },
220
221 origin: { get: function() {
222 var url = this._url;
223 if (url == null) { return ''; }
224 var originForPort = function(defaultPort) {
225 var origin = [url.scheme, url.host, +url.port || defaultPort];
226 // XXX should be "unicode serialization"
227 return origin[0] + '://' + origin[1] +
228 (origin[2] === defaultPort ? '' : (':' + origin[2]));
229 };
230 switch (url.scheme) {
231 case 'ftp':
232 return originForPort(21);
233 case 'gopher':
234 return originForPort(70);
235 case 'http':
236 case 'ws':
237 return originForPort(80);
238 case 'https':
239 case 'wss':
240 return originForPort(443);
241 default:
242 // this is what chrome does
243 return url.scheme + '://';
244 }
245 } },
246
247 /*
248 searchParams: {
249 get: function() {
250 var url = this._url;
251 // XXX
252 },
253 set: function(v) {
254 var output = this.href;
255 var url = new URL(output);
256 // XXX
257 this.href = output;
258 },
259 },
260 */
261 });
262
263 URLUtils._inherit = function(proto) {
264 // copy getters/setters from URLUtils to o.
265 Object.getOwnPropertyNames(URLUtils.prototype).forEach(function(p) {
266 if (p==='constructor' || p==='href') { return; }
267 var desc = Object.getOwnPropertyDescriptor(URLUtils.prototype, p);
268 Object.defineProperty(proto, p, desc);
269 });
270 };