|
1 | | -import { describe, it, expect, vi, beforeEach } from "vitest"; |
| 1 | +import { describe, it, expect, vi, beforeEach, afterEach } from "vitest"; |
2 | 2 | import handleBuilderInteraction from "../mouseClick"; |
3 | 3 | import { VisualBuilder } from "../../index"; |
4 | 4 | import { FieldSchemaMap } from "../../utils/fieldSchemaMap"; |
@@ -250,3 +250,122 @@ describe("handleBuilderInteraction — pauseFeedback guard", () => { |
250 | 250 | expect(generateThread).toHaveBeenCalled(); |
251 | 251 | }); |
252 | 252 | }); |
| 253 | + |
| 254 | +describe("handleBuilderInteraction — alt+click on anchor", () => { |
| 255 | + const originalLocation = window.location; |
| 256 | + |
| 257 | + beforeEach(() => { |
| 258 | + Object.defineProperty(window, "location", { |
| 259 | + value: { href: "" }, |
| 260 | + writable: true, |
| 261 | + }); |
| 262 | + }); |
| 263 | + |
| 264 | + afterEach(() => { |
| 265 | + Object.defineProperty(window, "location", { |
| 266 | + value: originalLocation, |
| 267 | + writable: true, |
| 268 | + }); |
| 269 | + }); |
| 270 | + |
| 271 | + it("prevents default/propagation and drives navigation itself instead of relying on the native click", async () => { |
| 272 | + document.body.innerHTML = ""; |
| 273 | + const anchor = document.createElement("a"); |
| 274 | + anchor.href = "https://example.com/blog#anchor"; |
| 275 | + document.body.appendChild(anchor); |
| 276 | + |
| 277 | + const params = makeParams(anchor); |
| 278 | + Object.defineProperty(params.event, "altKey", { value: true }); |
| 279 | + const preventDefaultSpy = vi.spyOn(params.event, "preventDefault"); |
| 280 | + const stopPropagationSpy = vi.spyOn(params.event, "stopPropagation"); |
| 281 | + |
| 282 | + await handleBuilderInteraction(params); |
| 283 | + |
| 284 | + expect(preventDefaultSpy).toHaveBeenCalled(); |
| 285 | + expect(stopPropagationSpy).toHaveBeenCalled(); |
| 286 | + expect(window.location.href).toBe("https://example.com/blog#anchor"); |
| 287 | + expect(getCsDataOfElement).not.toHaveBeenCalled(); |
| 288 | + }); |
| 289 | + |
| 290 | + it("does nothing on alt+click when the target isn't an anchor", async () => { |
| 291 | + const editableElement = makeEditableElement(); |
| 292 | + const params = makeParams(editableElement); |
| 293 | + Object.defineProperty(params.event, "altKey", { value: true }); |
| 294 | + const preventDefaultSpy = vi.spyOn(params.event, "preventDefault"); |
| 295 | + |
| 296 | + await handleBuilderInteraction(params); |
| 297 | + |
| 298 | + expect(preventDefaultSpy).not.toHaveBeenCalled(); |
| 299 | + expect(window.location.href).toBe(""); |
| 300 | + expect(getCsDataOfElement).not.toHaveBeenCalled(); |
| 301 | + }); |
| 302 | + |
| 303 | + it("opens target=_blank links (RTE 'open in new tab') in a new tab instead of hijacking the iframe", async () => { |
| 304 | + document.body.innerHTML = ""; |
| 305 | + const anchor = document.createElement("a"); |
| 306 | + anchor.href = "https://example.com/other-entry"; |
| 307 | + anchor.target = "_blank"; |
| 308 | + document.body.appendChild(anchor); |
| 309 | + |
| 310 | + const openSpy = vi.spyOn(window, "open").mockImplementation(() => null); |
| 311 | + const params = makeParams(anchor); |
| 312 | + Object.defineProperty(params.event, "altKey", { value: true }); |
| 313 | + |
| 314 | + await handleBuilderInteraction(params); |
| 315 | + |
| 316 | + expect(openSpy).toHaveBeenCalledWith( |
| 317 | + "https://example.com/other-entry", |
| 318 | + "_blank", |
| 319 | + "noopener,noreferrer" |
| 320 | + ); |
| 321 | + expect(window.location.href).toBe(""); |
| 322 | + openSpy.mockRestore(); |
| 323 | + }); |
| 324 | + |
| 325 | + it("resolves the anchor ancestor when the click lands on nested formatted text inside the link", async () => { |
| 326 | + document.body.innerHTML = ""; |
| 327 | + const anchor = document.createElement("a"); |
| 328 | + anchor.href = "https://example.com/bold-link"; |
| 329 | + const bold = document.createElement("strong"); |
| 330 | + bold.textContent = "click me"; |
| 331 | + anchor.appendChild(bold); |
| 332 | + document.body.appendChild(anchor); |
| 333 | + |
| 334 | + const params = makeParams(bold); |
| 335 | + Object.defineProperty(params.event, "altKey", { value: true }); |
| 336 | + const preventDefaultSpy = vi.spyOn(params.event, "preventDefault"); |
| 337 | + |
| 338 | + await handleBuilderInteraction(params); |
| 339 | + |
| 340 | + expect(preventDefaultSpy).toHaveBeenCalled(); |
| 341 | + expect(window.location.href).toBe("https://example.com/bold-link"); |
| 342 | + }); |
| 343 | + |
| 344 | + it("blocks unsafe url schemes like javascript: on alt+click", async () => { |
| 345 | + document.body.innerHTML = ""; |
| 346 | + const anchor = document.createElement("a"); |
| 347 | + anchor.href = "javascript:alert(1)"; |
| 348 | + document.body.appendChild(anchor); |
| 349 | + |
| 350 | + const params = makeParams(anchor); |
| 351 | + Object.defineProperty(params.event, "altKey", { value: true }); |
| 352 | + |
| 353 | + await handleBuilderInteraction(params); |
| 354 | + |
| 355 | + expect(window.location.href).toBe(""); |
| 356 | + }); |
| 357 | + |
| 358 | + it("allows relative hrefs (resolve to the page's own http/https scheme)", async () => { |
| 359 | + document.body.innerHTML = ""; |
| 360 | + const anchor = document.createElement("a"); |
| 361 | + anchor.href = "/other-entry"; |
| 362 | + document.body.appendChild(anchor); |
| 363 | + |
| 364 | + const params = makeParams(anchor); |
| 365 | + Object.defineProperty(params.event, "altKey", { value: true }); |
| 366 | + |
| 367 | + await handleBuilderInteraction(params); |
| 368 | + |
| 369 | + expect(window.location.href).toBe(anchor.href); |
| 370 | + }); |
| 371 | +}); |
0 commit comments