-
-
Notifications
You must be signed in to change notification settings - Fork 218
feat(atom/rss/json): support language and on feed and feed items (#196) #226
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
marcojakob
wants to merge
1
commit into
jpmonette:master
Choose a base branch
from
marcojakob:feat/atom-xml-lang
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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&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&malicious=code">'); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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]]>"); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
languagefield instead?