Skip to content
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

Handle edge case in jsx-runtime #4158

Merged
merged 4 commits into from
Aug 5, 2022
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
5 changes: 5 additions & 0 deletions .changeset/hungry-vans-deliver.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fix edge case where MDX components would be escaped
4 changes: 2 additions & 2 deletions packages/astro/src/runtime/server/jsx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ export async function renderJSX(result: SSRResult, vnode: any): Promise<any> {
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:
return '';
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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<em><slot/></em>

<style>
em {
color: red;
}
</style>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p><slot /></p>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1><slot/></h1>
Original file line number Diff line number Diff line change
@@ -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>
Original file line number Diff line number Diff line change
@@ -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_
32 changes: 32 additions & 0 deletions packages/integrations/mdx/test/mdx-escape.test.js
Original file line number Diff line number Diff line change
@@ -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');
});
});