Skip to content
This repository has been archived by the owner on Aug 1, 2024. It is now read-only.

Commit

Permalink
Implement toString in UrlLike return value for IE.
Browse files Browse the repository at this point in the history
RELNOTES: Implement toString in UrlLike return value for IE.

PiperOrigin-RevId: 475399562
Change-Id: Ib0437faf9422290cd5350e8fa8d5ac4193f40d5c
  • Loading branch information
12wrigja authored and copybara-github committed Sep 19, 2022
1 parent 9808f4a commit ceb174a
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
4 changes: 3 additions & 1 deletion closure/goog/url/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -382,8 +382,9 @@ const createAnchorElementInIE = function(urlStr) {
if (!aTag.hostname) {
throw new Error(`${urlStr} is not a valid URL.`);
}
const href = aTag.href;
const urlLike = {
href: aTag.href,
href,
protocol: aTag.protocol,
username: '',
password: '',
Expand All @@ -392,6 +393,7 @@ const createAnchorElementInIE = function(urlStr) {
pathname: '/' + aTag.pathname,
search: aTag.search,
hash: aTag.hash,
toString: () => href,
};
// Canonicalize the port out from the URL if it matches
const canonicalPort = canonicalPortForProtocols.get(aTag.protocol);
Expand Down
54 changes: 54 additions & 0 deletions closure/goog/url/url_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ const resolveWithTestChecks = function(urlStr, baseStr = undefined) {
assertEquals(nativeResolve.pathname, packageResolve.pathname);
assertEquals(nativeResolve.search, packageResolve.search);
assertEquals(nativeResolve.hash, packageResolve.hash);
assertEquals(nativeResolve.href, packageResolve.href);
assertEquals(nativeResolve.toString(), packageResolve.toString());
}
if (packageThrow) {
throw packageThrow;
Expand All @@ -92,6 +94,8 @@ testSuite({
assertEquals('/', url.pathname);
assertEquals('', url.search);
assertEquals('', url.hash);
assertEquals('http://www.google.com/', url.toString());
assertEquals('http://www.google.com/', url.href);
},

testWithPort() {
Expand All @@ -106,6 +110,8 @@ testSuite({
assertEquals('/', url.pathname);
assertEquals('', url.search);
assertEquals('', url.hash);
assertEquals('http://www.google.com:8080/', url.toString());
assertEquals('http://www.google.com:8080/', url.href);
},

testWithPath() {
Expand All @@ -120,6 +126,8 @@ testSuite({
assertEquals('/search', url.pathname);
assertEquals('', url.search);
assertEquals('', url.hash);
assertEquals('http://www.google.com/search', url.toString());
assertEquals('http://www.google.com/search', url.href);
},

testWithQueryData() {
Expand All @@ -134,6 +142,8 @@ testSuite({
assertEquals('/path', url.pathname);
assertEquals('?a=b&b=c', url.search);
assertEquals('', url.hash);
assertEquals('http://www.google.com/path?a=b&b=c', url.toString());
assertEquals('http://www.google.com/path?a=b&b=c', url.href);
},

testComplex() {
Expand All @@ -149,6 +159,10 @@ testSuite({
assertEquals('/path', url.pathname);
assertEquals('?q=query', url.search);
assertEquals('#fragmento', url.hash);
assertEquals(
'http://www.google.com:8080/path?q=query#fragmento', url.toString());
assertEquals(
'http://www.google.com:8080/path?q=query#fragmento', url.href);
},

testWithNewline() {
Expand All @@ -162,6 +176,10 @@ testSuite({
assertEquals('/path', url.pathname);
assertEquals('?q=query', url.search);
assertEquals('#fragmento', url.hash);
assertEquals(
'http://www.google.com:8080/path?q=query#fragmento', url.toString());
assertEquals(
'http://www.google.com:8080/path?q=query#fragmento', url.href);
},

testWithRelativeParam() {
Expand All @@ -175,6 +193,10 @@ testSuite({
assertEquals('/path', url.pathname);
assertEquals('?q=query', url.search);
assertEquals('#fragmento', url.hash);
assertEquals(
'http://www.google.com:8080/path?q=query#fragmento', url.toString());
assertEquals(
'http://www.google.com:8080/path?q=query#fragmento', url.href);
},

testWithRelativeParamThatIsAbsolute() {
Expand All @@ -192,6 +214,9 @@ testSuite({
assertEquals('/path', url.pathname);
assertEquals('?q=query', url.search);
assertEquals('#fragmento', url.hash);
assertEquals(
'https://docs.google.com/path?q=query#fragmento', url.toString());
assertEquals('https://docs.google.com/path?q=query#fragmento', url.href);
},

testWithBaseThatHasRelativeParts() {
Expand All @@ -206,6 +231,9 @@ testSuite({
assertEquals('/path1', url.pathname);
assertEquals('?q=query', url.search);
assertEquals('#fragmento', url.hash);
assertEquals(
'https://docs.google.com/path1?q=query#fragmento', url.toString());
assertEquals('https://docs.google.com/path1?q=query#fragmento', url.href);
},

testWithBaseThatHasRelativePartsAndOnlySearchRelative() {
Expand All @@ -220,6 +248,8 @@ testSuite({
assertEquals('/path', url.pathname);
assertEquals('?q=query', url.search);
assertEquals('', url.hash);
assertEquals('https://google.com/path?q=query', url.toString());
assertEquals('https://google.com/path?q=query', url.href);
},

testWithBaseThatHasRelativePartsAndOnlyHashRelative() {
Expand All @@ -235,6 +265,8 @@ testSuite({
// And so is the query
assertEquals('?query=q', url.search);
assertEquals('#query', url.hash);
assertEquals('https://google.com/path?query=q#query', url.toString());
assertEquals('https://google.com/path?query=q#query', url.href);
},

testWithBaseAndNoIndicatorsInRelative() {
Expand All @@ -249,6 +281,8 @@ testSuite({
assertEquals('/query', url.pathname);
assertEquals('', url.search);
assertEquals('', url.hash);
assertEquals('https://google.com/query', url.toString());
assertEquals('https://google.com/query', url.href);
},

testResolvesRelativeToRelativePath() {
Expand All @@ -262,6 +296,8 @@ testSuite({
assertEquals('/new', url.pathname);
assertEquals('?q=query', url.search);
assertEquals('', url.hash);
assertEquals('https://google.com/new?q=query', url.toString());
assertEquals('https://google.com/new?q=query', url.href);
},

testResolvesRelativeToRelativePathBackslash() {
Expand All @@ -275,6 +311,8 @@ testSuite({
assertEquals('/new', url.pathname);
assertEquals('?q=query', url.search);
assertEquals('', url.hash);
assertEquals('https://google.com/new?q=query', url.toString());
assertEquals('https://google.com/new?q=query', url.href);
},

testResolvesTwoDots() {
Expand All @@ -289,6 +327,8 @@ testSuite({
// "after" that to be removed - search and hash
assertEquals(url.search, '');
assertEquals(url.hash, '');
assertEquals('https://google.com/maps/', url.toString());
assertEquals('https://google.com/maps/', url.href);
},

testResolvesTwoDotsNoBasePath() {
Expand All @@ -302,6 +342,8 @@ testSuite({
// "after" that to be removed - search and hash
assertEquals(url.search, '');
assertEquals(url.hash, '');
assertEquals('https://google.com/', url.toString());
assertEquals('https://google.com/', url.href);
},

testResolvesTwoDotsShortBasePath() {
Expand All @@ -316,6 +358,7 @@ testSuite({
// "after" that to be removed - search and hash
assertEquals(url.search, '');
assertEquals(url.hash, '');
assertEquals('https://google.com/', url.toString());
},

testResolvesDot() {
Expand All @@ -329,6 +372,8 @@ testSuite({
// "after" that to be removed - search and hash
assertEquals(url.search, '');
assertEquals(url.hash, '');
assertEquals('https://google.com/maps/search/', url.toString());
assertEquals('https://google.com/maps/search/', url.href);
},

testResolvesDotNoBasePath() {
Expand All @@ -341,6 +386,8 @@ testSuite({
// "after" that to be removed - search and hash
assertEquals(url.search, '');
assertEquals(url.hash, '');
assertEquals('https://google.com/', url.toString());
assertEquals('https://google.com/', url.href);
},

testResolvesDotShortBasePath() {
Expand All @@ -354,6 +401,8 @@ testSuite({
// "after" that to be removed - search and hash
assertEquals(url.search, '');
assertEquals(url.hash, '');
assertEquals('https://google.com/', url.toString());
assertEquals('https://google.com/', url.href);
},

testResolvesEmptyString() {
Expand All @@ -365,11 +414,16 @@ testSuite({
if (!userAgent.isEdge()) {
assertEquals(url.pathname, '/maps/search/new');
assertEquals(url.search, '?hl=de');
assertEquals(
'https://google.com/maps/search/new?hl=de', url.toString());
assertEquals('https://google.com/maps/search/new?hl=de', url.href);
} else {
// Edge is weird here and instead follows the same conventions for '.'
// it removes one chunk of path and every part after it.
assertEquals(url.pathname, '/maps/search/');
assertEquals(url.search, '');
assertEquals('https://google.com/maps/search/', url.toString());
assertEquals('https://google.com/maps/search/', url.href);
}
assertEquals(url.hash, '');
},
Expand Down

0 comments on commit ceb174a

Please # to comment.