@ui5/webcomponents-base 2.10.0 → 2.10.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,148 @@
1
+ import TestGeneric from "../../test/test-elements/Generic.js";
2
+ import { setThemeRoot } from "../../src/config/ThemeRoot.js";
3
+ import { getTheme } from "../../src/config/Theme.js";
4
+ import applyTheme from "../../src/theming/applyTheme.js";
5
+
6
+ const THEME = "sap_horizon";
7
+
8
+ const addMetaTag = (content: string) => {
9
+ cy.window().then($win => {
10
+ const metaTag = $win.document.createElement("meta");
11
+ metaTag.name = "sap-allowed-theme-origins";
12
+ metaTag.content = content;
13
+ $win.document.head.append(metaTag);
14
+ });
15
+ };
16
+
17
+ const removeMetaTag = () => {
18
+ cy.window().then($win => {
19
+ $win.document.head.querySelector("[name='sap-allowed-theme-origins']")?.remove();
20
+ });
21
+ };
22
+
23
+ const removeThemeLink = () => {
24
+ cy.window().then($win => {
25
+ $win.document.head.querySelector("link[sap-ui-webcomponents-theme]")?.remove();
26
+ });
27
+ };
28
+
29
+ const applyAndCheckLink = (theme: string) => {
30
+ cy.wrap({ applyTheme, getTheme })
31
+ .invoke("getTheme")
32
+ .then(() => {
33
+ return cy.wrap({ applyTheme }).invoke("applyTheme", theme);
34
+ });
35
+ };
36
+
37
+ // ─── Without meta tag ────────────────────────────────────────────────────────
38
+
39
+ describe("ThemeRoot via setThemeRoot API — without meta tag", () => {
40
+ before(() => {
41
+ cy.mount(<TestGeneric />);
42
+ });
43
+
44
+ afterEach(() => {
45
+ removeThemeLink();
46
+ cy.wrap({ setThemeRoot }).invoke("setThemeRoot", undefined);
47
+ });
48
+
49
+ [
50
+ { label: "absolute URL (different origin)", themeRoot: "http://example2.com/themes/", blocked: true },
51
+ { label: "absolute URL (different protocol)", themeRoot: "https://example.com/themes/", blocked: true },
52
+ { label: "absolute URL (different port)", themeRoot: "http://example:9090.com/themes/", blocked: true },
53
+ { label: "absolute URL (same host, no meta tag)", themeRoot: "http://example.com/themes/", blocked: true },
54
+ { label: "protocol-relative (different origin)", themeRoot: "//example2.com/themes/", blocked: true },
55
+ { label: "protocol-relative (different port)", themeRoot: "//example:9090.com/themes/", blocked: true },
56
+ { label: "protocol-relative (same host, no meta tag)", themeRoot: "//example.com/themes/", blocked: true },
57
+ { label: "root-relative", themeRoot: "/themes/", blocked: false },
58
+ { label: "relative (current dir)", themeRoot: "./themes/", blocked: false },
59
+ { label: "relative (parent dir)", themeRoot: "../themes/", blocked: false },
60
+ ].forEach(({ label, themeRoot, blocked }) => {
61
+ it(`${blocked ? "should not create" : "should create"} a link element — ${label}: ${themeRoot}`, () => {
62
+ cy.wrap({ setThemeRoot }).invoke("setThemeRoot", themeRoot);
63
+ applyAndCheckLink(THEME);
64
+ if (blocked) {
65
+ cy.get(`link[sap-ui-webcomponents-theme='${THEME}']`).should("not.exist");
66
+ } else {
67
+ cy.get(`link[sap-ui-webcomponents-theme='${THEME}']`)
68
+ .should("exist")
69
+ .and("have.attr", "href")
70
+ .and("include", `UI5/Base/baseLib/${THEME}/css_variables.css`);
71
+ }
72
+ });
73
+ });
74
+ });
75
+
76
+ // ─── With meta tag ────────────────────────────────────────────────────────────
77
+
78
+ describe("ThemeRoot via setThemeRoot API — with meta tag (allowed: http://example.com)", () => {
79
+ before(() => {
80
+ addMetaTag("http://example.com");
81
+ cy.mount(<TestGeneric />);
82
+ });
83
+
84
+ afterEach(() => {
85
+ removeThemeLink();
86
+ cy.wrap({ setThemeRoot }).invoke("setThemeRoot", undefined);
87
+ });
88
+
89
+ after(() => {
90
+ removeMetaTag();
91
+ });
92
+
93
+ const blocked = [
94
+ { label: "absolute URL (different origin)", themeRoot: "http://example2.com/themes/" },
95
+ { label: "absolute URL (different protocol)", themeRoot: "https://example.com/themes/" },
96
+ { label: "absolute URL (different port)", themeRoot: "http://example:9090.com/themes/" },
97
+ { label: "protocol-relative (different origin)", themeRoot: "//example2.com/themes/" },
98
+ { label: "protocol-relative (different port)", themeRoot: "//example:9090.com/themes/" },
99
+ ];
100
+
101
+ const allowed = [
102
+ {
103
+ label: "absolute URL (matches allowed origin)",
104
+ themeRoot: "http://example.com/themes/",
105
+ expectedHref: "http://example.com/themes/UI5/Base/baseLib/sap_horizon/css_variables.css",
106
+ },
107
+ {
108
+ label: "protocol-relative (resolves to allowed origin)",
109
+ themeRoot: "//example.com/themes/",
110
+ expectedHref: "http://example.com/themes/UI5/Base/baseLib/sap_horizon/css_variables.css",
111
+ },
112
+ ];
113
+
114
+ const sameOriginAllowed = [
115
+ { label: "root-relative", themeRoot: "/themes/" },
116
+ { label: "relative (current dir)", themeRoot: "./themes/" },
117
+ { label: "relative (parent dir)", themeRoot: "../themes/" },
118
+ ];
119
+
120
+ blocked.forEach(({ label, themeRoot }) => {
121
+ it(`should not create a link element — blocked: ${label}: ${themeRoot}`, () => {
122
+ cy.wrap({ setThemeRoot }).invoke("setThemeRoot", themeRoot);
123
+ applyAndCheckLink(THEME);
124
+ cy.get(`link[sap-ui-webcomponents-theme='${THEME}']`).should("not.exist");
125
+ });
126
+ });
127
+
128
+ allowed.forEach(({ label, themeRoot, expectedHref }) => {
129
+ it(`should create a link element with correct href — allowed: ${label}: ${themeRoot}`, () => {
130
+ cy.wrap({ setThemeRoot }).invoke("setThemeRoot", themeRoot);
131
+ applyAndCheckLink(THEME);
132
+ cy.get(`link[sap-ui-webcomponents-theme='${THEME}']`)
133
+ .should("exist")
134
+ .and("have.attr", "href", expectedHref);
135
+ });
136
+ });
137
+
138
+ sameOriginAllowed.forEach(({ label, themeRoot }) => {
139
+ it(`should create a link element — same-origin: ${label}: ${themeRoot}`, () => {
140
+ cy.wrap({ setThemeRoot }).invoke("setThemeRoot", themeRoot);
141
+ applyAndCheckLink(THEME);
142
+ cy.get(`link[sap-ui-webcomponents-theme='${THEME}']`)
143
+ .should("exist")
144
+ .and("have.attr", "href")
145
+ .and("include", `UI5/Base/baseLib/${THEME}/css_variables.css`);
146
+ });
147
+ });
148
+ });