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