From a400434961e8165580e8f6731795154b7da09ad9 Mon Sep 17 00:00:00 2001 From: ephraimduncan Date: Fri, 31 Jul 2026 14:55:19 +0000 Subject: [PATCH] fix(elements): prevent invalid citation URL from crashing message render --- .../__tests__/inline-citation.test.tsx | 33 +++++++++++++++++++ packages/elements/src/inline-citation.tsx | 7 ++-- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/packages/elements/__tests__/inline-citation.test.tsx b/packages/elements/__tests__/inline-citation.test.tsx index 601d47ae..3ff59966 100644 --- a/packages/elements/__tests__/inline-citation.test.tsx +++ b/packages/elements/__tests__/inline-citation.test.tsx @@ -92,6 +92,39 @@ describe("inlineCitationCardTrigger", () => { ); expect(screen.getByText("unknown")).toBeInTheDocument(); }); + + it("renders raw source for a bare domain without a scheme", () => { + expect(() => + render( + + + + ) + ).not.toThrow(); + expect(screen.getByText("example.com/nlp")).toBeInTheDocument(); + }); + + it("renders raw source for a relative path", () => { + expect(() => + render( + + + + ) + ).not.toThrow(); + expect(screen.getByText("/nlp-advances")).toBeInTheDocument(); + }); + + it("renders raw source for a non-URL string", () => { + expect(() => + render( + + + + ) + ).not.toThrow(); + expect(screen.getByText("not a url")).toBeInTheDocument(); + }); }); describe("inlineCitationCardBody", () => { diff --git a/packages/elements/src/inline-citation.tsx b/packages/elements/src/inline-citation.tsx index 171c81cf..3789ac15 100644 --- a/packages/elements/src/inline-citation.tsx +++ b/packages/elements/src/inline-citation.tsx @@ -1,7 +1,9 @@ "use client"; -import { Badge } from "@repo/shadcn-ui/components/ui/badge"; import type { CarouselApi } from "@repo/shadcn-ui/components/ui/carousel"; +import type { ComponentProps } from "react"; + +import { Badge } from "@repo/shadcn-ui/components/ui/badge"; import { Carousel, CarouselContent, @@ -14,7 +16,6 @@ import { } from "@repo/shadcn-ui/components/ui/hover-card"; import { cn } from "@repo/shadcn-ui/lib/utils"; import { ArrowLeftIcon, ArrowRightIcon } from "lucide-react"; -import type { ComponentProps } from "react"; import { createContext, useCallback, @@ -70,7 +71,7 @@ export const InlineCitationCardTrigger = ({ > {sources[0] ? ( <> - {new URL(sources[0]).hostname}{" "} + {URL.canParse(sources[0]) ? new URL(sources[0]).hostname : sources[0]}{" "} {sources.length > 1 && `+${sources.length - 1}`} ) : (