12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547 |
- goog.provide('goog.Uri');
- goog.provide('goog.Uri.QueryData');
- goog.require('goog.array');
- goog.require('goog.asserts');
- goog.require('goog.string');
- goog.require('goog.structs');
- goog.require('goog.structs.Map');
- goog.require('goog.uri.utils');
- goog.require('goog.uri.utils.ComponentIndex');
- goog.require('goog.uri.utils.StandardQueryParam');
- goog.Uri = function(opt_uri, opt_ignoreCase) {
-
- this.scheme_ = '';
-
- this.userInfo_ = '';
-
- this.domain_ = '';
-
- this.port_ = null;
-
- this.path_ = '';
-
- this.fragment_ = '';
-
- this.isReadOnly_ = false;
-
- this.ignoreCase_ = false;
-
- this.queryData_;
-
- var m;
- if (opt_uri instanceof goog.Uri) {
- this.ignoreCase_ =
- goog.isDef(opt_ignoreCase) ? opt_ignoreCase : opt_uri.getIgnoreCase();
- this.setScheme(opt_uri.getScheme());
- this.setUserInfo(opt_uri.getUserInfo());
- this.setDomain(opt_uri.getDomain());
- this.setPort(opt_uri.getPort());
- this.setPath(opt_uri.getPath());
- this.setQueryData(opt_uri.getQueryData().clone());
- this.setFragment(opt_uri.getFragment());
- } else if (opt_uri && (m = goog.uri.utils.split(String(opt_uri)))) {
- this.ignoreCase_ = !!opt_ignoreCase;
-
-
-
- this.setScheme(m[goog.uri.utils.ComponentIndex.SCHEME] || '', true);
- this.setUserInfo(m[goog.uri.utils.ComponentIndex.USER_INFO] || '', true);
- this.setDomain(m[goog.uri.utils.ComponentIndex.DOMAIN] || '', true);
- this.setPort(m[goog.uri.utils.ComponentIndex.PORT]);
- this.setPath(m[goog.uri.utils.ComponentIndex.PATH] || '', true);
- this.setQueryData(m[goog.uri.utils.ComponentIndex.QUERY_DATA] || '', true);
- this.setFragment(m[goog.uri.utils.ComponentIndex.FRAGMENT] || '', true);
- } else {
- this.ignoreCase_ = !!opt_ignoreCase;
- this.queryData_ = new goog.Uri.QueryData(null, null, this.ignoreCase_);
- }
- };
- goog.Uri.preserveParameterTypesCompatibilityFlag = false;
- goog.Uri.RANDOM_PARAM = goog.uri.utils.StandardQueryParam.RANDOM;
- goog.Uri.prototype.toString = function() {
- var out = [];
- var scheme = this.getScheme();
- if (scheme) {
- out.push(
- goog.Uri.encodeSpecialChars_(
- scheme, goog.Uri.reDisallowedInSchemeOrUserInfo_, true),
- ':');
- }
- var domain = this.getDomain();
- if (domain || scheme == 'file') {
- out.push('//');
- var userInfo = this.getUserInfo();
- if (userInfo) {
- out.push(
- goog.Uri.encodeSpecialChars_(
- userInfo, goog.Uri.reDisallowedInSchemeOrUserInfo_, true),
- '@');
- }
- out.push(goog.Uri.removeDoubleEncoding_(goog.string.urlEncode(domain)));
- var port = this.getPort();
- if (port != null) {
- out.push(':', String(port));
- }
- }
- var path = this.getPath();
- if (path) {
- if (this.hasDomain() && path.charAt(0) != '/') {
- out.push('/');
- }
- out.push(
- goog.Uri.encodeSpecialChars_(
- path, path.charAt(0) == '/' ? goog.Uri.reDisallowedInAbsolutePath_ :
- goog.Uri.reDisallowedInRelativePath_,
- true));
- }
- var query = this.getEncodedQuery();
- if (query) {
- out.push('?', query);
- }
- var fragment = this.getFragment();
- if (fragment) {
- out.push(
- '#', goog.Uri.encodeSpecialChars_(
- fragment, goog.Uri.reDisallowedInFragment_));
- }
- return out.join('');
- };
- goog.Uri.prototype.resolve = function(relativeUri) {
- var absoluteUri = this.clone();
-
-
- var overridden = relativeUri.hasScheme();
- if (overridden) {
- absoluteUri.setScheme(relativeUri.getScheme());
- } else {
- overridden = relativeUri.hasUserInfo();
- }
- if (overridden) {
- absoluteUri.setUserInfo(relativeUri.getUserInfo());
- } else {
- overridden = relativeUri.hasDomain();
- }
- if (overridden) {
- absoluteUri.setDomain(relativeUri.getDomain());
- } else {
- overridden = relativeUri.hasPort();
- }
- var path = relativeUri.getPath();
- if (overridden) {
- absoluteUri.setPort(relativeUri.getPort());
- } else {
- overridden = relativeUri.hasPath();
- if (overridden) {
-
- if (path.charAt(0) != '/') {
-
- if (this.hasDomain() && !this.hasPath()) {
-
- path = '/' + path;
- } else {
-
- var lastSlashIndex = absoluteUri.getPath().lastIndexOf('/');
- if (lastSlashIndex != -1) {
- path = absoluteUri.getPath().substr(0, lastSlashIndex + 1) + path;
- }
- }
- }
- path = goog.Uri.removeDotSegments(path);
- }
- }
- if (overridden) {
- absoluteUri.setPath(path);
- } else {
- overridden = relativeUri.hasQuery();
- }
- if (overridden) {
- absoluteUri.setQueryData(relativeUri.getQueryData().clone());
- } else {
- overridden = relativeUri.hasFragment();
- }
- if (overridden) {
- absoluteUri.setFragment(relativeUri.getFragment());
- }
- return absoluteUri;
- };
- goog.Uri.prototype.clone = function() {
- return new goog.Uri(this);
- };
- goog.Uri.prototype.getScheme = function() {
- return this.scheme_;
- };
- goog.Uri.prototype.setScheme = function(newScheme, opt_decode) {
- this.enforceReadOnly();
- this.scheme_ =
- opt_decode ? goog.Uri.decodeOrEmpty_(newScheme, true) : newScheme;
-
-
- if (this.scheme_) {
- this.scheme_ = this.scheme_.replace(/:$/, '');
- }
- return this;
- };
- goog.Uri.prototype.hasScheme = function() {
- return !!this.scheme_;
- };
- goog.Uri.prototype.getUserInfo = function() {
- return this.userInfo_;
- };
- goog.Uri.prototype.setUserInfo = function(newUserInfo, opt_decode) {
- this.enforceReadOnly();
- this.userInfo_ =
- opt_decode ? goog.Uri.decodeOrEmpty_(newUserInfo) : newUserInfo;
- return this;
- };
- goog.Uri.prototype.hasUserInfo = function() {
- return !!this.userInfo_;
- };
- goog.Uri.prototype.getDomain = function() {
- return this.domain_;
- };
- goog.Uri.prototype.setDomain = function(newDomain, opt_decode) {
- this.enforceReadOnly();
- this.domain_ =
- opt_decode ? goog.Uri.decodeOrEmpty_(newDomain, true) : newDomain;
- return this;
- };
- goog.Uri.prototype.hasDomain = function() {
- return !!this.domain_;
- };
- goog.Uri.prototype.getPort = function() {
- return this.port_;
- };
- goog.Uri.prototype.setPort = function(newPort) {
- this.enforceReadOnly();
- if (newPort) {
- newPort = Number(newPort);
- if (isNaN(newPort) || newPort < 0) {
- throw Error('Bad port number ' + newPort);
- }
- this.port_ = newPort;
- } else {
- this.port_ = null;
- }
- return this;
- };
- goog.Uri.prototype.hasPort = function() {
- return this.port_ != null;
- };
- goog.Uri.prototype.getPath = function() {
- return this.path_;
- };
- goog.Uri.prototype.setPath = function(newPath, opt_decode) {
- this.enforceReadOnly();
- this.path_ = opt_decode ? goog.Uri.decodeOrEmpty_(newPath, true) : newPath;
- return this;
- };
- goog.Uri.prototype.hasPath = function() {
- return !!this.path_;
- };
- goog.Uri.prototype.hasQuery = function() {
- return this.queryData_.toString() !== '';
- };
- goog.Uri.prototype.setQueryData = function(queryData, opt_decode) {
- this.enforceReadOnly();
- if (queryData instanceof goog.Uri.QueryData) {
- this.queryData_ = queryData;
- this.queryData_.setIgnoreCase(this.ignoreCase_);
- } else {
- if (!opt_decode) {
-
-
- queryData = goog.Uri.encodeSpecialChars_(
- queryData, goog.Uri.reDisallowedInQuery_);
- }
- this.queryData_ = new goog.Uri.QueryData(queryData, null, this.ignoreCase_);
- }
- return this;
- };
- goog.Uri.prototype.setQuery = function(newQuery, opt_decode) {
- return this.setQueryData(newQuery, opt_decode);
- };
- goog.Uri.prototype.getEncodedQuery = function() {
- return this.queryData_.toString();
- };
- goog.Uri.prototype.getDecodedQuery = function() {
- return this.queryData_.toDecodedString();
- };
- goog.Uri.prototype.getQueryData = function() {
- return this.queryData_;
- };
- goog.Uri.prototype.getQuery = function() {
- return this.getEncodedQuery();
- };
- goog.Uri.prototype.setParameterValue = function(key, value) {
- this.enforceReadOnly();
- this.queryData_.set(key, value);
- return this;
- };
- goog.Uri.prototype.setParameterValues = function(key, values) {
- this.enforceReadOnly();
- if (!goog.isArray(values)) {
- values = [String(values)];
- }
- this.queryData_.setValues(key, values);
- return this;
- };
- goog.Uri.prototype.getParameterValues = function(name) {
- return this.queryData_.getValues(name);
- };
- goog.Uri.prototype.getParameterValue = function(paramName) {
-
-
-
- return (this.queryData_.get(paramName));
- };
- goog.Uri.prototype.getFragment = function() {
- return this.fragment_;
- };
- goog.Uri.prototype.setFragment = function(newFragment, opt_decode) {
- this.enforceReadOnly();
- this.fragment_ =
- opt_decode ? goog.Uri.decodeOrEmpty_(newFragment) : newFragment;
- return this;
- };
- goog.Uri.prototype.hasFragment = function() {
- return !!this.fragment_;
- };
- goog.Uri.prototype.hasSameDomainAs = function(uri2) {
- return ((!this.hasDomain() && !uri2.hasDomain()) ||
- this.getDomain() == uri2.getDomain()) &&
- ((!this.hasPort() && !uri2.hasPort()) ||
- this.getPort() == uri2.getPort());
- };
- goog.Uri.prototype.makeUnique = function() {
- this.enforceReadOnly();
- this.setParameterValue(goog.Uri.RANDOM_PARAM, goog.string.getRandomString());
- return this;
- };
- goog.Uri.prototype.removeParameter = function(key) {
- this.enforceReadOnly();
- this.queryData_.remove(key);
- return this;
- };
- goog.Uri.prototype.setReadOnly = function(isReadOnly) {
- this.isReadOnly_ = isReadOnly;
- return this;
- };
- goog.Uri.prototype.isReadOnly = function() {
- return this.isReadOnly_;
- };
- goog.Uri.prototype.enforceReadOnly = function() {
- if (this.isReadOnly_) {
- throw Error('Tried to modify a read-only Uri');
- }
- };
- goog.Uri.prototype.setIgnoreCase = function(ignoreCase) {
- this.ignoreCase_ = ignoreCase;
- if (this.queryData_) {
- this.queryData_.setIgnoreCase(ignoreCase);
- }
- return this;
- };
- goog.Uri.prototype.getIgnoreCase = function() {
- return this.ignoreCase_;
- };
- goog.Uri.parse = function(uri, opt_ignoreCase) {
- return uri instanceof goog.Uri ? uri.clone() :
- new goog.Uri(uri, opt_ignoreCase);
- };
- goog.Uri.create = function(
- opt_scheme, opt_userInfo, opt_domain, opt_port, opt_path, opt_query,
- opt_fragment, opt_ignoreCase) {
- var uri = new goog.Uri(null, opt_ignoreCase);
-
- opt_scheme && uri.setScheme(opt_scheme);
- opt_userInfo && uri.setUserInfo(opt_userInfo);
- opt_domain && uri.setDomain(opt_domain);
- opt_port && uri.setPort(opt_port);
- opt_path && uri.setPath(opt_path);
- opt_query && uri.setQueryData(opt_query);
- opt_fragment && uri.setFragment(opt_fragment);
- return uri;
- };
- goog.Uri.resolve = function(base, rel) {
- if (!(base instanceof goog.Uri)) {
- base = goog.Uri.parse(base);
- }
- if (!(rel instanceof goog.Uri)) {
- rel = goog.Uri.parse(rel);
- }
- return base.resolve(rel);
- };
- goog.Uri.removeDotSegments = function(path) {
- if (path == '..' || path == '.') {
- return '';
- } else if (
- !goog.string.contains(path, './') && !goog.string.contains(path, '/.')) {
-
-
- return path;
- } else {
- var leadingSlash = goog.string.startsWith(path, '/');
- var segments = path.split('/');
- var out = [];
- for (var pos = 0; pos < segments.length;) {
- var segment = segments[pos++];
- if (segment == '.') {
- if (leadingSlash && pos == segments.length) {
- out.push('');
- }
- } else if (segment == '..') {
- if (out.length > 1 || out.length == 1 && out[0] != '') {
- out.pop();
- }
- if (leadingSlash && pos == segments.length) {
- out.push('');
- }
- } else {
- out.push(segment);
- leadingSlash = true;
- }
- }
- return out.join('/');
- }
- };
- goog.Uri.decodeOrEmpty_ = function(val, opt_preserveReserved) {
-
- if (!val) {
- return '';
- }
-
-
-
- return opt_preserveReserved ? decodeURI(val.replace(/%25/g, '%2525')) :
- decodeURIComponent(val);
- };
- goog.Uri.encodeSpecialChars_ = function(
- unescapedPart, extra, opt_removeDoubleEncoding) {
- if (goog.isString(unescapedPart)) {
- var encoded = encodeURI(unescapedPart).replace(extra, goog.Uri.encodeChar_);
- if (opt_removeDoubleEncoding) {
-
-
- encoded = goog.Uri.removeDoubleEncoding_(encoded);
- }
- return encoded;
- }
- return null;
- };
- goog.Uri.encodeChar_ = function(ch) {
- var n = ch.charCodeAt(0);
- return '%' + ((n >> 4) & 0xf).toString(16) + (n & 0xf).toString(16);
- };
- goog.Uri.removeDoubleEncoding_ = function(doubleEncodedString) {
- return doubleEncodedString.replace(/%25([0-9a-fA-F]{2})/g, '%$1');
- };
- goog.Uri.reDisallowedInSchemeOrUserInfo_ = /[#\/\?@]/g;
- goog.Uri.reDisallowedInRelativePath_ = /[\#\?:]/g;
- goog.Uri.reDisallowedInAbsolutePath_ = /[\#\?]/g;
- goog.Uri.reDisallowedInQuery_ = /[\#\?@]/g;
- goog.Uri.reDisallowedInFragment_ = /#/g;
- goog.Uri.haveSameDomain = function(uri1String, uri2String) {
-
-
- var pieces1 = goog.uri.utils.split(uri1String);
- var pieces2 = goog.uri.utils.split(uri2String);
- return pieces1[goog.uri.utils.ComponentIndex.DOMAIN] ==
- pieces2[goog.uri.utils.ComponentIndex.DOMAIN] &&
- pieces1[goog.uri.utils.ComponentIndex.PORT] ==
- pieces2[goog.uri.utils.ComponentIndex.PORT];
- };
- goog.Uri.QueryData = function(opt_query, opt_uri, opt_ignoreCase) {
-
- this.keyMap_ = null;
-
- this.count_ = null;
-
- this.encodedQuery_ = opt_query || null;
-
- this.ignoreCase_ = !!opt_ignoreCase;
- };
- goog.Uri.QueryData.prototype.ensureKeyMapInitialized_ = function() {
- if (!this.keyMap_) {
- this.keyMap_ = new goog.structs.Map();
- this.count_ = 0;
- if (this.encodedQuery_) {
- var self = this;
- goog.uri.utils.parseQueryData(this.encodedQuery_, function(name, value) {
- self.add(goog.string.urlDecode(name), value);
- });
- }
- }
- };
- goog.Uri.QueryData.createFromMap = function(map, opt_uri, opt_ignoreCase) {
- var keys = goog.structs.getKeys(map);
- if (typeof keys == 'undefined') {
- throw Error('Keys are undefined');
- }
- var queryData = new goog.Uri.QueryData(null, null, opt_ignoreCase);
- var values = goog.structs.getValues(map);
- for (var i = 0; i < keys.length; i++) {
- var key = keys[i];
- var value = values[i];
- if (!goog.isArray(value)) {
- queryData.add(key, value);
- } else {
- queryData.setValues(key, value);
- }
- }
- return queryData;
- };
- goog.Uri.QueryData.createFromKeysValues = function(
- keys, values, opt_uri, opt_ignoreCase) {
- if (keys.length != values.length) {
- throw Error('Mismatched lengths for keys/values');
- }
- var queryData = new goog.Uri.QueryData(null, null, opt_ignoreCase);
- for (var i = 0; i < keys.length; i++) {
- queryData.add(keys[i], values[i]);
- }
- return queryData;
- };
- goog.Uri.QueryData.prototype.getCount = function() {
- this.ensureKeyMapInitialized_();
- return this.count_;
- };
- goog.Uri.QueryData.prototype.add = function(key, value) {
- this.ensureKeyMapInitialized_();
- this.invalidateCache_();
- key = this.getKeyName_(key);
- var values = this.keyMap_.get(key);
- if (!values) {
- this.keyMap_.set(key, (values = []));
- }
- values.push(value);
- this.count_ = goog.asserts.assertNumber(this.count_) + 1;
- return this;
- };
- goog.Uri.QueryData.prototype.remove = function(key) {
- this.ensureKeyMapInitialized_();
- key = this.getKeyName_(key);
- if (this.keyMap_.containsKey(key)) {
- this.invalidateCache_();
-
- this.count_ =
- goog.asserts.assertNumber(this.count_) - this.keyMap_.get(key).length;
- return this.keyMap_.remove(key);
- }
- return false;
- };
- goog.Uri.QueryData.prototype.clear = function() {
- this.invalidateCache_();
- this.keyMap_ = null;
- this.count_ = 0;
- };
- goog.Uri.QueryData.prototype.isEmpty = function() {
- this.ensureKeyMapInitialized_();
- return this.count_ == 0;
- };
- goog.Uri.QueryData.prototype.containsKey = function(key) {
- this.ensureKeyMapInitialized_();
- key = this.getKeyName_(key);
- return this.keyMap_.containsKey(key);
- };
- goog.Uri.QueryData.prototype.containsValue = function(value) {
-
-
-
- var vals = this.getValues();
- return goog.array.contains(vals, value);
- };
- goog.Uri.QueryData.prototype.forEach = function(f, opt_scope) {
- this.ensureKeyMapInitialized_();
- this.keyMap_.forEach(function(values, key) {
- goog.array.forEach(values, function(value) {
- f.call(opt_scope, value, key, this);
- }, this);
- }, this);
- };
- goog.Uri.QueryData.prototype.getKeys = function() {
- this.ensureKeyMapInitialized_();
-
- var vals = this.keyMap_.getValues();
- var keys = this.keyMap_.getKeys();
- var rv = [];
- for (var i = 0; i < keys.length; i++) {
- var val = vals[i];
- for (var j = 0; j < val.length; j++) {
- rv.push(keys[i]);
- }
- }
- return rv;
- };
- goog.Uri.QueryData.prototype.getValues = function(opt_key) {
- this.ensureKeyMapInitialized_();
- var rv = [];
- if (goog.isString(opt_key)) {
- if (this.containsKey(opt_key)) {
- rv = goog.array.concat(rv, this.keyMap_.get(this.getKeyName_(opt_key)));
- }
- } else {
-
- var values = this.keyMap_.getValues();
- for (var i = 0; i < values.length; i++) {
- rv = goog.array.concat(rv, values[i]);
- }
- }
- return rv;
- };
- goog.Uri.QueryData.prototype.set = function(key, value) {
- this.ensureKeyMapInitialized_();
- this.invalidateCache_();
-
-
-
-
-
- key = this.getKeyName_(key);
- if (this.containsKey(key)) {
- this.count_ =
- goog.asserts.assertNumber(this.count_) - this.keyMap_.get(key).length;
- }
- this.keyMap_.set(key, [value]);
- this.count_ = goog.asserts.assertNumber(this.count_) + 1;
- return this;
- };
- goog.Uri.QueryData.prototype.get = function(key, opt_default) {
- var values = key ? this.getValues(key) : [];
- if (goog.Uri.preserveParameterTypesCompatibilityFlag) {
- return values.length > 0 ? values[0] : opt_default;
- } else {
- return values.length > 0 ? String(values[0]) : opt_default;
- }
- };
- goog.Uri.QueryData.prototype.setValues = function(key, values) {
- this.remove(key);
- if (values.length > 0) {
- this.invalidateCache_();
- this.keyMap_.set(this.getKeyName_(key), goog.array.clone(values));
- this.count_ = goog.asserts.assertNumber(this.count_) + values.length;
- }
- };
- goog.Uri.QueryData.prototype.toString = function() {
- if (this.encodedQuery_) {
- return this.encodedQuery_;
- }
- if (!this.keyMap_) {
- return '';
- }
- var sb = [];
-
-
-
- var keys = this.keyMap_.getKeys();
- for (var i = 0; i < keys.length; i++) {
- var key = keys[i];
- var encodedKey = goog.string.urlEncode(key);
- var val = this.getValues(key);
- for (var j = 0; j < val.length; j++) {
- var param = encodedKey;
-
-
- if (val[j] !== '') {
- param += '=' + goog.string.urlEncode(val[j]);
- }
- sb.push(param);
- }
- }
- return this.encodedQuery_ = sb.join('&');
- };
- goog.Uri.QueryData.prototype.toDecodedString = function() {
- return goog.Uri.decodeOrEmpty_(this.toString());
- };
- goog.Uri.QueryData.prototype.invalidateCache_ = function() {
- this.encodedQuery_ = null;
- };
- goog.Uri.QueryData.prototype.filterKeys = function(keys) {
- this.ensureKeyMapInitialized_();
- this.keyMap_.forEach(function(value, key) {
- if (!goog.array.contains(keys, key)) {
- this.remove(key);
- }
- }, this);
- return this;
- };
- goog.Uri.QueryData.prototype.clone = function() {
- var rv = new goog.Uri.QueryData();
- rv.encodedQuery_ = this.encodedQuery_;
- if (this.keyMap_) {
- rv.keyMap_ = this.keyMap_.clone();
- rv.count_ = this.count_;
- }
- return rv;
- };
- goog.Uri.QueryData.prototype.getKeyName_ = function(arg) {
- var keyName = String(arg);
- if (this.ignoreCase_) {
- keyName = keyName.toLowerCase();
- }
- return keyName;
- };
- goog.Uri.QueryData.prototype.setIgnoreCase = function(ignoreCase) {
- var resetKeys = ignoreCase && !this.ignoreCase_;
- if (resetKeys) {
- this.ensureKeyMapInitialized_();
- this.invalidateCache_();
- this.keyMap_.forEach(function(value, key) {
- var lowerCase = key.toLowerCase();
- if (key != lowerCase) {
- this.remove(key);
- this.setValues(lowerCase, value);
- }
- }, this);
- }
- this.ignoreCase_ = ignoreCase;
- };
- goog.Uri.QueryData.prototype.extend = function(var_args) {
- for (var i = 0; i < arguments.length; i++) {
- var data = arguments[i];
- goog.structs.forEach(
- data, function(value, key) { this.add(key, value); }, this);
- }
- };
|