param parentUri param childUri return export function resolveReferenceUri ( parentUri , childUri ) { if ( absoluteUrlWithOriginRe . test ( childUri ) ) { return childUri ; } else if ( childUri . startsWith ( "/" ) ) { const origin = getUrlOrigin ( parentUri ) ; return origin + childUri ; } else { if ( childUri . startsWith ( "./" ) ) { childUri = childUri . substr ( 2 ) ; } const parentBaseUrl = baseUrl ( parentUri ) ; return parentBaseUrl + childUri ; } } const absoluteUrlWithOriginRe = new RegExp ( "^(((?:[a-z]+:)?//)|(file:/)|(data:))" , "i" ) ; /**
 * Returns base URL of given resource URL.
 *
 * `Url` with trailing slash are considered genuine 'locations', they are returned as is, however if
 * `url` ends with name component it is treated as "leaf", so last path component is removed.
 *
 * Standalone files (without any folder structure) are considered relative to `./`.
 *
 * Examples:
 * ```
 *     https://foo.com/themes/a.json -> https://foo.com/themes/
 *     https://foo.com/themes/ -> https://foo.com/themes/
 *     https://foo.com/themes -> https://foo.com/ // note, themes is treated as leaf
 *     themes/day.json -> themes/
 *     themes -> ./
 * ```
 */ export function baseUrl ( url ) { if ( url === undefined ) { return "./" ; } let idx = url . indexOf ( "#" ) ; if ( idx !== - 1 ) { url = url . slice ( 0 , idx ) ; } idx = url . indexOf ( "?" ) ; if ( idx !== - 1 ) { url = url . slice ( 0 , idx ) ; } idx = url . lastIndexOf ( "/" ) ; if ( idx === - 1 ) { return "./" ; } else { return url . substring ( 0 , idx + 1 ) ; } } example param url return export function getUrlOrigin ( url ) { if ( url === undefined ) { return "" ; } const parsed = getUrlHostAndProtocol ( url ) ; if ( parsed . protocol === "file:" ) { return "file://" ; } else if ( parsed . host && parsed . protocol ) { return parsed . protocol + "//" + parsed . host ; } else if ( parsed . host ) { return "//" + parsed . host ; } else if ( parsed . protocol ) { return parsed . protocol + "//" ; } else { return "" ; } } /**
 * Parse `host` and `protocol` part from URL.
 */ export function getUrlHostAndProtocol ( url ) { const urlOriginRe = new RegExp ( /^(?:([a-z]+:))?\/\/([^\/]*)/ , "i" ) ; const match = url . match ( urlOriginRe ) ; if ( ! match ) { throw new Error ( `getUrlHostAndProtocol: unable to parse URL '${ url }'` ) ; } return { protocol : match [ 1 ] , host : match [ 2 ] } ; }