Skip to content

Commit c947c28

Browse files
Fix missing type-attribute in xml-stylesheet with xslt extension (#13867)
Co-authored-by: Florian Lefebvre <[email protected]>
1 parent 7ea9544 commit c947c28

3 files changed

Lines changed: 24 additions & 1 deletion

File tree

.changeset/huge-islands-fold.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@astrojs/rss': patch
3+
---
4+
5+
Fixes a missing type attribute when providing a XSLT stylesheet

packages/astro-rss/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ async function generateRSS(rssOptions: ValidatedRSSOptions): Promise<string> {
173173
const parser = new XMLParser(xmlOptions);
174174
const root: any = { '?xml': { '@_version': '1.0', '@_encoding': 'UTF-8' } };
175175
if (typeof rssOptions.stylesheet === 'string') {
176-
const isXSL = /\.xsl$/i.test(rssOptions.stylesheet);
176+
const isXSL = /\.xslt?$/i.test(rssOptions.stylesheet);
177177
root['?xml-stylesheet'] = {
178178
'@_href': rssOptions.stylesheet,
179179
...(isXSL && { '@_type': 'text/xsl' }),

packages/astro-rss/test/rss.test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ const validXmlWithCustomDataResult = `<?xml version="1.0" encoding="UTF-8"?><rss
3535
const validXmlWithStylesheet = `<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet href="/feedstylesheet.css"?><rss version="2.0"><channel><title><![CDATA[${title}]]></title><description><![CDATA[${description}]]></description><link>${site}/</link></channel></rss>`;
3636
// biome-ignore format: keep in one line
3737
const validXmlWithXSLStylesheet = `<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet href="/feedstylesheet.xsl" type="text/xsl"?><rss version="2.0"><channel><title><![CDATA[${title}]]></title><description><![CDATA[${description}]]></description><link>${site}/</link></channel></rss>`;
38+
// biome-ignore format: keep in one line
39+
const validXmlWithXSLTStylesheet = `<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet href="/feedstylesheet.xslt" type="text/xsl"?><rss version="2.0"><channel><title><![CDATA[${title}]]></title><description><![CDATA[${description}]]></description><link>${site}/</link></channel></rss>`;
3840

3941
function assertXmlDeepEqual(a, b) {
4042
const parsedA = parseXmlString(a);
@@ -161,9 +163,25 @@ describe('getRssString', () => {
161163
stylesheet: '/feedstylesheet.xsl',
162164
});
163165

166+
// xml2js doesn't parse processing instructions. Assert the type is present.
167+
assert.equal(str.includes('type="text/xsl"'), true);
164168
assertXmlDeepEqual(str, validXmlWithXSLStylesheet);
165169
});
166170

171+
it('should include xml-stylesheet instruction with xslt type when stylesheet is set to xslt file', async () => {
172+
const str = await getRssString({
173+
title,
174+
description,
175+
items: [],
176+
site,
177+
stylesheet: '/feedstylesheet.xslt',
178+
});
179+
180+
// xml2js doesn't parse processing instructions. Assert the type is present.
181+
assert.equal(str.includes('type="text/xsl"'), true);
182+
assertXmlDeepEqual(str, validXmlWithXSLTStylesheet);
183+
});
184+
167185
it('should preserve self-closing tags on `customData`', async () => {
168186
const customData =
169187
'<atom:link href="https://example.com/feed.xml" rel="self" type="application/rss+xml"/>';

0 commit comments

Comments
 (0)