lib/goog/uri/uri.js

1// Copyright 2006 The Closure Library Authors. All Rights Reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS-IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15/**
16 * @fileoverview Class for parsing and formatting URIs.
17 *
18 * Use goog.Uri(string) to parse a URI string. Use goog.Uri.create(...) to
19 * create a new instance of the goog.Uri object from Uri parts.
20 *
21 * e.g: <code>var myUri = new goog.Uri(window.location);</code>
22 *
23 * Implements RFC 3986 for parsing/formatting URIs.
24 * http://www.ietf.org/rfc/rfc3986.txt
25 *
26 * Some changes have been made to the interface (more like .NETs), though the
27 * internal representation is now of un-encoded parts, this will change the
28 * behavior slightly.
29 *
30 */
31
32goog.provide('goog.Uri');
33goog.provide('goog.Uri.QueryData');
34
35goog.require('goog.array');
36goog.require('goog.string');
37goog.require('goog.structs');
38goog.require('goog.structs.Map');
39goog.require('goog.uri.utils');
40goog.require('goog.uri.utils.ComponentIndex');
41goog.require('goog.uri.utils.StandardQueryParam');
42
43
44
45/**
46 * This class contains setters and getters for the parts of the URI.
47 * The <code>getXyz</code>/<code>setXyz</code> methods return the decoded part
48 * -- so<code>goog.Uri.parse('/foo%20bar').getPath()</code> will return the
49 * decoded path, <code>/foo bar</code>.
50 *
51 * Reserved characters (see RFC 3986 section 2.2) can be present in
52 * their percent-encoded form in scheme, domain, and path URI components and
53 * will not be auto-decoded. For example:
54 * <code>goog.Uri.parse('rel%61tive/path%2fto/resource').getPath()</code> will
55 * return <code>relative/path%2fto/resource</code>.
56 *
57 * The constructor accepts an optional unparsed, raw URI string. The parser
58 * is relaxed, so special characters that aren't escaped but don't cause
59 * ambiguities will not cause parse failures.
60 *
61 * All setters return <code>this</code> and so may be chained, a la
62 * <code>goog.Uri.parse('/foo').setFragment('part').toString()</code>.
63 *
64 * @param {*=} opt_uri Optional string URI to parse
65 * (use goog.Uri.create() to create a URI from parts), or if
66 * a goog.Uri is passed, a clone is created.
67 * @param {boolean=} opt_ignoreCase If true, #getParameterValue will ignore
68 * the case of the parameter name.
69 *
70 * @throws URIError If opt_uri is provided and URI is malformed (that is,
71 * if decodeURIComponent fails on any of the URI components).
72 * @constructor
73 * @struct
74 */
75goog.Uri = function(opt_uri, opt_ignoreCase) {
76 /**
77 * Scheme such as "http".
78 * @private {string}
79 */
80 this.scheme_ = '';
81
82 /**
83 * User credentials in the form "username:password".
84 * @private {string}
85 */
86 this.userInfo_ = '';
87
88 /**
89 * Domain part, e.g. "www.google.com".
90 * @private {string}
91 */
92 this.domain_ = '';
93
94 /**
95 * Port, e.g. 8080.
96 * @private {?number}
97 */
98 this.port_ = null;
99
100 /**
101 * Path, e.g. "/tests/img.png".
102 * @private {string}
103 */
104 this.path_ = '';
105
106 /**
107 * The fragment without the #.
108 * @private {string}
109 */
110 this.fragment_ = '';
111
112 /**
113 * Whether or not this Uri should be treated as Read Only.
114 * @private {boolean}
115 */
116 this.isReadOnly_ = false;
117
118 /**
119 * Whether or not to ignore case when comparing query params.
120 * @private {boolean}
121 */
122 this.ignoreCase_ = false;
123
124 /**
125 * Object representing query data.
126 * @private {!goog.Uri.QueryData}
127 */
128 this.queryData_;
129
130 // Parse in the uri string
131 var m;
132 if (opt_uri instanceof goog.Uri) {
133 this.ignoreCase_ = goog.isDef(opt_ignoreCase) ?
134 opt_ignoreCase : opt_uri.getIgnoreCase();
135 this.setScheme(opt_uri.getScheme());
136 this.setUserInfo(opt_uri.getUserInfo());
137 this.setDomain(opt_uri.getDomain());
138 this.setPort(opt_uri.getPort());
139 this.setPath(opt_uri.getPath());
140 this.setQueryData(opt_uri.getQueryData().clone());
141 this.setFragment(opt_uri.getFragment());
142 } else if (opt_uri && (m = goog.uri.utils.split(String(opt_uri)))) {
143 this.ignoreCase_ = !!opt_ignoreCase;
144
145 // Set the parts -- decoding as we do so.
146 // COMPATABILITY NOTE - In IE, unmatched fields may be empty strings,
147 // whereas in other browsers they will be undefined.
148 this.setScheme(m[goog.uri.utils.ComponentIndex.SCHEME] || '', true);
149 this.setUserInfo(m[goog.uri.utils.ComponentIndex.USER_INFO] || '', true);
150 this.setDomain(m[goog.uri.utils.ComponentIndex.DOMAIN] || '', true);
151 this.setPort(m[goog.uri.utils.ComponentIndex.PORT]);
152 this.setPath(m[goog.uri.utils.ComponentIndex.PATH] || '', true);
153 this.setQueryData(m[goog.uri.utils.ComponentIndex.QUERY_DATA] || '', true);
154 this.setFragment(m[goog.uri.utils.ComponentIndex.FRAGMENT] || '', true);
155
156 } else {
157 this.ignoreCase_ = !!opt_ignoreCase;
158 this.queryData_ = new goog.Uri.QueryData(null, null, this.ignoreCase_);
159 }
160};
161
162
163/**
164 * If true, we preserve the type of query parameters set programmatically.
165 *
166 * This means that if you set a parameter to a boolean, and then call
167 * getParameterValue, you will get a boolean back.
168 *
169 * If false, we will coerce parameters to strings, just as they would
170 * appear in real URIs.
171 *
172 * TODO(nicksantos): Remove this once people have time to fix all tests.
173 *
174 * @type {boolean}
175 */
176goog.Uri.preserveParameterTypesCompatibilityFlag = false;
177
178
179/**
180 * Parameter name added to stop caching.
181 * @type {string}
182 */
183goog.Uri.RANDOM_PARAM = goog.uri.utils.StandardQueryParam.RANDOM;
184
185
186/**
187 * @return {string} The string form of the url.
188 * @override
189 */
190goog.Uri.prototype.toString = function() {
191 var out = [];
192
193 var scheme = this.getScheme();
194 if (scheme) {
195 out.push(goog.Uri.encodeSpecialChars_(
196 scheme, goog.Uri.reDisallowedInSchemeOrUserInfo_, true), ':');
197 }
198
199 var domain = this.getDomain();
200 if (domain || scheme == 'file') {
201 out.push('//');
202
203 var userInfo = this.getUserInfo();
204 if (userInfo) {
205 out.push(goog.Uri.encodeSpecialChars_(
206 userInfo, goog.Uri.reDisallowedInSchemeOrUserInfo_, true), '@');
207 }
208
209 out.push(goog.Uri.removeDoubleEncoding_(goog.string.urlEncode(domain)));
210
211 var port = this.getPort();
212 if (port != null) {
213 out.push(':', String(port));
214 }
215 }
216
217 var path = this.getPath();
218 if (path) {
219 if (this.hasDomain() && path.charAt(0) != '/') {
220 out.push('/');
221 }
222 out.push(goog.Uri.encodeSpecialChars_(
223 path,
224 path.charAt(0) == '/' ?
225 goog.Uri.reDisallowedInAbsolutePath_ :
226 goog.Uri.reDisallowedInRelativePath_,
227 true));
228 }
229
230 var query = this.getEncodedQuery();
231 if (query) {
232 out.push('?', query);
233 }
234
235 var fragment = this.getFragment();
236 if (fragment) {
237 out.push('#', goog.Uri.encodeSpecialChars_(
238 fragment, goog.Uri.reDisallowedInFragment_));
239 }
240 return out.join('');
241};
242
243
244/**
245 * Resolves the given relative URI (a goog.Uri object), using the URI
246 * represented by this instance as the base URI.
247 *
248 * There are several kinds of relative URIs:<br>
249 * 1. foo - replaces the last part of the path, the whole query and fragment<br>
250 * 2. /foo - replaces the the path, the query and fragment<br>
251 * 3. //foo - replaces everything from the domain on. foo is a domain name<br>
252 * 4. ?foo - replace the query and fragment<br>
253 * 5. #foo - replace the fragment only
254 *
255 * Additionally, if relative URI has a non-empty path, all ".." and "."
256 * segments will be resolved, as described in RFC 3986.
257 *
258 * @param {!goog.Uri} relativeUri The relative URI to resolve.
259 * @return {!goog.Uri} The resolved URI.
260 */
261goog.Uri.prototype.resolve = function(relativeUri) {
262
263 var absoluteUri = this.clone();
264
265 // we satisfy these conditions by looking for the first part of relativeUri
266 // that is not blank and applying defaults to the rest
267
268 var overridden = relativeUri.hasScheme();
269
270 if (overridden) {
271 absoluteUri.setScheme(relativeUri.getScheme());
272 } else {
273 overridden = relativeUri.hasUserInfo();
274 }
275
276 if (overridden) {
277 absoluteUri.setUserInfo(relativeUri.getUserInfo());
278 } else {
279 overridden = relativeUri.hasDomain();
280 }
281
282 if (overridden) {
283 absoluteUri.setDomain(relativeUri.getDomain());
284 } else {
285 overridden = relativeUri.hasPort();
286 }
287
288 var path = relativeUri.getPath();
289 if (overridden) {
290 absoluteUri.setPort(relativeUri.getPort());
291 } else {
292 overridden = relativeUri.hasPath();
293 if (overridden) {
294 // resolve path properly
295 if (path.charAt(0) != '/') {
296 // path is relative
297 if (this.hasDomain() && !this.hasPath()) {
298 // RFC 3986, section 5.2.3, case 1
299 path = '/' + path;
300 } else {
301 // RFC 3986, section 5.2.3, case 2
302 var lastSlashIndex = absoluteUri.getPath().lastIndexOf('/');
303 if (lastSlashIndex != -1) {
304 path = absoluteUri.getPath().substr(0, lastSlashIndex + 1) + path;
305 }
306 }
307 }
308 path = goog.Uri.removeDotSegments(path);
309 }
310 }
311
312 if (overridden) {
313 absoluteUri.setPath(path);
314 } else {
315 overridden = relativeUri.hasQuery();
316 }
317
318 if (overridden) {
319 absoluteUri.setQueryData(relativeUri.getDecodedQuery());
320 } else {
321 overridden = relativeUri.hasFragment();
322 }
323
324 if (overridden) {
325 absoluteUri.setFragment(relativeUri.getFragment());
326 }
327
328 return absoluteUri;
329};
330
331
332/**
333 * Clones the URI instance.
334 * @return {!goog.Uri} New instance of the URI object.
335 */
336goog.Uri.prototype.clone = function() {
337 return new goog.Uri(this);
338};
339
340
341/**
342 * @return {string} The encoded scheme/protocol for the URI.
343 */
344goog.Uri.prototype.getScheme = function() {
345 return this.scheme_;
346};
347
348
349/**
350 * Sets the scheme/protocol.
351 * @throws URIError If opt_decode is true and newScheme is malformed (that is,
352 * if decodeURIComponent fails).
353 * @param {string} newScheme New scheme value.
354 * @param {boolean=} opt_decode Optional param for whether to decode new value.
355 * @return {!goog.Uri} Reference to this URI object.
356 */
357goog.Uri.prototype.setScheme = function(newScheme, opt_decode) {
358 this.enforceReadOnly();
359 this.scheme_ = opt_decode ? goog.Uri.decodeOrEmpty_(newScheme, true) :
360 newScheme;
361
362 // remove an : at the end of the scheme so somebody can pass in
363 // window.location.protocol
364 if (this.scheme_) {
365 this.scheme_ = this.scheme_.replace(/:$/, '');
366 }
367 return this;
368};
369
370
371/**
372 * @return {boolean} Whether the scheme has been set.
373 */
374goog.Uri.prototype.hasScheme = function() {
375 return !!this.scheme_;
376};
377
378
379/**
380 * @return {string} The decoded user info.
381 */
382goog.Uri.prototype.getUserInfo = function() {
383 return this.userInfo_;
384};
385
386
387/**
388 * Sets the userInfo.
389 * @throws URIError If opt_decode is true and newUserInfo is malformed (that is,
390 * if decodeURIComponent fails).
391 * @param {string} newUserInfo New userInfo value.
392 * @param {boolean=} opt_decode Optional param for whether to decode new value.
393 * @return {!goog.Uri} Reference to this URI object.
394 */
395goog.Uri.prototype.setUserInfo = function(newUserInfo, opt_decode) {
396 this.enforceReadOnly();
397 this.userInfo_ = opt_decode ? goog.Uri.decodeOrEmpty_(newUserInfo) :
398 newUserInfo;
399 return this;
400};
401
402
403/**
404 * @return {boolean} Whether the user info has been set.
405 */
406goog.Uri.prototype.hasUserInfo = function() {
407 return !!this.userInfo_;
408};
409
410
411/**
412 * @return {string} The decoded domain.
413 */
414goog.Uri.prototype.getDomain = function() {
415 return this.domain_;
416};
417
418
419/**
420 * Sets the domain.
421 * @throws URIError If opt_decode is true and newDomain is malformed (that is,
422 * if decodeURIComponent fails).
423 * @param {string} newDomain New domain value.
424 * @param {boolean=} opt_decode Optional param for whether to decode new value.
425 * @return {!goog.Uri} Reference to this URI object.
426 */
427goog.Uri.prototype.setDomain = function(newDomain, opt_decode) {
428 this.enforceReadOnly();
429 this.domain_ = opt_decode ? goog.Uri.decodeOrEmpty_(newDomain, true) :
430 newDomain;
431 return this;
432};
433
434
435/**
436 * @return {boolean} Whether the domain has been set.
437 */
438goog.Uri.prototype.hasDomain = function() {
439 return !!this.domain_;
440};
441
442
443/**
444 * @return {?number} The port number.
445 */
446goog.Uri.prototype.getPort = function() {
447 return this.port_;
448};
449
450
451/**
452 * Sets the port number.
453 * @param {*} newPort Port number. Will be explicitly casted to a number.
454 * @return {!goog.Uri} Reference to this URI object.
455 */
456goog.Uri.prototype.setPort = function(newPort) {
457 this.enforceReadOnly();
458
459 if (newPort) {
460 newPort = Number(newPort);
461 if (isNaN(newPort) || newPort < 0) {
462 throw Error('Bad port number ' + newPort);
463 }
464 this.port_ = newPort;
465 } else {
466 this.port_ = null;
467 }
468
469 return this;
470};
471
472
473/**
474 * @return {boolean} Whether the port has been set.
475 */
476goog.Uri.prototype.hasPort = function() {
477 return this.port_ != null;
478};
479
480
481/**
482 * @return {string} The decoded path.
483 */
484goog.Uri.prototype.getPath = function() {
485 return this.path_;
486};
487
488
489/**
490 * Sets the path.
491 * @throws URIError If opt_decode is true and newPath is malformed (that is,
492 * if decodeURIComponent fails).
493 * @param {string} newPath New path value.
494 * @param {boolean=} opt_decode Optional param for whether to decode new value.
495 * @return {!goog.Uri} Reference to this URI object.
496 */
497goog.Uri.prototype.setPath = function(newPath, opt_decode) {
498 this.enforceReadOnly();
499 this.path_ = opt_decode ? goog.Uri.decodeOrEmpty_(newPath, true) : newPath;
500 return this;
501};
502
503
504/**
505 * @return {boolean} Whether the path has been set.
506 */
507goog.Uri.prototype.hasPath = function() {
508 return !!this.path_;
509};
510
511
512/**
513 * @return {boolean} Whether the query string has been set.
514 */
515goog.Uri.prototype.hasQuery = function() {
516 return this.queryData_.toString() !== '';
517};
518
519
520/**
521 * Sets the query data.
522 * @param {goog.Uri.QueryData|string|undefined} queryData QueryData object.
523 * @param {boolean=} opt_decode Optional param for whether to decode new value.
524 * Applies only if queryData is a string.
525 * @return {!goog.Uri} Reference to this URI object.
526 */
527goog.Uri.prototype.setQueryData = function(queryData, opt_decode) {
528 this.enforceReadOnly();
529
530 if (queryData instanceof goog.Uri.QueryData) {
531 this.queryData_ = queryData;
532 this.queryData_.setIgnoreCase(this.ignoreCase_);
533 } else {
534 if (!opt_decode) {
535 // QueryData accepts encoded query string, so encode it if
536 // opt_decode flag is not true.
537 queryData = goog.Uri.encodeSpecialChars_(queryData,
538 goog.Uri.reDisallowedInQuery_);
539 }
540 this.queryData_ = new goog.Uri.QueryData(queryData, null, this.ignoreCase_);
541 }
542
543 return this;
544};
545
546
547/**
548 * Sets the URI query.
549 * @param {string} newQuery New query value.
550 * @param {boolean=} opt_decode Optional param for whether to decode new value.
551 * @return {!goog.Uri} Reference to this URI object.
552 */
553goog.Uri.prototype.setQuery = function(newQuery, opt_decode) {
554 return this.setQueryData(newQuery, opt_decode);
555};
556
557
558/**
559 * @return {string} The encoded URI query, not including the ?.
560 */
561goog.Uri.prototype.getEncodedQuery = function() {
562 return this.queryData_.toString();
563};
564
565
566/**
567 * @return {string} The decoded URI query, not including the ?.
568 */
569goog.Uri.prototype.getDecodedQuery = function() {
570 return this.queryData_.toDecodedString();
571};
572
573
574/**
575 * Returns the query data.
576 * @return {!goog.Uri.QueryData} QueryData object.
577 */
578goog.Uri.prototype.getQueryData = function() {
579 return this.queryData_;
580};
581
582
583/**
584 * @return {string} The encoded URI query, not including the ?.
585 *
586 * Warning: This method, unlike other getter methods, returns encoded
587 * value, instead of decoded one.
588 */
589goog.Uri.prototype.getQuery = function() {
590 return this.getEncodedQuery();
591};
592
593
594/**
595 * Sets the value of the named query parameters, clearing previous values for
596 * that key.
597 *
598 * @param {string} key The parameter to set.
599 * @param {*} value The new value.
600 * @return {!goog.Uri} Reference to this URI object.
601 */
602goog.Uri.prototype.setParameterValue = function(key, value) {
603 this.enforceReadOnly();
604 this.queryData_.set(key, value);
605 return this;
606};
607
608
609/**
610 * Sets the values of the named query parameters, clearing previous values for
611 * that key. Not new values will currently be moved to the end of the query
612 * string.
613 *
614 * So, <code>goog.Uri.parse('foo?a=b&c=d&e=f').setParameterValues('c', ['new'])
615 * </code> yields <tt>foo?a=b&e=f&c=new</tt>.</p>
616 *
617 * @param {string} key The parameter to set.
618 * @param {*} values The new values. If values is a single
619 * string then it will be treated as the sole value.
620 * @return {!goog.Uri} Reference to this URI object.
621 */
622goog.Uri.prototype.setParameterValues = function(key, values) {
623 this.enforceReadOnly();
624
625 if (!goog.isArray(values)) {
626 values = [String(values)];
627 }
628
629 this.queryData_.setValues(key, values);
630
631 return this;
632};
633
634
635/**
636 * Returns the value<b>s</b> for a given cgi parameter as a list of decoded
637 * query parameter values.
638 * @param {string} name The parameter to get values for.
639 * @return {!Array<?>} The values for a given cgi parameter as a list of
640 * decoded query parameter values.
641 */
642goog.Uri.prototype.getParameterValues = function(name) {
643 return this.queryData_.getValues(name);
644};
645
646
647/**
648 * Returns the first value for a given cgi parameter or undefined if the given
649 * parameter name does not appear in the query string.
650 * @param {string} paramName Unescaped parameter name.
651 * @return {string|undefined} The first value for a given cgi parameter or
652 * undefined if the given parameter name does not appear in the query
653 * string.
654 */
655goog.Uri.prototype.getParameterValue = function(paramName) {
656 // NOTE(nicksantos): This type-cast is a lie when
657 // preserveParameterTypesCompatibilityFlag is set to true.
658 // But this should only be set to true in tests.
659 return /** @type {string|undefined} */ (this.queryData_.get(paramName));
660};
661
662
663/**
664 * @return {string} The URI fragment, not including the #.
665 */
666goog.Uri.prototype.getFragment = function() {
667 return this.fragment_;
668};
669
670
671/**
672 * Sets the URI fragment.
673 * @throws URIError If opt_decode is true and newFragment is malformed (that is,
674 * if decodeURIComponent fails).
675 * @param {string} newFragment New fragment value.
676 * @param {boolean=} opt_decode Optional param for whether to decode new value.
677 * @return {!goog.Uri} Reference to this URI object.
678 */
679goog.Uri.prototype.setFragment = function(newFragment, opt_decode) {
680 this.enforceReadOnly();
681 this.fragment_ = opt_decode ? goog.Uri.decodeOrEmpty_(newFragment) :
682 newFragment;
683 return this;
684};
685
686
687/**
688 * @return {boolean} Whether the URI has a fragment set.
689 */
690goog.Uri.prototype.hasFragment = function() {
691 return !!this.fragment_;
692};
693
694
695/**
696 * Returns true if this has the same domain as that of uri2.
697 * @param {!goog.Uri} uri2 The URI object to compare to.
698 * @return {boolean} true if same domain; false otherwise.
699 */
700goog.Uri.prototype.hasSameDomainAs = function(uri2) {
701 return ((!this.hasDomain() && !uri2.hasDomain()) ||
702 this.getDomain() == uri2.getDomain()) &&
703 ((!this.hasPort() && !uri2.hasPort()) ||
704 this.getPort() == uri2.getPort());
705};
706
707
708/**
709 * Adds a random parameter to the Uri.
710 * @return {!goog.Uri} Reference to this Uri object.
711 */
712goog.Uri.prototype.makeUnique = function() {
713 this.enforceReadOnly();
714 this.setParameterValue(goog.Uri.RANDOM_PARAM, goog.string.getRandomString());
715
716 return this;
717};
718
719
720/**
721 * Removes the named query parameter.
722 *
723 * @param {string} key The parameter to remove.
724 * @return {!goog.Uri} Reference to this URI object.
725 */
726goog.Uri.prototype.removeParameter = function(key) {
727 this.enforceReadOnly();
728 this.queryData_.remove(key);
729 return this;
730};
731
732
733/**
734 * Sets whether Uri is read only. If this goog.Uri is read-only,
735 * enforceReadOnly_ will be called at the start of any function that may modify
736 * this Uri.
737 * @param {boolean} isReadOnly whether this goog.Uri should be read only.
738 * @return {!goog.Uri} Reference to this Uri object.
739 */
740goog.Uri.prototype.setReadOnly = function(isReadOnly) {
741 this.isReadOnly_ = isReadOnly;
742 return this;
743};
744
745
746/**
747 * @return {boolean} Whether the URI is read only.
748 */
749goog.Uri.prototype.isReadOnly = function() {
750 return this.isReadOnly_;
751};
752
753
754/**
755 * Checks if this Uri has been marked as read only, and if so, throws an error.
756 * This should be called whenever any modifying function is called.
757 */
758goog.Uri.prototype.enforceReadOnly = function() {
759 if (this.isReadOnly_) {
760 throw Error('Tried to modify a read-only Uri');
761 }
762};
763
764
765/**
766 * Sets whether to ignore case.
767 * NOTE: If there are already key/value pairs in the QueryData, and
768 * ignoreCase_ is set to false, the keys will all be lower-cased.
769 * @param {boolean} ignoreCase whether this goog.Uri should ignore case.
770 * @return {!goog.Uri} Reference to this Uri object.
771 */
772goog.Uri.prototype.setIgnoreCase = function(ignoreCase) {
773 this.ignoreCase_ = ignoreCase;
774 if (this.queryData_) {
775 this.queryData_.setIgnoreCase(ignoreCase);
776 }
777 return this;
778};
779
780
781/**
782 * @return {boolean} Whether to ignore case.
783 */
784goog.Uri.prototype.getIgnoreCase = function() {
785 return this.ignoreCase_;
786};
787
788
789//==============================================================================
790// Static members
791//==============================================================================
792
793
794/**
795 * Creates a uri from the string form. Basically an alias of new goog.Uri().
796 * If a Uri object is passed to parse then it will return a clone of the object.
797 *
798 * @throws URIError If parsing the URI is malformed. The passed URI components
799 * should all be parseable by decodeURIComponent.
800 * @param {*} uri Raw URI string or instance of Uri
801 * object.
802 * @param {boolean=} opt_ignoreCase Whether to ignore the case of parameter
803 * names in #getParameterValue.
804 * @return {!goog.Uri} The new URI object.
805 */
806goog.Uri.parse = function(uri, opt_ignoreCase) {
807 return uri instanceof goog.Uri ?
808 uri.clone() : new goog.Uri(uri, opt_ignoreCase);
809};
810
811
812/**
813 * Creates a new goog.Uri object from unencoded parts.
814 *
815 * @param {?string=} opt_scheme Scheme/protocol or full URI to parse.
816 * @param {?string=} opt_userInfo username:password.
817 * @param {?string=} opt_domain www.google.com.
818 * @param {?number=} opt_port 9830.
819 * @param {?string=} opt_path /some/path/to/a/file.html.
820 * @param {string|goog.Uri.QueryData=} opt_query a=1&b=2.
821 * @param {?string=} opt_fragment The fragment without the #.
822 * @param {boolean=} opt_ignoreCase Whether to ignore parameter name case in
823 * #getParameterValue.
824 *
825 * @return {!goog.Uri} The new URI object.
826 */
827goog.Uri.create = function(opt_scheme, opt_userInfo, opt_domain, opt_port,
828 opt_path, opt_query, opt_fragment, opt_ignoreCase) {
829
830 var uri = new goog.Uri(null, opt_ignoreCase);
831
832 // Only set the parts if they are defined and not empty strings.
833 opt_scheme && uri.setScheme(opt_scheme);
834 opt_userInfo && uri.setUserInfo(opt_userInfo);
835 opt_domain && uri.setDomain(opt_domain);
836 opt_port && uri.setPort(opt_port);
837 opt_path && uri.setPath(opt_path);
838 opt_query && uri.setQueryData(opt_query);
839 opt_fragment && uri.setFragment(opt_fragment);
840
841 return uri;
842};
843
844
845/**
846 * Resolves a relative Uri against a base Uri, accepting both strings and
847 * Uri objects.
848 *
849 * @param {*} base Base Uri.
850 * @param {*} rel Relative Uri.
851 * @return {!goog.Uri} Resolved uri.
852 */
853goog.Uri.resolve = function(base, rel) {
854 if (!(base instanceof goog.Uri)) {
855 base = goog.Uri.parse(base);
856 }
857
858 if (!(rel instanceof goog.Uri)) {
859 rel = goog.Uri.parse(rel);
860 }
861
862 return base.resolve(rel);
863};
864
865
866/**
867 * Removes dot segments in given path component, as described in
868 * RFC 3986, section 5.2.4.
869 *
870 * @param {string} path A non-empty path component.
871 * @return {string} Path component with removed dot segments.
872 */
873goog.Uri.removeDotSegments = function(path) {
874 if (path == '..' || path == '.') {
875 return '';
876
877 } else if (!goog.string.contains(path, './') &&
878 !goog.string.contains(path, '/.')) {
879 // This optimization detects uris which do not contain dot-segments,
880 // and as a consequence do not require any processing.
881 return path;
882
883 } else {
884 var leadingSlash = goog.string.startsWith(path, '/');
885 var segments = path.split('/');
886 var out = [];
887
888 for (var pos = 0; pos < segments.length; ) {
889 var segment = segments[pos++];
890
891 if (segment == '.') {
892 if (leadingSlash && pos == segments.length) {
893 out.push('');
894 }
895 } else if (segment == '..') {
896 if (out.length > 1 || out.length == 1 && out[0] != '') {
897 out.pop();
898 }
899 if (leadingSlash && pos == segments.length) {
900 out.push('');
901 }
902 } else {
903 out.push(segment);
904 leadingSlash = true;
905 }
906 }
907
908 return out.join('/');
909 }
910};
911
912
913/**
914 * Decodes a value or returns the empty string if it isn't defined or empty.
915 * @throws URIError If decodeURIComponent fails to decode val.
916 * @param {string|undefined} val Value to decode.
917 * @param {boolean=} opt_preserveReserved If true, restricted characters will
918 * not be decoded.
919 * @return {string} Decoded value.
920 * @private
921 */
922goog.Uri.decodeOrEmpty_ = function(val, opt_preserveReserved) {
923 // Don't use UrlDecode() here because val is not a query parameter.
924 if (!val) {
925 return '';
926 }
927
928 // decodeURI has the same output for '%2f' and '%252f'. We double encode %25
929 // so that we can distinguish between the 2 inputs. This is later undone by
930 // removeDoubleEncoding_.
931 return opt_preserveReserved ?
932 decodeURI(val.replace(/%25/g, '%2525')) : decodeURIComponent(val);
933};
934
935
936/**
937 * If unescapedPart is non null, then escapes any characters in it that aren't
938 * valid characters in a url and also escapes any special characters that
939 * appear in extra.
940 *
941 * @param {*} unescapedPart The string to encode.
942 * @param {RegExp} extra A character set of characters in [\01-\177].
943 * @param {boolean=} opt_removeDoubleEncoding If true, remove double percent
944 * encoding.
945 * @return {?string} null iff unescapedPart == null.
946 * @private
947 */
948goog.Uri.encodeSpecialChars_ = function(unescapedPart, extra,
949 opt_removeDoubleEncoding) {
950 if (goog.isString(unescapedPart)) {
951 var encoded = encodeURI(unescapedPart).
952 replace(extra, goog.Uri.encodeChar_);
953 if (opt_removeDoubleEncoding) {
954 // encodeURI double-escapes %XX sequences used to represent restricted
955 // characters in some URI components, remove the double escaping here.
956 encoded = goog.Uri.removeDoubleEncoding_(encoded);
957 }
958 return encoded;
959 }
960 return null;
961};
962
963
964/**
965 * Converts a character in [\01-\177] to its unicode character equivalent.
966 * @param {string} ch One character string.
967 * @return {string} Encoded string.
968 * @private
969 */
970goog.Uri.encodeChar_ = function(ch) {
971 var n = ch.charCodeAt(0);
972 return '%' + ((n >> 4) & 0xf).toString(16) + (n & 0xf).toString(16);
973};
974
975
976/**
977 * Removes double percent-encoding from a string.
978 * @param {string} doubleEncodedString String
979 * @return {string} String with double encoding removed.
980 * @private
981 */
982goog.Uri.removeDoubleEncoding_ = function(doubleEncodedString) {
983 return doubleEncodedString.replace(/%25([0-9a-fA-F]{2})/g, '%$1');
984};
985
986
987/**
988 * Regular expression for characters that are disallowed in the scheme or
989 * userInfo part of the URI.
990 * @type {RegExp}
991 * @private
992 */
993goog.Uri.reDisallowedInSchemeOrUserInfo_ = /[#\/\?@]/g;
994
995
996/**
997 * Regular expression for characters that are disallowed in a relative path.
998 * Colon is included due to RFC 3986 3.3.
999 * @type {RegExp}
1000 * @private
1001 */
1002goog.Uri.reDisallowedInRelativePath_ = /[\#\?:]/g;
1003
1004
1005/**
1006 * Regular expression for characters that are disallowed in an absolute path.
1007 * @type {RegExp}
1008 * @private
1009 */
1010goog.Uri.reDisallowedInAbsolutePath_ = /[\#\?]/g;
1011
1012
1013/**
1014 * Regular expression for characters that are disallowed in the query.
1015 * @type {RegExp}
1016 * @private
1017 */
1018goog.Uri.reDisallowedInQuery_ = /[\#\?@]/g;
1019
1020
1021/**
1022 * Regular expression for characters that are disallowed in the fragment.
1023 * @type {RegExp}
1024 * @private
1025 */
1026goog.Uri.reDisallowedInFragment_ = /#/g;
1027
1028
1029/**
1030 * Checks whether two URIs have the same domain.
1031 * @param {string} uri1String First URI string.
1032 * @param {string} uri2String Second URI string.
1033 * @return {boolean} true if the two URIs have the same domain; false otherwise.
1034 */
1035goog.Uri.haveSameDomain = function(uri1String, uri2String) {
1036 // Differs from goog.uri.utils.haveSameDomain, since this ignores scheme.
1037 // TODO(gboyer): Have this just call goog.uri.util.haveSameDomain.
1038 var pieces1 = goog.uri.utils.split(uri1String);
1039 var pieces2 = goog.uri.utils.split(uri2String);
1040 return pieces1[goog.uri.utils.ComponentIndex.DOMAIN] ==
1041 pieces2[goog.uri.utils.ComponentIndex.DOMAIN] &&
1042 pieces1[goog.uri.utils.ComponentIndex.PORT] ==
1043 pieces2[goog.uri.utils.ComponentIndex.PORT];
1044};
1045
1046
1047
1048/**
1049 * Class used to represent URI query parameters. It is essentially a hash of
1050 * name-value pairs, though a name can be present more than once.
1051 *
1052 * Has the same interface as the collections in goog.structs.
1053 *
1054 * @param {?string=} opt_query Optional encoded query string to parse into
1055 * the object.
1056 * @param {goog.Uri=} opt_uri Optional uri object that should have its
1057 * cache invalidated when this object updates. Deprecated -- this
1058 * is no longer required.
1059 * @param {boolean=} opt_ignoreCase If true, ignore the case of the parameter
1060 * name in #get.
1061 * @constructor
1062 * @struct
1063 * @final
1064 */
1065goog.Uri.QueryData = function(opt_query, opt_uri, opt_ignoreCase) {
1066 /**
1067 * The map containing name/value or name/array-of-values pairs.
1068 * May be null if it requires parsing from the query string.
1069 *
1070 * We need to use a Map because we cannot guarantee that the key names will
1071 * not be problematic for IE.
1072 *
1073 * @private {goog.structs.Map<string, !Array<*>>}
1074 */
1075 this.keyMap_ = null;
1076
1077 /**
1078 * The number of params, or null if it requires computing.
1079 * @private {?number}
1080 */
1081 this.count_ = null;
1082
1083 /**
1084 * Encoded query string, or null if it requires computing from the key map.
1085 * @private {?string}
1086 */
1087 this.encodedQuery_ = opt_query || null;
1088
1089 /**
1090 * If true, ignore the case of the parameter name in #get.
1091 * @private {boolean}
1092 */
1093 this.ignoreCase_ = !!opt_ignoreCase;
1094};
1095
1096
1097/**
1098 * If the underlying key map is not yet initialized, it parses the
1099 * query string and fills the map with parsed data.
1100 * @private
1101 */
1102goog.Uri.QueryData.prototype.ensureKeyMapInitialized_ = function() {
1103 if (!this.keyMap_) {
1104 this.keyMap_ = new goog.structs.Map();
1105 this.count_ = 0;
1106 if (this.encodedQuery_) {
1107 var self = this;
1108 goog.uri.utils.parseQueryData(this.encodedQuery_, function(name, value) {
1109 self.add(goog.string.urlDecode(name), value);
1110 });
1111 }
1112 }
1113};
1114
1115
1116/**
1117 * Creates a new query data instance from a map of names and values.
1118 *
1119 * @param {!goog.structs.Map<string, ?>|!Object} map Map of string parameter
1120 * names to parameter value. If parameter value is an array, it is
1121 * treated as if the key maps to each individual value in the
1122 * array.
1123 * @param {goog.Uri=} opt_uri URI object that should have its cache
1124 * invalidated when this object updates.
1125 * @param {boolean=} opt_ignoreCase If true, ignore the case of the parameter
1126 * name in #get.
1127 * @return {!goog.Uri.QueryData} The populated query data instance.
1128 */
1129goog.Uri.QueryData.createFromMap = function(map, opt_uri, opt_ignoreCase) {
1130 var keys = goog.structs.getKeys(map);
1131 if (typeof keys == 'undefined') {
1132 throw Error('Keys are undefined');
1133 }
1134
1135 var queryData = new goog.Uri.QueryData(null, null, opt_ignoreCase);
1136 var values = goog.structs.getValues(map);
1137 for (var i = 0; i < keys.length; i++) {
1138 var key = keys[i];
1139 var value = values[i];
1140 if (!goog.isArray(value)) {
1141 queryData.add(key, value);
1142 } else {
1143 queryData.setValues(key, value);
1144 }
1145 }
1146 return queryData;
1147};
1148
1149
1150/**
1151 * Creates a new query data instance from parallel arrays of parameter names
1152 * and values. Allows for duplicate parameter names. Throws an error if the
1153 * lengths of the arrays differ.
1154 *
1155 * @param {!Array<string>} keys Parameter names.
1156 * @param {!Array<?>} values Parameter values.
1157 * @param {goog.Uri=} opt_uri URI object that should have its cache
1158 * invalidated when this object updates.
1159 * @param {boolean=} opt_ignoreCase If true, ignore the case of the parameter
1160 * name in #get.
1161 * @return {!goog.Uri.QueryData} The populated query data instance.
1162 */
1163goog.Uri.QueryData.createFromKeysValues = function(
1164 keys, values, opt_uri, opt_ignoreCase) {
1165 if (keys.length != values.length) {
1166 throw Error('Mismatched lengths for keys/values');
1167 }
1168 var queryData = new goog.Uri.QueryData(null, null, opt_ignoreCase);
1169 for (var i = 0; i < keys.length; i++) {
1170 queryData.add(keys[i], values[i]);
1171 }
1172 return queryData;
1173};
1174
1175
1176/**
1177 * @return {?number} The number of parameters.
1178 */
1179goog.Uri.QueryData.prototype.getCount = function() {
1180 this.ensureKeyMapInitialized_();
1181 return this.count_;
1182};
1183
1184
1185/**
1186 * Adds a key value pair.
1187 * @param {string} key Name.
1188 * @param {*} value Value.
1189 * @return {!goog.Uri.QueryData} Instance of this object.
1190 */
1191goog.Uri.QueryData.prototype.add = function(key, value) {
1192 this.ensureKeyMapInitialized_();
1193 this.invalidateCache_();
1194
1195 key = this.getKeyName_(key);
1196 var values = this.keyMap_.get(key);
1197 if (!values) {
1198 this.keyMap_.set(key, (values = []));
1199 }
1200 values.push(value);
1201 this.count_++;
1202 return this;
1203};
1204
1205
1206/**
1207 * Removes all the params with the given key.
1208 * @param {string} key Name.
1209 * @return {boolean} Whether any parameter was removed.
1210 */
1211goog.Uri.QueryData.prototype.remove = function(key) {
1212 this.ensureKeyMapInitialized_();
1213
1214 key = this.getKeyName_(key);
1215 if (this.keyMap_.containsKey(key)) {
1216 this.invalidateCache_();
1217
1218 // Decrement parameter count.
1219 this.count_ -= this.keyMap_.get(key).length;
1220 return this.keyMap_.remove(key);
1221 }
1222 return false;
1223};
1224
1225
1226/**
1227 * Clears the parameters.
1228 */
1229goog.Uri.QueryData.prototype.clear = function() {
1230 this.invalidateCache_();
1231 this.keyMap_ = null;
1232 this.count_ = 0;
1233};
1234
1235
1236/**
1237 * @return {boolean} Whether we have any parameters.
1238 */
1239goog.Uri.QueryData.prototype.isEmpty = function() {
1240 this.ensureKeyMapInitialized_();
1241 return this.count_ == 0;
1242};
1243
1244
1245/**
1246 * Whether there is a parameter with the given name
1247 * @param {string} key The parameter name to check for.
1248 * @return {boolean} Whether there is a parameter with the given name.
1249 */
1250goog.Uri.QueryData.prototype.containsKey = function(key) {
1251 this.ensureKeyMapInitialized_();
1252 key = this.getKeyName_(key);
1253 return this.keyMap_.containsKey(key);
1254};
1255
1256
1257/**
1258 * Whether there is a parameter with the given value.
1259 * @param {*} value The value to check for.
1260 * @return {boolean} Whether there is a parameter with the given value.
1261 */
1262goog.Uri.QueryData.prototype.containsValue = function(value) {
1263 // NOTE(arv): This solution goes through all the params even if it was the
1264 // first param. We can get around this by not reusing code or by switching to
1265 // iterators.
1266 var vals = this.getValues();
1267 return goog.array.contains(vals, value);
1268};
1269
1270
1271/**
1272 * Returns all the keys of the parameters. If a key is used multiple times
1273 * it will be included multiple times in the returned array
1274 * @return {!Array<string>} All the keys of the parameters.
1275 */
1276goog.Uri.QueryData.prototype.getKeys = function() {
1277 this.ensureKeyMapInitialized_();
1278 // We need to get the values to know how many keys to add.
1279 var vals = /** @type {!Array<*>} */ (this.keyMap_.getValues());
1280 var keys = this.keyMap_.getKeys();
1281 var rv = [];
1282 for (var i = 0; i < keys.length; i++) {
1283 var val = vals[i];
1284 for (var j = 0; j < val.length; j++) {
1285 rv.push(keys[i]);
1286 }
1287 }
1288 return rv;
1289};
1290
1291
1292/**
1293 * Returns all the values of the parameters with the given name. If the query
1294 * data has no such key this will return an empty array. If no key is given
1295 * all values wil be returned.
1296 * @param {string=} opt_key The name of the parameter to get the values for.
1297 * @return {!Array<?>} All the values of the parameters with the given name.
1298 */
1299goog.Uri.QueryData.prototype.getValues = function(opt_key) {
1300 this.ensureKeyMapInitialized_();
1301 var rv = [];
1302 if (goog.isString(opt_key)) {
1303 if (this.containsKey(opt_key)) {
1304 rv = goog.array.concat(rv, this.keyMap_.get(this.getKeyName_(opt_key)));
1305 }
1306 } else {
1307 // Return all values.
1308 var values = this.keyMap_.getValues();
1309 for (var i = 0; i < values.length; i++) {
1310 rv = goog.array.concat(rv, values[i]);
1311 }
1312 }
1313 return rv;
1314};
1315
1316
1317/**
1318 * Sets a key value pair and removes all other keys with the same value.
1319 *
1320 * @param {string} key Name.
1321 * @param {*} value Value.
1322 * @return {!goog.Uri.QueryData} Instance of this object.
1323 */
1324goog.Uri.QueryData.prototype.set = function(key, value) {
1325 this.ensureKeyMapInitialized_();
1326 this.invalidateCache_();
1327
1328 // TODO(chrishenry): This could be better written as
1329 // this.remove(key), this.add(key, value), but that would reorder
1330 // the key (since the key is first removed and then added at the
1331 // end) and we would have to fix unit tests that depend on key
1332 // ordering.
1333 key = this.getKeyName_(key);
1334 if (this.containsKey(key)) {
1335 this.count_ -= this.keyMap_.get(key).length;
1336 }
1337 this.keyMap_.set(key, [value]);
1338 this.count_++;
1339 return this;
1340};
1341
1342
1343/**
1344 * Returns the first value associated with the key. If the query data has no
1345 * such key this will return undefined or the optional default.
1346 * @param {string} key The name of the parameter to get the value for.
1347 * @param {*=} opt_default The default value to return if the query data
1348 * has no such key.
1349 * @return {*} The first string value associated with the key, or opt_default
1350 * if there's no value.
1351 */
1352goog.Uri.QueryData.prototype.get = function(key, opt_default) {
1353 var values = key ? this.getValues(key) : [];
1354 if (goog.Uri.preserveParameterTypesCompatibilityFlag) {
1355 return values.length > 0 ? values[0] : opt_default;
1356 } else {
1357 return values.length > 0 ? String(values[0]) : opt_default;
1358 }
1359};
1360
1361
1362/**
1363 * Sets the values for a key. If the key already exists, this will
1364 * override all of the existing values that correspond to the key.
1365 * @param {string} key The key to set values for.
1366 * @param {!Array<?>} values The values to set.
1367 */
1368goog.Uri.QueryData.prototype.setValues = function(key, values) {
1369 this.remove(key);
1370
1371 if (values.length > 0) {
1372 this.invalidateCache_();
1373 this.keyMap_.set(this.getKeyName_(key), goog.array.clone(values));
1374 this.count_ += values.length;
1375 }
1376};
1377
1378
1379/**
1380 * @return {string} Encoded query string.
1381 * @override
1382 */
1383goog.Uri.QueryData.prototype.toString = function() {
1384 if (this.encodedQuery_) {
1385 return this.encodedQuery_;
1386 }
1387
1388 if (!this.keyMap_) {
1389 return '';
1390 }
1391
1392 var sb = [];
1393
1394 // In the past, we use this.getKeys() and this.getVals(), but that
1395 // generates a lot of allocations as compared to simply iterating
1396 // over the keys.
1397 var keys = this.keyMap_.getKeys();
1398 for (var i = 0; i < keys.length; i++) {
1399 var key = keys[i];
1400 var encodedKey = goog.string.urlEncode(key);
1401 var val = this.getValues(key);
1402 for (var j = 0; j < val.length; j++) {
1403 var param = encodedKey;
1404 // Ensure that null and undefined are encoded into the url as
1405 // literal strings.
1406 if (val[j] !== '') {
1407 param += '=' + goog.string.urlEncode(val[j]);
1408 }
1409 sb.push(param);
1410 }
1411 }
1412
1413 return this.encodedQuery_ = sb.join('&');
1414};
1415
1416
1417/**
1418 * @throws URIError If URI is malformed (that is, if decodeURIComponent fails on
1419 * any of the URI components).
1420 * @return {string} Decoded query string.
1421 */
1422goog.Uri.QueryData.prototype.toDecodedString = function() {
1423 return goog.Uri.decodeOrEmpty_(this.toString());
1424};
1425
1426
1427/**
1428 * Invalidate the cache.
1429 * @private
1430 */
1431goog.Uri.QueryData.prototype.invalidateCache_ = function() {
1432 this.encodedQuery_ = null;
1433};
1434
1435
1436/**
1437 * Removes all keys that are not in the provided list. (Modifies this object.)
1438 * @param {Array<string>} keys The desired keys.
1439 * @return {!goog.Uri.QueryData} a reference to this object.
1440 */
1441goog.Uri.QueryData.prototype.filterKeys = function(keys) {
1442 this.ensureKeyMapInitialized_();
1443 this.keyMap_.forEach(
1444 function(value, key) {
1445 if (!goog.array.contains(keys, key)) {
1446 this.remove(key);
1447 }
1448 }, this);
1449 return this;
1450};
1451
1452
1453/**
1454 * Clone the query data instance.
1455 * @return {!goog.Uri.QueryData} New instance of the QueryData object.
1456 */
1457goog.Uri.QueryData.prototype.clone = function() {
1458 var rv = new goog.Uri.QueryData();
1459 rv.encodedQuery_ = this.encodedQuery_;
1460 if (this.keyMap_) {
1461 rv.keyMap_ = this.keyMap_.clone();
1462 rv.count_ = this.count_;
1463 }
1464 return rv;
1465};
1466
1467
1468/**
1469 * Helper function to get the key name from a JavaScript object. Converts
1470 * the object to a string, and to lower case if necessary.
1471 * @private
1472 * @param {*} arg The object to get a key name from.
1473 * @return {string} valid key name which can be looked up in #keyMap_.
1474 */
1475goog.Uri.QueryData.prototype.getKeyName_ = function(arg) {
1476 var keyName = String(arg);
1477 if (this.ignoreCase_) {
1478 keyName = keyName.toLowerCase();
1479 }
1480 return keyName;
1481};
1482
1483
1484/**
1485 * Ignore case in parameter names.
1486 * NOTE: If there are already key/value pairs in the QueryData, and
1487 * ignoreCase_ is set to false, the keys will all be lower-cased.
1488 * @param {boolean} ignoreCase whether this goog.Uri should ignore case.
1489 */
1490goog.Uri.QueryData.prototype.setIgnoreCase = function(ignoreCase) {
1491 var resetKeys = ignoreCase && !this.ignoreCase_;
1492 if (resetKeys) {
1493 this.ensureKeyMapInitialized_();
1494 this.invalidateCache_();
1495 this.keyMap_.forEach(
1496 function(value, key) {
1497 var lowerCase = key.toLowerCase();
1498 if (key != lowerCase) {
1499 this.remove(key);
1500 this.setValues(lowerCase, value);
1501 }
1502 }, this);
1503 }
1504 this.ignoreCase_ = ignoreCase;
1505};
1506
1507
1508/**
1509 * Extends a query data object with another query data or map like object. This
1510 * operates 'in-place', it does not create a new QueryData object.
1511 *
1512 * @param {...(goog.Uri.QueryData|goog.structs.Map<?, ?>|Object)} var_args
1513 * The object from which key value pairs will be copied.
1514 */
1515goog.Uri.QueryData.prototype.extend = function(var_args) {
1516 for (var i = 0; i < arguments.length; i++) {
1517 var data = arguments[i];
1518 goog.structs.forEach(data,
1519 /** @this {goog.Uri.QueryData} */
1520 function(value, key) {
1521 this.add(key, value);
1522 }, this);
1523 }
1524};