MCPcopy Create free account
hub / github.com/UI5/webcomponents / validateThemeRoot

Function validateThemeRoot

packages/base/src/validateThemeRoot.ts:29–68  ·  view source on GitHub ↗
(themeRoot: string)

Source from the content-addressed store, hash-verified

27};
28
29const validateThemeRoot = (themeRoot: string) => {
30 let resultUrl;
31 let isSameOrigin = false;
32
33 try {
34 if (themeRoot.startsWith(".") || (themeRoot.startsWith("/") && !themeRoot.startsWith("//"))) {
35 // Handle relative url
36 // new URL("/newExmPath", "http://example.com/exmPath") => http://example.com/newExmPath
37 // new URL("./newExmPath", "http://example.com/exmPath") => http://example.com/exmPath/newExmPath
38 // new URL("../newExmPath", "http://example.com/exmPath") => http://example.com/newExmPath
39 resultUrl = new URL(themeRoot, getLocationHref()).toString();
40 isSameOrigin = true;
41 } else {
42 // Protocol-relative URLs (//host/path) need a base to resolve the protocol
43 const themeRootURL = themeRoot.startsWith("//") ? new URL(themeRoot, getLocationHref()) : new URL(themeRoot);
44 const origin = themeRootURL.origin;
45 const currentOrigin = new URL(getLocationHref()).origin;
46
47 // Check if the absolute URL is same-origin
48 isSameOrigin = origin === currentOrigin;
49
50 if (origin && validateThemeOrigin(origin, isSameOrigin)) {
51 // If origin is allowed, use it
52 resultUrl = themeRootURL.toString();
53 } else {
54 // If origin is not allowed, return undefined to indicate validation failed
55 return undefined;
56 }
57 }
58
59 if (!resultUrl.endsWith("/")) {
60 resultUrl = `${resultUrl}/`;
61 }
62
63 return `${resultUrl}UI5/`;
64 } catch (e) {
65 // Catch if URL is not correct
66 return undefined;
67 }
68};
69
70export default validateThemeRoot;

Callers 1

Calls 3

getLocationHrefFunction · 0.85
validateThemeOriginFunction · 0.85
toStringMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…