@alepha/react 0.7.5 → 0.7.7

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.
@@ -1,43 +0,0 @@
1
- import type { Head } from "./ServerHeadProvider.ts";
2
-
3
- export class BrowserHeadProvider {
4
- renderHead(document: Document, head: Head): void {
5
- if (head.title) {
6
- document.title = head.title;
7
- }
8
-
9
- if (head.bodyAttributes) {
10
- for (const [key, value] of Object.entries(head.bodyAttributes)) {
11
- if (value) {
12
- document.body.setAttribute(key, value);
13
- } else {
14
- document.body.removeAttribute(key);
15
- }
16
- }
17
- }
18
-
19
- if (head.htmlAttributes) {
20
- for (const [key, value] of Object.entries(head.htmlAttributes)) {
21
- if (value) {
22
- document.documentElement.setAttribute(key, value);
23
- } else {
24
- document.documentElement.removeAttribute(key);
25
- }
26
- }
27
- }
28
-
29
- if (head.meta) {
30
- for (const [key, value] of Object.entries(head.meta)) {
31
- const meta = document.querySelector(`meta[name="${key}"]`);
32
- if (meta) {
33
- meta.setAttribute("content", value.content);
34
- } else {
35
- const newMeta = document.createElement("meta");
36
- newMeta.setAttribute("name", key);
37
- newMeta.setAttribute("content", value.content);
38
- document.head.appendChild(newMeta);
39
- }
40
- }
41
- }
42
- }
43
- }
@@ -1,91 +0,0 @@
1
- export interface Head {
2
- title?: string;
3
- htmlAttributes?: Record<string, string>;
4
- bodyAttributes?: Record<string, string>;
5
- meta?: Array<{ name: string; content: string }>;
6
- }
7
-
8
- export class ServerHeadProvider {
9
- renderHead(template: string, head: Head): string {
10
- let result = template;
11
-
12
- // Inject htmlAttributes
13
- const htmlAttributes = head.htmlAttributes;
14
- if (htmlAttributes) {
15
- result = result.replace(
16
- /<html([^>]*)>/i,
17
- (_, existingAttrs) =>
18
- `<html${this.mergeAttributes(existingAttrs, htmlAttributes)}>`,
19
- );
20
- }
21
-
22
- // Inject bodyAttributes
23
- const bodyAttributes = head.bodyAttributes;
24
- if (bodyAttributes) {
25
- result = result.replace(
26
- /<body([^>]*)>/i,
27
- (_, existingAttrs) =>
28
- `<body${this.mergeAttributes(existingAttrs, bodyAttributes)}>`,
29
- );
30
- }
31
-
32
- // Build head content
33
- let headContent = "";
34
- const title = head.title;
35
- if (title) {
36
- if (template.includes("<title>")) {
37
- result = result.replace(
38
- /<title>(.*?)<\/title>/i,
39
- () => `<title>${this.escapeHtml(title)}</title>`,
40
- );
41
- } else {
42
- headContent += `<title>${this.escapeHtml(title)}</title>\n`;
43
- }
44
- }
45
-
46
- if (head.meta) {
47
- for (const meta of head.meta) {
48
- headContent += `<meta name="${this.escapeHtml(meta.name)}" content="${this.escapeHtml(meta.content)}">\n`;
49
- }
50
- }
51
-
52
- // Inject into <head>...</head>
53
- result = result.replace(
54
- /<head([^>]*)>(.*?)<\/head>/is,
55
- (_, existingAttrs, existingHead) =>
56
- `<head${existingAttrs}>${existingHead}${headContent}</head>`,
57
- );
58
-
59
- return result.trim();
60
- }
61
-
62
- mergeAttributes(existing: string, attrs: Record<string, string>): string {
63
- const existingAttrs = this.parseAttributes(existing);
64
- const merged = { ...existingAttrs, ...attrs };
65
- return Object.entries(merged)
66
- .map(([k, v]) => ` ${k}="${this.escapeHtml(v)}"`)
67
- .join("");
68
- }
69
-
70
- parseAttributes(attrStr: string): Record<string, string> {
71
- const attrs: Record<string, string> = {};
72
- const attrRegex = /([^\s=]+)(?:="([^"]*)")?/g;
73
- let match: RegExpExecArray | null = attrRegex.exec(attrStr);
74
-
75
- while (match) {
76
- attrs[match[1]] = match[2] ?? "";
77
- match = attrRegex.exec(attrStr);
78
- }
79
-
80
- return attrs;
81
- }
82
-
83
- escapeHtml(str: string): string {
84
- return str
85
- .replace(/&/g, "&amp;")
86
- .replace(/</g, "&lt;")
87
- .replace(/>/g, "&gt;")
88
- .replace(/"/g, "&quot;")
89
- .replace(/'/g, "&#039;");
90
- }
91
- }