@unhead/ssr 1.3.5 → 1.3.8

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.
package/README.md CHANGED
@@ -9,4 +9,4 @@ npm install @unhead/ssr
9
9
 
10
10
  ## Documentation
11
11
 
12
- See the [Unhead](https://unhead.harlanzw.com/) for more details.
12
+ See the [Unhead](https://unhead.unjs.io/) for more details.
package/dist/index.cjs CHANGED
@@ -15,7 +15,7 @@ function propsToString(props) {
15
15
  return handledAttributes.length > 0 ? ` ${handledAttributes.join(" ")}` : "";
16
16
  }
17
17
 
18
- function encodeInnerHtml(str) {
18
+ function escapeHtml(str) {
19
19
  return str.replace(/[&<>"'/]/g, (char) => {
20
20
  switch (char) {
21
21
  case "&":
@@ -35,6 +35,9 @@ function encodeInnerHtml(str) {
35
35
  }
36
36
  });
37
37
  }
38
+ function escapeJson(str) {
39
+ return str.replace(/</g, "\\u003C");
40
+ }
38
41
  function tagToString(tag) {
39
42
  const attrs = propsToString(tag.props);
40
43
  const openTag = `<${tag.tag}${attrs}>`;
@@ -42,7 +45,9 @@ function tagToString(tag) {
42
45
  return shared.SelfClosingTags.includes(tag.tag) ? openTag : `${openTag}</${tag.tag}>`;
43
46
  let content = String(tag.innerHTML || "");
44
47
  if (tag.textContent)
45
- content = encodeInnerHtml(String(tag.textContent));
48
+ content = escapeHtml(String(tag.textContent));
49
+ if (tag.innerHTML && ["application/ld+json", "application/json"].includes(tag.props.type))
50
+ tag.innerHTML = escapeJson(tag.innerHTML);
46
51
  return shared.SelfClosingTags.includes(tag.tag) ? openTag : `${openTag}${content}</${tag.tag}>`;
47
52
  }
48
53
 
@@ -84,7 +89,8 @@ async function renderSSRHead(head) {
84
89
  return renderCtx.html;
85
90
  }
86
91
 
87
- exports.encodeInnerHtml = encodeInnerHtml;
92
+ exports.escapeHtml = escapeHtml;
93
+ exports.escapeJson = escapeJson;
88
94
  exports.propsToString = propsToString;
89
95
  exports.renderSSRHead = renderSSRHead;
90
96
  exports.ssrRenderTags = ssrRenderTags;
package/dist/index.d.ts CHANGED
@@ -5,7 +5,8 @@ declare function renderSSRHead<T extends {}>(head: Unhead<T>): Promise<SSRHeadPa
5
5
 
6
6
  declare function propsToString(props: Record<string, any>): string;
7
7
 
8
- declare function encodeInnerHtml(str: string): string;
8
+ declare function escapeHtml(str: string): string;
9
+ declare function escapeJson(str: string): string;
9
10
  declare function tagToString<T extends HeadTag>(tag: T): string;
10
11
 
11
12
  declare function ssrRenderTags<T extends HeadTag>(tags: T[]): {
@@ -16,4 +17,4 @@ declare function ssrRenderTags<T extends HeadTag>(tags: T[]): {
16
17
  bodyAttrs: string;
17
18
  };
18
19
 
19
- export { encodeInnerHtml, propsToString, renderSSRHead, ssrRenderTags, tagToString };
20
+ export { escapeHtml, escapeJson, propsToString, renderSSRHead, ssrRenderTags, tagToString };
package/dist/index.mjs CHANGED
@@ -13,7 +13,7 @@ function propsToString(props) {
13
13
  return handledAttributes.length > 0 ? ` ${handledAttributes.join(" ")}` : "";
14
14
  }
15
15
 
16
- function encodeInnerHtml(str) {
16
+ function escapeHtml(str) {
17
17
  return str.replace(/[&<>"'/]/g, (char) => {
18
18
  switch (char) {
19
19
  case "&":
@@ -33,6 +33,9 @@ function encodeInnerHtml(str) {
33
33
  }
34
34
  });
35
35
  }
36
+ function escapeJson(str) {
37
+ return str.replace(/</g, "\\u003C");
38
+ }
36
39
  function tagToString(tag) {
37
40
  const attrs = propsToString(tag.props);
38
41
  const openTag = `<${tag.tag}${attrs}>`;
@@ -40,7 +43,9 @@ function tagToString(tag) {
40
43
  return SelfClosingTags.includes(tag.tag) ? openTag : `${openTag}</${tag.tag}>`;
41
44
  let content = String(tag.innerHTML || "");
42
45
  if (tag.textContent)
43
- content = encodeInnerHtml(String(tag.textContent));
46
+ content = escapeHtml(String(tag.textContent));
47
+ if (tag.innerHTML && ["application/ld+json", "application/json"].includes(tag.props.type))
48
+ tag.innerHTML = escapeJson(tag.innerHTML);
44
49
  return SelfClosingTags.includes(tag.tag) ? openTag : `${openTag}${content}</${tag.tag}>`;
45
50
  }
46
51
 
@@ -82,4 +87,4 @@ async function renderSSRHead(head) {
82
87
  return renderCtx.html;
83
88
  }
84
89
 
85
- export { encodeInnerHtml, propsToString, renderSSRHead, ssrRenderTags, tagToString };
90
+ export { escapeHtml, escapeJson, propsToString, renderSSRHead, ssrRenderTags, tagToString };
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "@unhead/ssr",
3
3
  "type": "module",
4
- "version": "1.3.5",
4
+ "version": "1.3.8",
5
5
  "author": "Harlan Wilton <harlan@harlanzw.com>",
6
6
  "license": "MIT",
7
7
  "funding": "https://github.com/sponsors/harlan-zw",
8
- "homepage": "https://unhead.harlanzw.com",
8
+ "homepage": "https://unhead.unjs.io",
9
9
  "repository": {
10
10
  "type": "git",
11
11
  "url": "git+https://github.com/unjs/unhead.git",
@@ -29,8 +29,8 @@
29
29
  "dist"
30
30
  ],
31
31
  "dependencies": {
32
- "@unhead/schema": "1.3.5",
33
- "@unhead/shared": "1.3.5"
32
+ "@unhead/schema": "1.3.8",
33
+ "@unhead/shared": "1.3.8"
34
34
  },
35
35
  "scripts": {
36
36
  "build": "unbuild .",