MCPcopy
hub / github.com/ampproject/amphtml / resolveRelativeUrlFallback_

Function resolveRelativeUrlFallback_

src/url.js:573–599  ·  view source on GitHub ↗
(relativeUrlString, baseUrl)

Source from the content-addressed store, hash-verified

571 * @private @visibleForTesting
572 */
573export function resolveRelativeUrlFallback_(relativeUrlString, baseUrl) {
574 baseUrl = urlAsLocation(baseUrl);
575 relativeUrlString = relativeUrlString.replace(/\\/g, '/');
576 const relativeUrl = parseUrlDeprecated(relativeUrlString);
577
578 // Absolute URL.
579 if (relativeUrlString.toLowerCase().startsWith(relativeUrl.protocol)) {
580 return relativeUrl.href;
581 }
582
583 // Protocol-relative URL.
584 if (relativeUrlString.startsWith('//')) {
585 return baseUrl.protocol + relativeUrlString;
586 }
587
588 // Absolute path.
589 if (relativeUrlString.startsWith('/')) {
590 return baseUrl.origin + relativeUrlString;
591 }
592
593 // Relative path.
594 return (
595 baseUrl.origin +
596 baseUrl.pathname.replace(/\/[^/]*$/, '/') +
597 relativeUrlString
598 );
599}
600
601/**
602 * Add "__amp_source_origin" query parameter to the URL.

Callers 2

testRelUrlFunction · 0.90
resolveRelativeUrlFunction · 0.85

Calls 3

urlAsLocationFunction · 0.85
parseUrlDeprecatedFunction · 0.85
replaceMethod · 0.45

Tested by

no test coverage detected