(anchorEl, url, opt_cache)
| 104 | * @restricted |
| 105 | */ |
| 106 | export function parseUrlWithA(anchorEl, url, opt_cache) { |
| 107 | if (mode.isEsm()) { |
| 108 | // Doing this causes the <a> to auto-set its own href to the resolved path, |
| 109 | // which would be the baseUrl for the URL constructor. |
| 110 | anchorEl.href = ''; |
| 111 | return /** @type {?} */ (new URL(url, anchorEl.href)); |
| 112 | } |
| 113 | |
| 114 | if (opt_cache && opt_cache.has(url)) { |
| 115 | return opt_cache.get(url); |
| 116 | } |
| 117 | |
| 118 | anchorEl.href = url; |
| 119 | |
| 120 | // IE11 doesn't provide full URL components when parsing relative URLs. |
| 121 | // Assigning to itself again does the trick #3449. |
| 122 | if (!anchorEl.protocol) { |
| 123 | anchorEl.href = anchorEl.href; |
| 124 | } |
| 125 | |
| 126 | const info = /** @type {!Location} */ ({ |
| 127 | href: anchorEl.href, |
| 128 | protocol: anchorEl.protocol, |
| 129 | host: anchorEl.host, |
| 130 | hostname: anchorEl.hostname, |
| 131 | port: anchorEl.port == '0' ? '' : anchorEl.port, |
| 132 | pathname: anchorEl.pathname, |
| 133 | search: anchorEl.search, |
| 134 | hash: anchorEl.hash, |
| 135 | origin: null, // Set below. |
| 136 | }); |
| 137 | |
| 138 | // Some IE11 specific polyfills. |
| 139 | // 1) IE11 strips out the leading '/' in the pathname. |
| 140 | if (info.pathname[0] !== '/') { |
| 141 | info.pathname = '/' + info.pathname; |
| 142 | } |
| 143 | |
| 144 | // 2) For URLs with implicit ports, IE11 parses to default ports while |
| 145 | // other browsers leave the port field empty. |
| 146 | if ( |
| 147 | (info.protocol == 'http:' && info.port == 80) || |
| 148 | (info.protocol == 'https:' && info.port == 443) |
| 149 | ) { |
| 150 | info.port = ''; |
| 151 | info.host = info.hostname; |
| 152 | } |
| 153 | |
| 154 | // For data URI anchorEl.origin is equal to the string 'null' which is not useful. |
| 155 | // We instead return the actual origin which is the full URL. |
| 156 | let origin; |
| 157 | if (anchorEl.origin && anchorEl.origin != 'null') { |
| 158 | origin = anchorEl.origin; |
| 159 | } else if (info.protocol == 'data:' || !info.host) { |
| 160 | origin = info.href; |
| 161 | } else { |
| 162 | origin = info.protocol + '//' + info.host; |
| 163 | } |
no test coverage detected