This repository has been archived by the owner on Aug 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathuri.js
1600 lines (1370 loc) · 43.2 KB
/
uri.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @license
* Copyright The Closure Library Authors.
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @fileoverview Class for parsing and formatting URIs.
*
* This package is deprecated in favour of the Closure URL package (goog.url)
* when manipulating URIs for use by a browser. This package uses regular
* expressions to parse a potential URI which can fall out of sync with how a
* browser will actually interpret the URI. See
* `goog.uri.utils.setUrlPackageSupportLoggingHandler` for one way to identify
* URIs that should instead be parsed using the URL package.
*
* Use goog.Uri(string) to parse a URI string. Use goog.Uri.create(...) to
* create a new instance of the goog.Uri object from Uri parts.
*
* e.g: <code>var myUri = new goog.Uri(window.location);</code>
*
* Implements RFC 3986 for parsing/formatting URIs.
* http://www.ietf.org/rfc/rfc3986.txt
*
* Some changes have been made to the interface (more like .NETs), though the
* internal representation is now of un-encoded parts, this will change the
* behavior slightly.
*/
goog.provide('goog.Uri');
goog.provide('goog.Uri.QueryData');
goog.require('goog.array');
goog.require('goog.asserts');
goog.require('goog.collections.maps');
goog.require('goog.string');
goog.require('goog.structs');
goog.require('goog.uri.utils');
goog.require('goog.uri.utils.ComponentIndex');
goog.require('goog.uri.utils.StandardQueryParam');
/**
* This class contains setters and getters for the parts of the URI.
* The <code>getXyz</code>/<code>setXyz</code> methods return the decoded part
* -- so<code>goog.Uri.parse('/foo%20bar').getPath()</code> will return the
* decoded path, <code>/foo bar</code>.
*
* Reserved characters (see RFC 3986 section 2.2) can be present in
* their percent-encoded form in scheme, domain, and path URI components and
* will not be auto-decoded. For example:
* <code>goog.Uri.parse('rel%61tive/path%2fto/resource').getPath()</code> will
* return <code>relative/path%2fto/resource</code>.
*
* The constructor accepts an optional unparsed, raw URI string. The parser
* is relaxed, so special characters that aren't escaped but don't cause
* ambiguities will not cause parse failures.
*
* All setters return <code>this</code> and so may be chained, a la
* <code>goog.Uri.parse('/foo').setFragment('part').toString()</code>.
*
* @param {*=} opt_uri Optional string URI to parse
* (use goog.Uri.create() to create a URI from parts), or if
* a goog.Uri is passed, a clone is created.
* @param {boolean=} opt_ignoreCase If true, #getParameterValue will ignore
* the case of the parameter name.
*
* @throws URIError If opt_uri is provided and URI is malformed (that is,
* if decodeURIComponent fails on any of the URI components).
* @constructor
* @struct
*/
goog.Uri = function(opt_uri, opt_ignoreCase) {
'use strict';
/**
* Scheme such as "http".
* @private {string}
*/
this.scheme_ = '';
/**
* User credentials in the form "username:password".
* @private {string}
*/
this.userInfo_ = '';
/**
* Domain part, e.g. "www.google.com".
* @private {string}
*/
this.domain_ = '';
/**
* Port, e.g. 8080.
* @private {?number}
*/
this.port_ = null;
/**
* Path, e.g. "/tests/img.png".
* @private {string}
*/
this.path_ = '';
/**
* The fragment without the #.
* @private {string}
*/
this.fragment_ = '';
/**
* Whether or not this Uri should be treated as Read Only.
* @private {boolean}
*/
this.isReadOnly_ = false;
/**
* Whether or not to ignore case when comparing query params.
* @private {boolean}
*/
this.ignoreCase_ = false;
/**
* Object representing query data.
* @private {!goog.Uri.QueryData}
*/
this.queryData_;
// Parse in the uri string
var m;
if (opt_uri instanceof goog.Uri) {
this.ignoreCase_ = (opt_ignoreCase !== undefined) ? opt_ignoreCase :
opt_uri.getIgnoreCase();
this.setScheme(opt_uri.getScheme());
this.setUserInfo(opt_uri.getUserInfo());
this.setDomain(opt_uri.getDomain());
this.setPort(opt_uri.getPort());
this.setPath(opt_uri.getPath());
this.setQueryData(opt_uri.getQueryData().clone());
this.setFragment(opt_uri.getFragment());
} else if (opt_uri && (m = goog.uri.utils.split(String(opt_uri)))) {
this.ignoreCase_ = !!opt_ignoreCase;
// Set the parts -- decoding as we do so.
// COMPATIBILITY NOTE - In IE, unmatched fields may be empty strings,
// whereas in other browsers they will be undefined.
this.setScheme(m[goog.uri.utils.ComponentIndex.SCHEME] || '', true);
this.setUserInfo(m[goog.uri.utils.ComponentIndex.USER_INFO] || '', true);
this.setDomain(m[goog.uri.utils.ComponentIndex.DOMAIN] || '', true);
this.setPort(m[goog.uri.utils.ComponentIndex.PORT]);
this.setPath(m[goog.uri.utils.ComponentIndex.PATH] || '', true);
this.setQueryData(m[goog.uri.utils.ComponentIndex.QUERY_DATA] || '', true);
this.setFragment(m[goog.uri.utils.ComponentIndex.FRAGMENT] || '', true);
} else {
this.ignoreCase_ = !!opt_ignoreCase;
this.queryData_ = new goog.Uri.QueryData(null, this.ignoreCase_);
}
};
/**
* Parameter name added to stop caching.
* @type {string}
*/
goog.Uri.RANDOM_PARAM = goog.uri.utils.StandardQueryParam.RANDOM;
/**
* @return {string} The string form of the url.
* @override
*/
goog.Uri.prototype.toString = function() {
'use strict';
var out = [];
var scheme = this.getScheme();
if (scheme) {
out.push(
goog.Uri.encodeSpecialChars_(
scheme, goog.Uri.reDisallowedInSchemeOrUserInfo_, true),
':');
}
var domain = this.getDomain();
if (domain || scheme == 'file') {
out.push('//');
var userInfo = this.getUserInfo();
if (userInfo) {
out.push(
goog.Uri.encodeSpecialChars_(
userInfo, goog.Uri.reDisallowedInSchemeOrUserInfo_, true),
'@');
}
out.push(goog.Uri.removeDoubleEncoding_(goog.string.urlEncode(domain)));
var port = this.getPort();
if (port != null) {
out.push(':', String(port));
}
}
var path = this.getPath();
if (path) {
if (this.hasDomain() && path.charAt(0) != '/') {
out.push('/');
}
out.push(goog.Uri.encodeSpecialChars_(
path,
path.charAt(0) == '/' ? goog.Uri.reDisallowedInAbsolutePath_ :
goog.Uri.reDisallowedInRelativePath_,
true));
}
var query = this.getEncodedQuery();
if (query) {
out.push('?', query);
}
var fragment = this.getFragment();
if (fragment) {
out.push(
'#',
goog.Uri.encodeSpecialChars_(
fragment, goog.Uri.reDisallowedInFragment_));
}
return out.join('');
};
/**
* Resolves the given relative URI (a goog.Uri object), using the URI
* represented by this instance as the base URI.
*
* There are several kinds of relative URIs:<br>
* 1. foo - replaces the last part of the path, the whole query and fragment<br>
* 2. /foo - replaces the path, the query and fragment<br>
* 3. //foo - replaces everything from the domain on. foo is a domain name<br>
* 4. ?foo - replace the query and fragment<br>
* 5. #foo - replace the fragment only
*
* Additionally, if relative URI has a non-empty path, all ".." and "."
* segments will be resolved, as described in RFC 3986.
*
* @param {!goog.Uri} relativeUri The relative URI to resolve.
* @return {!goog.Uri} The resolved URI.
*/
goog.Uri.prototype.resolve = function(relativeUri) {
'use strict';
var absoluteUri = this.clone();
// we satisfy these conditions by looking for the first part of relativeUri
// that is not blank and applying defaults to the rest
var overridden = relativeUri.hasScheme();
if (overridden) {
absoluteUri.setScheme(relativeUri.getScheme());
} else {
overridden = relativeUri.hasUserInfo();
}
if (overridden) {
absoluteUri.setUserInfo(relativeUri.getUserInfo());
} else {
overridden = relativeUri.hasDomain();
}
if (overridden) {
absoluteUri.setDomain(relativeUri.getDomain());
} else {
overridden = relativeUri.hasPort();
}
var path = relativeUri.getPath();
if (overridden) {
absoluteUri.setPort(relativeUri.getPort());
} else {
overridden = relativeUri.hasPath();
if (overridden) {
// resolve path properly
if (path.charAt(0) != '/') {
// path is relative
if (this.hasDomain() && !this.hasPath()) {
// RFC 3986, section 5.2.3, case 1
path = '/' + path;
} else {
// RFC 3986, section 5.2.3, case 2
var lastSlashIndex = absoluteUri.getPath().lastIndexOf('/');
if (lastSlashIndex != -1) {
path = absoluteUri.getPath().slice(0, lastSlashIndex + 1) + path;
}
}
}
path = goog.Uri.removeDotSegments(path);
}
}
if (overridden) {
absoluteUri.setPath(path);
} else {
overridden = relativeUri.hasQuery();
}
if (overridden) {
absoluteUri.setQueryData(relativeUri.getQueryData().clone());
} else {
overridden = relativeUri.hasFragment();
}
if (overridden) {
absoluteUri.setFragment(relativeUri.getFragment());
}
return absoluteUri;
};
/**
* Clones the URI instance.
* @return {!goog.Uri} New instance of the URI object.
*/
goog.Uri.prototype.clone = function() {
'use strict';
return new goog.Uri(this);
};
/**
* @return {string} The encoded scheme/protocol for the URI.
*/
goog.Uri.prototype.getScheme = function() {
'use strict';
return this.scheme_;
};
/**
* Sets the scheme/protocol.
* @throws URIError If opt_decode is true and newScheme is malformed (that is,
* if decodeURIComponent fails).
* @param {string} newScheme New scheme value.
* @param {boolean=} opt_decode Optional param for whether to decode new value.
* @return {!goog.Uri} Reference to this URI object.
*/
goog.Uri.prototype.setScheme = function(newScheme, opt_decode) {
'use strict';
this.enforceReadOnly();
this.scheme_ =
opt_decode ? goog.Uri.decodeOrEmpty_(newScheme, true) : newScheme;
// remove an : at the end of the scheme so somebody can pass in
// window.location.protocol
if (this.scheme_) {
this.scheme_ = this.scheme_.replace(/:$/, '');
}
return this;
};
/**
* @return {boolean} Whether the scheme has been set.
*/
goog.Uri.prototype.hasScheme = function() {
'use strict';
return !!this.scheme_;
};
/**
* @return {string} The decoded user info.
*/
goog.Uri.prototype.getUserInfo = function() {
'use strict';
return this.userInfo_;
};
/**
* Sets the userInfo.
* @throws URIError If opt_decode is true and newUserInfo is malformed (that is,
* if decodeURIComponent fails).
* @param {string} newUserInfo New userInfo value.
* @param {boolean=} opt_decode Optional param for whether to decode new value.
* @return {!goog.Uri} Reference to this URI object.
*/
goog.Uri.prototype.setUserInfo = function(newUserInfo, opt_decode) {
'use strict';
this.enforceReadOnly();
this.userInfo_ =
opt_decode ? goog.Uri.decodeOrEmpty_(newUserInfo) : newUserInfo;
return this;
};
/**
* @return {boolean} Whether the user info has been set.
*/
goog.Uri.prototype.hasUserInfo = function() {
'use strict';
return !!this.userInfo_;
};
/**
* @return {string} The decoded domain.
*/
goog.Uri.prototype.getDomain = function() {
'use strict';
return this.domain_;
};
/**
* Sets the domain.
* @throws URIError If opt_decode is true and newDomain is malformed (that is,
* if decodeURIComponent fails).
* @param {string} newDomain New domain value.
* @param {boolean=} opt_decode Optional param for whether to decode new value.
* @return {!goog.Uri} Reference to this URI object.
*/
goog.Uri.prototype.setDomain = function(newDomain, opt_decode) {
'use strict';
this.enforceReadOnly();
this.domain_ =
opt_decode ? goog.Uri.decodeOrEmpty_(newDomain, true) : newDomain;
return this;
};
/**
* @return {boolean} Whether the domain has been set.
*/
goog.Uri.prototype.hasDomain = function() {
'use strict';
return !!this.domain_;
};
/**
* @return {?number} The port number.
*/
goog.Uri.prototype.getPort = function() {
'use strict';
return this.port_;
};
/**
* Sets the port number.
* @param {*} newPort Port number. Will be explicitly casted to a number.
* @return {!goog.Uri} Reference to this URI object.
*/
goog.Uri.prototype.setPort = function(newPort) {
'use strict';
this.enforceReadOnly();
if (newPort) {
newPort = Number(newPort);
if (isNaN(newPort) || newPort < 0) {
throw new Error('Bad port number ' + newPort);
}
this.port_ = newPort;
} else {
this.port_ = null;
}
return this;
};
/**
* @return {boolean} Whether the port has been set.
*/
goog.Uri.prototype.hasPort = function() {
'use strict';
return this.port_ != null;
};
/**
* @return {string} The decoded path.
*/
goog.Uri.prototype.getPath = function() {
'use strict';
return this.path_;
};
/**
* Sets the path.
* @throws URIError If opt_decode is true and newPath is malformed (that is,
* if decodeURIComponent fails).
* @param {string} newPath New path value.
* @param {boolean=} opt_decode Optional param for whether to decode new value.
* @return {!goog.Uri} Reference to this URI object.
*/
goog.Uri.prototype.setPath = function(newPath, opt_decode) {
'use strict';
this.enforceReadOnly();
this.path_ = opt_decode ? goog.Uri.decodeOrEmpty_(newPath, true) : newPath;
return this;
};
/**
* @return {boolean} Whether the path has been set.
*/
goog.Uri.prototype.hasPath = function() {
'use strict';
return !!this.path_;
};
/**
* @return {boolean} Whether the query string has been set.
*/
goog.Uri.prototype.hasQuery = function() {
'use strict';
return this.queryData_.toString() !== '';
};
/**
* Sets the query data.
* @param {goog.Uri.QueryData|string|undefined} queryData QueryData object.
* @param {boolean=} opt_decode Optional param for whether to decode new value.
* Applies only if queryData is a string.
* @return {!goog.Uri} Reference to this URI object.
*/
goog.Uri.prototype.setQueryData = function(queryData, opt_decode) {
'use strict';
this.enforceReadOnly();
if (queryData instanceof goog.Uri.QueryData) {
this.queryData_ = queryData;
this.queryData_.setIgnoreCase(this.ignoreCase_);
} else {
if (!opt_decode) {
// QueryData accepts encoded query string, so encode it if
// opt_decode flag is not true.
queryData = goog.Uri.encodeSpecialChars_(
queryData, goog.Uri.reDisallowedInQuery_);
}
this.queryData_ = new goog.Uri.QueryData(queryData, this.ignoreCase_);
}
return this;
};
/**
* Sets the URI query.
* @param {string} newQuery New query value.
* @param {boolean=} opt_decode Optional param for whether to decode new value.
* @return {!goog.Uri} Reference to this URI object.
*/
goog.Uri.prototype.setQuery = function(newQuery, opt_decode) {
'use strict';
return this.setQueryData(newQuery, opt_decode);
};
/**
* @return {string} The encoded URI query, not including the ?.
*/
goog.Uri.prototype.getEncodedQuery = function() {
'use strict';
return this.queryData_.toString();
};
/**
* @return {string} The decoded URI query, not including the ?.
*/
goog.Uri.prototype.getDecodedQuery = function() {
'use strict';
return this.queryData_.toDecodedString();
};
/**
* Returns the query data.
* @return {!goog.Uri.QueryData} QueryData object.
*/
goog.Uri.prototype.getQueryData = function() {
'use strict';
return this.queryData_;
};
/**
* @return {string} The encoded URI query, not including the ?.
*
* Warning: This method, unlike other getter methods, returns encoded
* value, instead of decoded one.
*/
goog.Uri.prototype.getQuery = function() {
'use strict';
return this.getEncodedQuery();
};
/**
* Sets the value of the named query parameters, clearing previous values for
* that key.
*
* @param {string} key The parameter to set.
* @param {*} value The new value. Value does not need to be encoded.
* @return {!goog.Uri} Reference to this URI object.
*/
goog.Uri.prototype.setParameterValue = function(key, value) {
'use strict';
this.enforceReadOnly();
this.queryData_.set(key, value);
return this;
};
/**
* Sets the values of the named query parameters, clearing previous values for
* that key. Not new values will currently be moved to the end of the query
* string.
*
* So, <code>goog.Uri.parse('foo?a=b&c=d&e=f').setParameterValues('c', ['new'])
* </code> yields <tt>foo?a=b&e=f&c=new</tt>.</p>
*
* @param {string} key The parameter to set.
* @param {*} values The new values. If values is a single
* string then it will be treated as the sole value. Values do not need to
* be encoded.
* @return {!goog.Uri} Reference to this URI object.
*/
goog.Uri.prototype.setParameterValues = function(key, values) {
'use strict';
this.enforceReadOnly();
if (!Array.isArray(values)) {
values = [String(values)];
}
this.queryData_.setValues(key, values);
return this;
};
/**
* Returns the value<b>s</b> for a given cgi parameter as a list of decoded
* query parameter values.
* @param {string} name The parameter to get values for.
* @return {!Array<?>} The values for a given cgi parameter as a list of
* decoded query parameter values.
*/
goog.Uri.prototype.getParameterValues = function(name) {
'use strict';
return this.queryData_.getValues(name);
};
/**
* Returns the first value for a given cgi parameter or undefined if the given
* parameter name does not appear in the query string.
* @param {string} paramName Unescaped parameter name.
* @return {string|undefined} The first value for a given cgi parameter or
* undefined if the given parameter name does not appear in the query
* string.
*/
goog.Uri.prototype.getParameterValue = function(paramName) {
'use strict';
return /** @type {string|undefined} */ (this.queryData_.get(paramName));
};
/**
* @return {string} The URI fragment, not including the #.
*/
goog.Uri.prototype.getFragment = function() {
'use strict';
return this.fragment_;
};
/**
* Sets the URI fragment.
* @throws URIError If opt_decode is true and newFragment is malformed (that is,
* if decodeURIComponent fails).
* @param {string} newFragment New fragment value.
* @param {boolean=} opt_decode Optional param for whether to decode new value.
* @return {!goog.Uri} Reference to this URI object.
*/
goog.Uri.prototype.setFragment = function(newFragment, opt_decode) {
'use strict';
this.enforceReadOnly();
this.fragment_ =
opt_decode ? goog.Uri.decodeOrEmpty_(newFragment) : newFragment;
return this;
};
/**
* @return {boolean} Whether the URI has a fragment set.
*/
goog.Uri.prototype.hasFragment = function() {
'use strict';
return !!this.fragment_;
};
/**
* Returns true if this has the same domain as that of uri2.
* @param {!goog.Uri} uri2 The URI object to compare to.
* @return {boolean} true if same domain; false otherwise.
*/
goog.Uri.prototype.hasSameDomainAs = function(uri2) {
'use strict';
return ((!this.hasDomain() && !uri2.hasDomain()) ||
this.getDomain() == uri2.getDomain()) &&
((!this.hasPort() && !uri2.hasPort()) ||
this.getPort() == uri2.getPort());
};
/**
* Adds a random parameter to the Uri.
* @return {!goog.Uri} Reference to this Uri object.
*/
goog.Uri.prototype.makeUnique = function() {
'use strict';
this.enforceReadOnly();
this.setParameterValue(goog.Uri.RANDOM_PARAM, goog.string.getRandomString());
return this;
};
/**
* Removes the named query parameter.
*
* @param {string} key The parameter to remove.
* @return {!goog.Uri} Reference to this URI object.
*/
goog.Uri.prototype.removeParameter = function(key) {
'use strict';
this.enforceReadOnly();
this.queryData_.remove(key);
return this;
};
/**
* Sets whether Uri is read only. If this goog.Uri is read-only,
* enforceReadOnly_ will be called at the start of any function that may modify
* this Uri.
* @param {boolean} isReadOnly whether this goog.Uri should be read only.
* @return {!goog.Uri} Reference to this Uri object.
*/
goog.Uri.prototype.setReadOnly = function(isReadOnly) {
'use strict';
this.isReadOnly_ = isReadOnly;
return this;
};
/**
* @return {boolean} Whether the URI is read only.
*/
goog.Uri.prototype.isReadOnly = function() {
'use strict';
return this.isReadOnly_;
};
/**
* Checks if this Uri has been marked as read only, and if so, throws an error.
* This should be called whenever any modifying function is called.
*/
goog.Uri.prototype.enforceReadOnly = function() {
'use strict';
if (this.isReadOnly_) {
throw new Error('Tried to modify a read-only Uri');
}
};
/**
* Sets whether to ignore case.
* NOTE: If there are already key/value pairs in the QueryData, and
* ignoreCase_ is set to false, the keys will all be lower-cased.
* @param {boolean} ignoreCase whether this goog.Uri should ignore case.
* @return {!goog.Uri} Reference to this Uri object.
*/
goog.Uri.prototype.setIgnoreCase = function(ignoreCase) {
'use strict';
this.ignoreCase_ = ignoreCase;
if (this.queryData_) {
this.queryData_.setIgnoreCase(ignoreCase);
}
return this;
};
/**
* @return {boolean} Whether to ignore case.
*/
goog.Uri.prototype.getIgnoreCase = function() {
'use strict';
return this.ignoreCase_;
};
//==============================================================================
// Static members
//==============================================================================
/**
* Creates a uri from the string form. Basically an alias of new goog.Uri().
* If a Uri object is passed to parse then it will return a clone of the object.
*
* @throws URIError If parsing the URI is malformed. The passed URI components
* should all be parseable by decodeURIComponent.
* @param {*} uri Raw URI string or instance of Uri
* object.
* @param {boolean=} opt_ignoreCase Whether to ignore the case of parameter
* names in #getParameterValue.
* @return {!goog.Uri} The new URI object.
*/
goog.Uri.parse = function(uri, opt_ignoreCase) {
'use strict';
return uri instanceof goog.Uri ? uri.clone() :
new goog.Uri(uri, opt_ignoreCase);
};
/**
* Creates a new goog.Uri object from unencoded parts.
*
* @param {?string=} opt_scheme Scheme/protocol or full URI to parse.
* @param {?string=} opt_userInfo username:password.
* @param {?string=} opt_domain www.google.com.
* @param {?number=} opt_port 9830.
* @param {?string=} opt_path /some/path/to/a/file.html.
* @param {string|goog.Uri.QueryData=} opt_query a=1&b=2.
* @param {?string=} opt_fragment The fragment without the #.
* @param {boolean=} opt_ignoreCase Whether to ignore parameter name case in
* #getParameterValue.
*
* @return {!goog.Uri} The new URI object.
*/
goog.Uri.create = function(
opt_scheme, opt_userInfo, opt_domain, opt_port, opt_path, opt_query,
opt_fragment, opt_ignoreCase) {
'use strict';
var uri = new goog.Uri(null, opt_ignoreCase);
// Only set the parts if they are defined and not empty strings.
opt_scheme && uri.setScheme(opt_scheme);
opt_userInfo && uri.setUserInfo(opt_userInfo);
opt_domain && uri.setDomain(opt_domain);
opt_port && uri.setPort(opt_port);
opt_path && uri.setPath(opt_path);
opt_query && uri.setQueryData(opt_query);
opt_fragment && uri.setFragment(opt_fragment);
return uri;
};
/**
* Resolves a relative Uri against a base Uri, accepting both strings and
* Uri objects.
*
* @param {*} base Base Uri.
* @param {*} rel Relative Uri.
* @return {!goog.Uri} Resolved uri.
*/
goog.Uri.resolve = function(base, rel) {
'use strict';
if (!(base instanceof goog.Uri)) {
base = goog.Uri.parse(base);
}
if (!(rel instanceof goog.Uri)) {
rel = goog.Uri.parse(rel);
}
return base.resolve(rel);
};
/**
* Removes dot segments in given path component, as described in
* RFC 3986, section 5.2.4.
*
* @param {string} path A non-empty path component.
* @return {string} Path component with removed dot segments.
*/
goog.Uri.removeDotSegments = function(path) {
'use strict';
if (path == '..' || path == '.') {
return '';
} else if (
!goog.string.contains(path, './') && !goog.string.contains(path, '/.')) {
// This optimization detects uris which do not contain dot-segments,
// and as a consequence do not require any processing.
return path;
} else {
var leadingSlash = goog.string.startsWith(path, '/');
var segments = path.split('/');
var out = [];
for (var pos = 0; pos < segments.length;) {
var segment = segments[pos++];
if (segment == '.') {
if (leadingSlash && pos == segments.length) {
out.push('');
}
} else if (segment == '..') {
if (out.length > 1 || out.length == 1 && out[0] != '') {
out.pop();
}
if (leadingSlash && pos == segments.length) {
out.push('');
}
} else {
out.push(segment);
leadingSlash = true;
}
}
return out.join('/');
}
};
/**
* Decodes a value or returns the empty string if it isn't defined or empty.
* @throws URIError If decodeURIComponent fails to decode val.
* @param {string|undefined} val Value to decode.
* @param {boolean=} opt_preserveReserved If true, restricted characters will
* not be decoded.
* @return {string} Decoded value.
* @private
*/
goog.Uri.decodeOrEmpty_ = function(val, opt_preserveReserved) {
'use strict';
// Don't use UrlDecode() here because val is not a query parameter.
if (!val) {
return '';
}
// decodeURI has the same output for '%2f' and '%252f'. We double encode %25
// so that we can distinguish between the 2 inputs. This is later undone by
// removeDoubleEncoding_.
return opt_preserveReserved ? decodeURI(val.replace(/%25/g, '%2525')) :
decodeURIComponent(val);
};
/**
* If unescapedPart is non null, then escapes any characters in it that aren't
* valid characters in a url and also escapes any special characters that
* appear in extra.
*
* @param {*} unescapedPart The string to encode.
* @param {RegExp} extra A character set of characters in [\01-\177].
* @param {boolean=} opt_removeDoubleEncoding If true, remove double percent
* encoding.
* @return {?string} null iff unescapedPart == null.
* @private
*/
goog.Uri.encodeSpecialChars_ = function(
unescapedPart, extra, opt_removeDoubleEncoding) {
'use strict';
if (typeof unescapedPart === 'string') {
var encoded = encodeURI(unescapedPart).replace(extra, goog.Uri.encodeChar_);
if (opt_removeDoubleEncoding) {
// encodeURI double-escapes %XX sequences used to represent restricted
// characters in some URI components, remove the double escaping here.
encoded = goog.Uri.removeDoubleEncoding_(encoded);
}
return encoded;
}
return null;
};
/**
* Converts a character in [\01-\177] to its unicode character equivalent.
* @param {string} ch One character string.
* @return {string} Encoded string.
* @private
*/