1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322/**
* next/head shim unit tests.
*
* Mirrors test cases from Next.js test/unit/next-head-rendering.test.ts,
* plus comprehensive coverage for vinext's Head SSR collection, HTML
* generation, allowed tags, and escaping.
*/
import { describe, it, expect, vi, beforeEach } from "vitest";
import React from "react";
import ReactDOMServer from "react-dom/server";
import Head, { resetSSRHead, getSSRHeadHTML, escapeAttr } from "../packages/vinext/src/shims/head.js";
// โโโ SSR rendering (mirrors Next.js test/unit/next-head-rendering.test.ts) โโ
describe("Rendering next/head", () => {
beforeEach(() => {
resetSSRHead();
});
it("should render outside of Next.js without error", () => {
// Next.js test: renderToString(<><Head /><p>hello world</p></>)
// Verifies Head doesn't throw when used standalone
const html = ReactDOMServer.renderToString(
React.createElement(React.Fragment, null,
React.createElement(Head, null),
React.createElement("p", null, "hello world"),
),
);
expect(html).toContain("hello world");
});
it("returns null (no rendered output in body)", () => {
const html = ReactDOMServer.renderToString(
React.createElement(Head, null,
React.createElement("title", null, "My Page"),
),
);
// Head always returns null โ elements are collected, not rendered inline
expect(html).toBe("");
});
});
// โโโ SSR head collection โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
describe("Head SSR collection", () => {
beforeEach(() => {
resetSSRHead();
});
it("collects title element", () => {
ReactDOMServer.renderToString(
React.createElement(Head, null,
React.createElement("title", null, "My Page Title"),
),
);
const headHtml = getSSRHeadHTML();
expect(headHtml).toContain("<title");
expect(headHtml).toContain("My Page Title");
expect(headHtml).toContain("</title>");
expect(headHtml).toContain('data-vinext-head="true"');
});
it("collects meta elements as self-closing", () => {
ReactDOMServer.renderToString(
React.createElement(Head, null,
React.createElement("meta", { name: "description", content: "A test page" }),
),
);
const headHtml = getSSRHeadHTML();
expect(headHtml).toContain('<meta name="description" content="A test page"');
expect(headHtml).toContain("/>"); // self-closing
expect(headHtml).not.toContain("</meta>");
});
it("collects link elements as self-closing", () => {
ReactDOMServer.renderToString(
React.createElement(Head, null,
React.createElement("link", { rel: "stylesheet", href: "/styles.css" }),
),
);
const headHtml = getSSRHeadHTML();
expect(headHtml).toContain('<link rel="stylesheet" href="/styles.css"');
expect(headHtml).toContain("/>"); // self-closing
});
it("collects style elements", () => {
ReactDOMServer.renderToString(
React.createElement(Head, null,
React.createElement("style", null, "body { color: red; }"),
),
);
const headHtml = getSSRHeadHTML();
expect(headHtml).toContain("<style");
// Text content is HTML-escaped
expect(headHtml).toContain("body { color: red; }");
});
it("collects script elements", () => {
ReactDOMServer.renderToString(
React.createElement(Head, null,
React.createElement("script", { src: "/analytics.js", async: true }),
),
);
const headHtml = getSSRHeadHTML();
expect(headHtml).toContain('<script src="/analytics.js" async');
expect(headHtml).toContain("</script>");
});
it("collects base element as self-closing", () => {
ReactDOMServer.renderToString(
React.createElement(Head, null,
React.createElement("base", { href: "https://example.com/" }),
),
);
const headHtml = getSSRHeadHTML();
expect(headHtml).toContain('<base href="https://example.com/"');
expect(headHtml).toContain("/>"); // self-closing
});
it("collects noscript elements", () => {
ReactDOMServer.renderToString(
React.createElement(Head, null,
React.createElement("noscript", null, "JavaScript is required"),
),
);
const headHtml = getSSRHeadHTML();
expect(headHtml).toContain("<noscript");
expect(headHtml).toContain("JavaScript is required");
expect(headHtml).toContain("</noscript>");
});
it("collects multiple head elements in order", () => {
ReactDOMServer.renderToString(
React.createElement(Head, null,
React.createElement("title", null, "First"),
React.createElement("meta", { name: "viewport", content: "width=device-width" }),
React.createElement("link", { rel: "icon", href: "/favicon.ico" }),
),
);
const headHtml = getSSRHeadHTML();
expect(headHtml).toContain("First");
expect(headHtml).toContain("viewport");
expect(headHtml).toContain("favicon.ico");
});
it("resets head between renders", () => {
ReactDOMServer.renderToString(
React.createElement(Head, null,
React.createElement("title", null, "Page 1"),
),
);
expect(getSSRHeadHTML()).toContain("Page 1");
resetSSRHead();
ReactDOMServer.renderToString(
React.createElement(Head, null,
React.createElement("title", null, "Page 2"),
),
);
const headHtml = getSSRHeadHTML();
expect(headHtml).toContain("Page 2");
expect(headHtml).not.toContain("Page 1");
});
it("returns empty string when no head elements", () => {
const headHtml = getSSRHeadHTML();
expect(headHtml).toBe("");
});
});
// โโโ Disallowed tags โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
describe("Head disallowed tags", () => {
beforeEach(() => {
resetSSRHead();
});
it("ignores <div> tag (not allowed in head)", () => {
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
ReactDOMServer.renderToString(
React.createElement(Head, null,
React.createElement("div", null, "bad"),
),
);
const headHtml = getSSRHeadHTML();
expect(headHtml).not.toContain("<div");
expect(headHtml).toBe("");
warn.mockRestore();
});
it("ignores <iframe> tag (security concern)", () => {
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
ReactDOMServer.renderToString(
React.createElement(Head, null,
React.createElement("iframe", { src: "https://evil.com" }),
),
);
const headHtml = getSSRHeadHTML();
expect(headHtml).not.toContain("<iframe");
expect(headHtml).toBe("");
warn.mockRestore();
});
it("ignores component elements (non-string type)", () => {
function CustomComponent() { return React.createElement("meta", { name: "custom" }); }
ReactDOMServer.renderToString(
React.createElement(Head, null,
React.createElement(CustomComponent),
),
);
const headHtml = getSSRHeadHTML();
// Component elements are ignored because child.type is not a string
expect(headHtml).toBe("");
});
it("keeps allowed tags while ignoring disallowed ones", () => {
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
ReactDOMServer.renderToString(
React.createElement(Head, null,
React.createElement("title", null, "Good"),
React.createElement("div", null, "Bad"),
React.createElement("meta", { name: "good" }),
),
);
const headHtml = getSSRHeadHTML();
expect(headHtml).toContain("Good");
expect(headHtml).toContain('name="good"');
expect(headHtml).not.toContain("<div");
warn.mockRestore();
});
});
// โโโ HTML/Attribute escaping โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
describe("Head escaping", () => {
beforeEach(() => {
resetSSRHead();
});
it("escapes HTML in text content", () => {
ReactDOMServer.renderToString(
React.createElement(Head, null,
React.createElement("title", null, 'Page <script>alert("xss")</script>'),
),
);
const headHtml = getSSRHeadHTML();
expect(headHtml).toContain("<script>");
expect(headHtml).not.toContain("<script>alert");
});
it("escapes HTML in attribute values", () => {
ReactDOMServer.renderToString(
React.createElement(Head, null,
React.createElement("meta", { name: 'test"value', content: "a<b>c&d" }),
),
);
const headHtml = getSSRHeadHTML();
expect(headHtml).toContain(""");
expect(headHtml).toContain("<");
expect(headHtml).toContain("&");
});
it("renders dangerouslySetInnerHTML raw on SSR", () => {
ReactDOMServer.renderToString(
React.createElement(Head, null,
React.createElement("script", {
dangerouslySetInnerHTML: { __html: 'console.log("hello")' },
}),
),
);
const headHtml = getSSRHeadHTML();
expect(headHtml).toContain('console.log("hello")');
});
it("converts className to class attribute", () => {
ReactDOMServer.renderToString(
React.createElement(Head, null,
React.createElement("style", { className: "critical" }, "body{}"),
),
);
const headHtml = getSSRHeadHTML();
expect(headHtml).toContain('class="critical"');
expect(headHtml).not.toContain("className");
});
it("renders boolean true attributes as bare attribute name", () => {
ReactDOMServer.renderToString(
React.createElement(Head, null,
React.createElement("script", { src: "/app.js", async: true, defer: true }),
),
);
const headHtml = getSSRHeadHTML();
expect(headHtml).toContain(" async ");
expect(headHtml).toContain(" defer ");
});
});
// โโโ escapeAttr utility โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
describe("escapeAttr", () => {
it("escapes ampersand", () => {
expect(escapeAttr("a&b")).toBe("a&b");
});
it("escapes double quotes", () => {
expect(escapeAttr('a"b')).toBe("a"b");
});
it("escapes angle brackets", () => {
expect(escapeAttr("a<b>c")).toBe("a<b>c");
});
it("returns safe strings unchanged", () => {
expect(escapeAttr("hello world")).toBe("hello world");
});
it("escapes all special chars together", () => {
expect(escapeAttr('&"<>')).toBe("&"<>");
});
});