Openverse is a search engine for openly-licensed media. This monorepo includes all application code.
Fix XML attribution output (#5662)
Nani-2007-codefe9d1af4ac84
diff --git a/frontend/shared/utils/attribution-html.ts b/frontend/shared/utils/attribution-html.tsindex 91d422c38..6bf483c38 100644--- a/frontend/shared/utils/attribution-html.ts+++ b/frontend/shared/utils/attribution-html.ts@@ -156,19 +156,29 @@ const extLink = (href: string, text: string) =>h("a", { rel: "noopener noreferrer", href: sanitizeHref(href) }, [text])/**- * Generate a Dublin Core conforming XML attribution snippet.+ * Generate a Dublin Core conforming XML attribution snippet. Elements whose+ * values are not known are omitted.** @param mediaItem - the media item being attributed* @returns the XML markup of the attribution*/-const xmlAttribution = (mediaItem: Record<string, string>) =>- `<attribution xmlns:dc="http://purl.org/dc/elements/1.1/">- <dc:creator>${mediaItem.creator}</dc:creator>- <dc:title>${mediaItem.title}</dc:title>- <dc:rights>${mediaItem.license_url}</dc:rights>- <dc:identifier>${mediaItem.foreign_landing_url}</dc:identifier>- <dc:type>${mediaItem.type}</dc:type>-</attribution>`+const xmlAttribution = (mediaItem: Record<string, string | undefined>) => {+ const dcElements = {+ creator: mediaItem.creator,+ title: mediaItem.title,+ rights: mediaItem.license_url,+ identifier: mediaItem.foreign_landing_url,+ type: mediaItem.type,+ }+ const lines = Object.entries(dcElements)+ .filter((entry): entry is [string, string] => Boolean(entry[1]))+ .map(([name, value]) => `\t<dc:${name}>${escapeHtml(value)}</dc:${name}>`)+ return [+ '<attribution xmlns:dc="http://purl.org/dc/elements/1.1/">',+ ...lines,+ "</attribution>",+ ].join("\n")+}/* Interfaces */@@ -307,6 +317,7 @@ export const getAttribution = (const xmlAttributionParts = {...restMediaItem,+ title: mediaItem.originalTitle || tFn("genericTitle"),type: frontendMediaType === "audio" ? "Sound" : "StillImage",}diff --git a/frontend/test/unit/specs/utils/attribution-html.spec.ts b/frontend/test/unit/specs/utils/attribution-html.spec.tsindex 75e113bea..4be26a059 100644--- a/frontend/test/unit/specs/utils/attribution-html.spec.ts+++ b/frontend/test/unit/specs/utils/attribution-html.spec.ts@@ -151,4 +151,46 @@ describe("getAttribution", () => {})expect(document.getElementsByTagName("img")).toHaveLength(0)})+ it("returns XML attribution with the correct title", () => {+ const xml = getAttribution(mediaItem, t, { isXml: true })++ expect(xml).toContain("<dc:title>Title</dc:title>")+ expect(xml).not.toContain("<dc:title>undefined</dc:title>")+ })++ it("uses the generic title in XML when the title is missing", () => {+ const mediaItemNoTitle = { ...mediaItem, originalTitle: "" }+ const xml = getAttribution(mediaItemNoTitle, t, { isXml: true })++ expect(xml).toContain("<dc:title>This work</dc:title>")+ })++ it("omits unknown elements from the XML attribution", () => {+ const mediaItemNoCreator = {+ ...mediaItem,+ creator: undefined,+ license_url: undefined,+ }+ const xml = getAttribution(mediaItemNoCreator, t, { isXml: true })++ expect(xml).not.toContain("dc:creator")+ expect(xml).not.toContain("dc:rights")+ expect(xml).not.toContain("undefined")+ expect(xml).toContain("<dc:title>Title</dc:title>")+ })++ it("escapes special characters in XML attribution", () => {+ const mediaItemWithSpecialCharacters = {+ ...mediaItem,+ originalTitle: "Cats & Dogs <Pets>",+ creator: "A & B",+ }++ const xml = getAttribution(mediaItemWithSpecialCharacters, t, {+ isXml: true,+ })++ expect(xml).toContain("<dc:title>Cats & Dogs <Pets></dc:title>")+ expect(xml).toContain("<dc:creator>A & B</dc:creator>")+ })})