Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/__tests__/__snapshots__/atom1.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

exports[`atom 1.0 should generate a valid feed 1`] = `
"<?xml version=\\"1.0\\" encoding=\\"utf-8\\"?>
<feed xmlns=\\"http://www.w3.org/2005/Atom\\">
<feed xmlns=\\"http://www.w3.org/2005/Atom\\" xml:lang=\\"en\\">
<id>http://example.com/?link=sanitized&amp;value=4</id>
<title>Feed Title</title>
<updated>2013-07-13T23:00:00.000Z</updated>
Expand Down
3 changes: 2 additions & 1 deletion src/__tests__/__snapshots__/json.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

exports[`json 1 should generate a valid feed 1`] = `
"{
\\"version\\": \\"https://jsonfeed.org/version/1\\",
\\"version\\": \\"https://jsonfeed.org/version/1.1\\",
\\"title\\": \\"Feed Title\\",
\\"home_page_url\\": \\"http://example.com/?link=sanitized&value=3\\",
\\"feed_url\\": \\"http://example.com/sampleFeed.json?link=sanitized&value=5\\",
\\"description\\": \\"This is my personnal feed!\\",
\\"language\\": \\"en\\",
\\"icon\\": \\"http://example.com/image.png?link=sanitized&value=6\\",
\\"author\\": {
\\"name\\": \\"John Doe\\",
Expand Down
135 changes: 135 additions & 0 deletions src/__tests__/atom1.language.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
import { Feed } from "../feed";

describe("atom 1.0 language support", () => {
it("should generate a feed with xml:lang attribute when language is specified", () => {
const feed = new Feed({
title: "Test Feed",
description: "Test Description",
link: "http://example.com",
id: "http://example.com",
language: "de-DE",
copyright: "Test Copyright",
});

const actual = feed.atom1();
expect(actual).toContain('xml:lang="de-DE"');
expect(actual).toContain('<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="de-DE">');
});

it("should generate a feed without xml:lang attribute when language is not specified", () => {
const feed = new Feed({
title: "Test Feed",
description: "Test Description",
link: "http://example.com",
id: "http://example.com",
copyright: "Test Copyright",
});

const actual = feed.atom1();
expect(actual).not.toContain("xml:lang");
expect(actual).toContain('<feed xmlns="http://www.w3.org/2005/Atom">');
});

it("should handle different language codes correctly", () => {
const testCases = ["en", "fr", "es-ES", "zh-CN", "ja-JP", "pt-BR"];

testCases.forEach((language) => {
const feed = new Feed({
title: "Test Feed",
description: "Test Description",
link: "http://example.com",
id: "http://example.com",
language,
copyright: "Test Copyright",
});

const actual = feed.atom1();
expect(actual).toContain(`xml:lang="${language}"`);
});
});

it("should sanitize language attribute like other attributes", () => {
const feed = new Feed({
title: "Test Feed",
description: "Test Description",
link: "http://example.com",
id: "http://example.com",
language: "en&test=malicious",
copyright: "Test Copyright",
});

const actual = feed.atom1();
// The language should be sanitized to prevent XML injection
expect(actual).toContain('xml:lang="en&amp;test=malicious"');
// Verify the XML is still valid by checking the structure
expect(actual).toContain('<feed xmlns="http://www.w3.org/2005/Atom"');
});

it("should support per-item language attributes", () => {
const feed = new Feed({
title: "Test Feed",
description: "Test Description",
link: "http://example.com",
id: "http://example.com",
language: "en",
copyright: "Test Copyright",
});

feed.addItem({
title: "Article in English",
link: "http://example.com/english",
date: new Date("2023-01-01"),
});

feed.addItem({
title: "Article en Français",
link: "http://example.com/french",
date: new Date("2023-01-02"),
language: "fr-FR",
});

feed.addItem({
title: "Artículo en Español",
link: "http://example.com/spanish",
date: new Date("2023-01-03"),
language: "es-ES",
});

const actual = feed.atom1();

// Feed should have default language
expect(actual).toContain('<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">');

// First entry should not have xml:lang (inherits from feed)
const firstEntryMatch = actual.match(/<entry>\s*<title[^>]*><!\[CDATA\[Article in English\]\]><\/title>/);
expect(firstEntryMatch).toBeTruthy();

// Second entry should have French language
expect(actual).toContain('<entry xml:lang="fr-FR">');
expect(actual).toContain("<![CDATA[Article en Français]]>");

// Third entry should have Spanish language
expect(actual).toContain('<entry xml:lang="es-ES">');
expect(actual).toContain("<![CDATA[Artículo en Español]]>");
});

it("should sanitize per-item language attributes", () => {
const feed = new Feed({
title: "Test Feed",
description: "Test Description",
link: "http://example.com",
id: "http://example.com",
copyright: "Test Copyright",
});

feed.addItem({
title: "Test Article",
link: "http://example.com/test",
date: new Date("2023-01-01"),
language: "fr&malicious=code",
});

const actual = feed.atom1();
expect(actual).toContain('<entry xml:lang="fr&amp;malicious=code">');
});
});
115 changes: 115 additions & 0 deletions src/__tests__/json.language.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import { Feed } from "../feed";

describe("json feed language support", () => {
it("should generate a feed with language when specified", () => {
const feed = new Feed({
title: "Test Feed",
description: "Test Description",
link: "http://example.com",
id: "http://example.com",
language: "fr-FR",
copyright: "Test Copyright",
});

const actual = JSON.parse(feed.json1());
expect(actual.language).toBe("fr-FR");
expect(actual.version).toBe("https://jsonfeed.org/version/1.1");
});

it("should generate a feed without language when not specified", () => {
const feed = new Feed({
title: "Test Feed",
description: "Test Description",
link: "http://example.com",
id: "http://example.com",
copyright: "Test Copyright",
});

const actual = JSON.parse(feed.json1());
expect(actual.language).toBeUndefined();
expect(actual.version).toBe("https://jsonfeed.org/version/1.1");
});

it("should handle different language codes correctly", () => {
const testCases = ["en", "de", "ja-JP", "zh-CN", "pt-BR", "es-ES"];

testCases.forEach((language) => {
const feed = new Feed({
title: "Test Feed",
description: "Test Description",
link: "http://example.com",
id: "http://example.com",
language,
copyright: "Test Copyright",
});

const actual = JSON.parse(feed.json1());
expect(actual.language).toBe(language);
});
});

it("should support per-item language", () => {
const feed = new Feed({
title: "Test Feed",
description: "Test Description",
link: "http://example.com",
id: "http://example.com",
language: "en",
copyright: "Test Copyright",
});

feed.addItem({
title: "Article in English",
link: "http://example.com/english",
date: new Date("2023-01-01"),
});

feed.addItem({
title: "Article en Français",
link: "http://example.com/french",
date: new Date("2023-01-02"),
language: "fr-FR",
});

feed.addItem({
title: "Artículo en Español",
link: "http://example.com/spanish",
date: new Date("2023-01-03"),
language: "es-ES",
});

const actual = JSON.parse(feed.json1());

// Feed should have default language
expect(actual.language).toBe("en");

// Check items
expect(actual.items).toHaveLength(3);

// First item should not have language (inherits from feed)
expect(actual.items[0].language).toBeUndefined();
expect(actual.items[0].title).toBe("Article in English");

// Second item should have French language
expect(actual.items[1].language).toBe("fr-FR");
expect(actual.items[1].title).toBe("Article en Français");

// Third item should have Spanish language
expect(actual.items[2].language).toBe("es-ES");
expect(actual.items[2].title).toBe("Artículo en Español");
});

it("should handle special characters in language codes", () => {
const feed = new Feed({
title: "Test Feed",
description: "Test Description",
link: "http://example.com",
id: "http://example.com",
language: "zh-Hans-CN", // Traditional Chinese (Simplified) in China
copyright: "Test Copyright",
});

const actual = JSON.parse(feed.json1());
expect(actual.language).toBe("zh-Hans-CN");
});
});
84 changes: 84 additions & 0 deletions src/__tests__/rss2.language.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { Feed } from "../feed";

describe("rss2 language support", () => {
it("should generate a feed with language element when specified", () => {
const feed = new Feed({
title: "Test Feed",
description: "Test Description",
link: "http://example.com",
id: "http://example.com",
language: "de-DE",
copyright: "Test Copyright",
});

const actual = feed.rss2();
expect(actual).toContain("<language>de-DE</language>");
});

it("should generate a feed without language element when not specified", () => {
const feed = new Feed({
title: "Test Feed",
description: "Test Description",
link: "http://example.com",
id: "http://example.com",
copyright: "Test Copyright",
});

const actual = feed.rss2();
expect(actual).not.toContain("<language>");
});

it("should handle different language codes correctly", () => {
const testCases = ["en", "fr", "es-ES", "zh-CN", "ja-JP", "pt-BR"];

testCases.forEach((language) => {
const feed = new Feed({
title: "Test Feed",
description: "Test Description",
link: "http://example.com",
id: "http://example.com",
language,
copyright: "Test Copyright",
});

const actual = feed.rss2();
expect(actual).toContain(`<language>${language}</language>`);
});
});

it("should verify RSS2 does not support per-item language (channel-level only)", () => {
const feed = new Feed({
title: "Test Feed",
description: "Test Description",
link: "http://example.com",
id: "http://example.com",
language: "en",
copyright: "Test Copyright",
});

feed.addItem({
title: "Article in English",
link: "http://example.com/english",
date: new Date("2023-01-01"),
});

feed.addItem({
title: "Article en Français",
link: "http://example.com/french",
date: new Date("2023-01-02"),
language: "fr-FR", // This should be ignored for RSS2
});

const actual = feed.rss2();

// Feed should have channel-level language
expect(actual).toContain("<language>en</language>");

// RSS2 does not support per-item language, so items should not have language elements
expect(actual).not.toContain("<language>fr-FR</language>");

// Verify items are present but without language
expect(actual).toContain("<![CDATA[Article in English]]>");
expect(actual).toContain("<![CDATA[Article en Français]]>");
});
});
14 changes: 13 additions & 1 deletion src/atom1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,17 @@ import { sanitize } from "./utils";
export default (ins: Feed) => {
const { options } = ins;

const feedAttrs: Record<string, string> = { xmlns: "http://www.w3.org/2005/Atom" };
if (ins.options.language) {
// Atom uses the reserved "xml:" namespace for language;
// no extra xmlns declaration is required.
feedAttrs["xml:lang"] = sanitize(ins.options.language) || ins.options.language;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why fall back to the unsanitized value if the sanitization fails or returns a falsy value? Wouldn't it be better to throw an error, or log a warning and omit the language field instead?

}

const base: any = {
_declaration: { _attributes: { version: "1.0", encoding: "utf-8" } },
feed: {
_attributes: { xmlns: "http://www.w3.org/2005/Atom" },
_attributes: feedAttrs,
id: options.id,
title: options.title,
updated: options.updated ? options.updated.toISOString() : new Date().toISOString(),
Expand Down Expand Up @@ -96,6 +103,11 @@ export default (ins: Feed) => {
updated: item.date.toISOString(),
};

// Add xml:lang attribute if language is specified for this item
if (item.language) {
entry._attributes = { "xml:lang": sanitize(item.language) || item.language };
}

//
// entry: recommended elements
//
Expand Down
Loading