0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-16 21:46:22 -05:00

Handle edge case in jsx-runtime (#4158)

* fix(#4135): handle edge case in jsx-runtime

* test: add mdx test case

* chore: fix utils reference

* test: fix mdx escape test

Co-authored-by: Nate Moore <nate@astro.build>
This commit is contained in:
Nate Moore 2022-08-05 10:35:47 -05:00 committed by GitHub
parent e7bee22d18
commit e569f0a5c7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 66 additions and 2 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Fix edge case where MDX components would be escaped

View file

@ -51,12 +51,12 @@ export async function renderJSX(result: SSRResult, vnode: any): Promise<any> {
props[key] = value; props[key] = value;
} }
} }
return await renderToString(result, vnode.type as any, props, slots); return markHTMLString(await renderToString(result, vnode.type as any, props, slots));
} }
case !vnode.type && (vnode.type as any) !== 0: case !vnode.type && (vnode.type as any) !== 0:
return ''; return '';
case typeof vnode.type === 'string' && vnode.type !== ClientOnlyPlaceholder: case typeof vnode.type === 'string' && vnode.type !== ClientOnlyPlaceholder:
return await renderElement(result, vnode.type as string, vnode.props ?? {}); return markHTMLString(await renderElement(result, vnode.type as string, vnode.props ?? {}));
} }
if (vnode.type) { if (vnode.type) {

View file

@ -0,0 +1,7 @@
<em><slot/></em>
<style>
em {
color: red;
}
</style>

View file

@ -0,0 +1 @@
<p><slot /></p>

View file

@ -0,0 +1 @@
<h1><slot/></h1>

View file

@ -0,0 +1,5 @@
import P from '../components/P.astro';
import Em from '../components/Em.astro';
<P>Render <Em>Me</Em></P>
<P><Em>Me</Em></P>

View file

@ -0,0 +1,13 @@
import P from '../components/P.astro';
import Em from '../components/Em.astro';
import Title from '../components/Title.astro';
export const components = { p: P, em: Em, h1: Title };
# Hello _there_
# _there_
Hello _there_
_there_

View file

@ -0,0 +1,32 @@
import mdx from '@astrojs/mdx';
import { expect } from 'chai';
import { parseHTML } from 'linkedom';
import { loadFixture } from '../../../astro/test/test-utils.js';
const FIXTURE_ROOT = new URL('./fixtures/mdx-escape/', import.meta.url);
describe('MDX frontmatter', () => {
let fixture;
before(async () => {
fixture = await loadFixture({
root: FIXTURE_ROOT,
integrations: [mdx()],
});
await fixture.build();
});
it('does not have unescaped HTML at top-level', async () => {
const html = await fixture.readFile('/index.html');
const { document } = parseHTML(html);
expect(document.body.textContent).to.not.include('<em');
});
it('does not have unescaped HTML inside html tags', async () => {
const html = await fixture.readFile('/html-tag/index.html');
const { document } = parseHTML(html);
expect(document.body.textContent).to.not.include('<em');
});
});