Skip to content
Merged
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
22 changes: 22 additions & 0 deletions src/__tests__/atom1.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,28 @@ describe("atom 1.0", () => {
expect(actual).toContain('<link rel="enclosure" href="https://example.com/hello&amp;world.png"');
});

it("should escape & in category attributes", () => {
const feed = new Feed({
title: "Feed Title",
id: "http://example.com/",
link: "http://example.com/",
updated,
});
feed.addCategory("Arts & Crafts");
feed.addItem({
title: "Hello World",
link: "http://example.org/2013/12/14",
date: updated,
category: [{ name: "R&D", scheme: "https://example.com/s?a=1&b=2" }],
});
const actual = feed.atom1();
// unlike a text node, xml-js does not escape `&` inside an attribute value,
// so it must be escaped before being handed to the serializer
expect(actual).toContain('<category term="Arts &amp; Crafts"/>');
expect(actual).toContain('<category label="R&amp;D" scheme="https://example.com/s?a=1&amp;b=2"/>');
expect(actual).not.toContain("Arts & Crafts");
});

it("should emit the default generator when none is provided", () => {
const feed = new Feed({ title: "Feed Title", id: "https://example.com/", link: "https://example.com/" });
expect(feed.atom1()).toContain("<generator>https://github.com/jpmonette/feed</generator>");
Expand Down
8 changes: 4 additions & 4 deletions src/atom1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
export default (ins: Feed) => {
const { options } = ins;

const base: any = {

Check warning on line 14 in src/atom1.ts

View workflow job for this annotation

GitHub Actions / eslint

Unexpected any. Specify a different type
_declaration: { _attributes: { version: "1.0", encoding: "utf-8" } },
_instruction: {},
feed: {
Expand Down Expand Up @@ -82,7 +82,7 @@
base.feed.category = [];

ins.categories.forEach((category: string) => {
base.feed.category.push({ _attributes: { term: category } });
base.feed.category.push({ _attributes: { term: sanitize(category) } });
});

base.feed.contributor = [];
Expand Down Expand Up @@ -227,12 +227,12 @@
const formatEnclosure = (enclosure: string | Enclosure, mimeCategory = "image") => {
if (typeof enclosure === "string") {
const sanitizedUrl = sanitize(enclosure);
const type = new URL(sanitizedUrl!).pathname.split(".").slice(-1)[0];

Check warning on line 230 in src/atom1.ts

View workflow job for this annotation

GitHub Actions / eslint

Forbidden non-null assertion
return { _attributes: { rel: "enclosure", href: sanitizedUrl, type: `${mimeCategory}/${type}` } };
}

const sanitizedUrl = sanitize(enclosure.url);
const type = new URL(sanitizedUrl!).pathname.split(".").slice(-1)[0];

Check warning on line 235 in src/atom1.ts

View workflow job for this annotation

GitHub Actions / eslint

Forbidden non-null assertion
return {
_attributes: {
rel: "enclosure",
Expand All @@ -253,9 +253,9 @@

return {
_attributes: {
label: name,
scheme,
term,
label: sanitize(name),
scheme: sanitize(scheme),
term: sanitize(term),
},
};
};
Loading