cloudDialogDescription.test.ts 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // @vitest-environment jsdom
  2. import { cleanup, fireEvent, render, screen } from "@testing-library/react";
  3. import { createElement } from "react";
  4. import { afterEach, describe, expect, it, vi } from "vitest";
  5. import { CloudDialogDescription } from "@/components/cloud-content-dialog/CloudDialogDescription.js";
  6. afterEach(cleanup);
  7. describe("简单 HTML 描述", () => {
  8. it("renders Markdown structure and rejects embedded active content", () => {
  9. const { container } = render(
  10. createElement(CloudDialogDescription, {
  11. description: {
  12. format: "markdown",
  13. text: "## 标题\n\n**强调** 与 `代码`\n\n- 第一项\n- 第二项\n\n<script>bad()</script>\n\n![image](https://example.com/a.png)",
  14. },
  15. }),
  16. );
  17. expect(container.querySelector("h2")?.textContent).toBe("标题");
  18. expect(container.querySelector("strong")?.textContent).toBe("强调");
  19. expect(container.querySelectorAll("li")).toHaveLength(2);
  20. expect(container.querySelector("script,img")).toBeNull();
  21. });
  22. it.each([
  23. "color:rgb(20, 30, 40)",
  24. "background-color:#abcdef",
  25. "font-style:italic",
  26. "font-size:var(--text-ui-lg)",
  27. "font-weight:500",
  28. "line-height:1.5",
  29. "text-align:center",
  30. "white-space:nowrap",
  31. "overflow-wrap:anywhere",
  32. "word-break:break-all",
  33. "text-decoration:underline dashed",
  34. "text-decoration-line:underline",
  35. "text-decoration-style:dotted",
  36. "text-decoration-color:var(--color-foreground-subtle)",
  37. "text-decoration-thickness:2px",
  38. "text-underline-offset:4px",
  39. "letter-spacing:1px",
  40. "padding:2px 4px",
  41. "margin-left:1rem",
  42. ])("保留合法排版 %s", (style) => {
  43. const { container } = render(
  44. createElement(CloudDialogDescription, {
  45. description: { format: "html", text: `<span style="${style}">内容</span>` },
  46. }),
  47. );
  48. expect(container.querySelector("span")!.getAttribute("style")).toBeTruthy();
  49. });
  50. it("链接降级为文字时仍保留 class 和合法 style", () => {
  51. const { container } = render(
  52. createElement(CloudDialogDescription, {
  53. description: {
  54. format: "html",
  55. text: '<a href="https://example.com" class="font-semibold unknown" style="margin-left:8px">链接</a>',
  56. },
  57. }),
  58. );
  59. expect(container.querySelector("a")).toBeNull();
  60. expect(container.querySelector("span")!.className).toBe("font-semibold unknown");
  61. expect(container.querySelector("span")!.style.marginLeft).toBe("8px");
  62. });
  63. it("保留基础排版和安全链接,点击交给宿主", () => {
  64. const openExternal = vi.fn();
  65. const { container } = render(
  66. createElement(CloudDialogDescription, {
  67. description: {
  68. format: "html",
  69. text: '<p>你好 <b>权益</b><br><em>说明</em></p><ul><li><code>code</code></li></ul><a href="https://example.com/help" title="帮助">帮助</a>',
  70. },
  71. onOpenExternal: openExternal,
  72. }),
  73. );
  74. expect(container.querySelector("b")?.textContent).toBe("权益");
  75. expect(container.querySelector("li code")?.textContent).toBe("code");
  76. fireEvent.click(screen.getByRole("link"));
  77. expect(openExternal).toHaveBeenCalledWith("https://example.com/help");
  78. });
  79. it("删除可执行内容、危险样式和危险链接,但保留未知 class", () => {
  80. const { container } = render(
  81. createElement(CloudDialogDescription, {
  82. description: {
  83. format: "html",
  84. text: '<script>alert(1)</script><iframe src="https://example.com"></iframe><svg onload="alert(1)"><a>SVG</a></svg><img src=x onerror="alert(1)"><p style="position:fixed" class="bad" id="bad" onclick="alert(1)">保留</p><a href="java&#x73;cript:alert(1)">危险</a><a href="data:text/html,test">data</a><a href="//example.com">relative</a>',
  85. },
  86. onOpenExternal: vi.fn(),
  87. }),
  88. );
  89. expect(container.querySelector("script,iframe,svg,img,[style],[onclick],[id=bad]")).toBeNull();
  90. expect(container.querySelector(".bad")?.textContent).toBe("保留");
  91. expect(container.querySelector("a")).toBeNull();
  92. expect(container.textContent).toContain("保留");
  93. expect(container.textContent).not.toContain("alert");
  94. });
  95. it("合并 Tailwind 类且保留允许的 inline style,删除危险声明", () => {
  96. const { container } = render(
  97. createElement(CloudDialogDescription, {
  98. description: {
  99. format: "html",
  100. text: '<p class="text-center text-ui-base/relaxed remote-unknown" style="margin:8px 1rem;position:fixed;background-image:url(https://example.com);--custom:red"><b class="font-normal" style="font-weight:600;color:var(--color-foreground);font-size:var(--text-ui-base)">模型</b><span class="whitespace-nowrap font-semibold text-foreground underline decoration-foreground-subtle decoration-dashed underline-offset-4" style="text-decoration:underline dashed;text-underline-offset:4px">日期</span></p>',
  101. },
  102. }),
  103. );
  104. const p = container.querySelector("p")!;
  105. expect(p.classList.contains("remote-unknown")).toBe(true);
  106. expect(p.style.margin).toBe("8px 1rem");
  107. expect(p.style.position).toBe("");
  108. expect(p.style.backgroundImage).toBe("");
  109. expect(p.style.getPropertyValue("--custom")).toBe("");
  110. const b = container.querySelector("b")!;
  111. expect(b.classList.contains("font-normal")).toBe(true);
  112. expect(b.classList.contains("font-semibold")).toBe(false);
  113. expect(b.style.fontWeight).toBe("600");
  114. expect(b.style.color).toBe("var(--color-foreground)");
  115. expect(b.style.fontSize).toBe("var(--text-ui-base)");
  116. expect(container.querySelector("span")!.style.textUnderlineOffset).toBe("4px");
  117. });
  118. it.each([
  119. "margin:-1px",
  120. "padding:99999px",
  121. "color:var(--unknown)",
  122. "color:var(--color-foreground,red)",
  123. "font-size:90px",
  124. "line-height:999",
  125. "font-weight:600!important",
  126. "text-decoration:underline url(x)",
  127. "letter-spacing:calc(1px + 2px)",
  128. ])("拒绝不支持的样式 %s", (style) => {
  129. const { container } = render(
  130. createElement(CloudDialogDescription, {
  131. description: { format: "html", text: `<span style="${style}">内容</span>` },
  132. }),
  133. );
  134. expect(container.querySelector("span")!.getAttribute("style")).toBeNull();
  135. });
  136. it("纯文本与 Markdown 内嵌 HTML 不解释为 HTML", () => {
  137. for (const format of ["plain_text", "markdown"] as const) {
  138. const { container, unmount } = render(
  139. createElement(CloudDialogDescription, {
  140. description: { format, text: "<b>literal</b>" },
  141. }),
  142. );
  143. expect(container.querySelector("b")).toBeNull();
  144. expect(container.textContent).toBe("<b>literal</b>");
  145. unmount();
  146. }
  147. });
  148. it("无平台回调时链接为文字,超限内容退化为纯文本", () => {
  149. const { container, rerender } = render(
  150. createElement(CloudDialogDescription, {
  151. description: { format: "html", text: '<a href="https://example.com">帮助</a>' },
  152. }),
  153. );
  154. expect(container.querySelector("a")).toBeNull();
  155. const text = "<b>" + "x".repeat(20_000) + "</b>";
  156. rerender(createElement(CloudDialogDescription, { description: { format: "html", text } }));
  157. expect(container.querySelector("b")).toBeNull();
  158. expect(container.textContent).toBe(text);
  159. const deep = "<span>".repeat(40) + "text" + "</span>".repeat(40);
  160. rerender(
  161. createElement(CloudDialogDescription, { description: { format: "html", text: deep } }),
  162. );
  163. expect(container.querySelector("span")).toBeNull();
  164. expect(container.textContent).toBe(deep);
  165. });
  166. });