Skip to content

Commit 887d7af

Browse files
committed
fix: various jsx runtime bugs
- Fix `Fragment` not accepting non-array children and not sanitizing children - Increase jsx robustness against falsy values - Fix the value 0 not being rendered
1 parent 283eab3 commit 887d7af

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## 0.1.2
4+
5+
- Fix `Fragment` not accepting non-array children and not sanitizing children
6+
- Increase jsx robustness against falsy values
7+
- Fix the value 0 not being rendered
8+
39
## 0.1.1
410

511
- Allow `jsx` to process arrays of children, unblocks Bun

src/typed-html/jsx-runtime.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
import { createElement } from "typed-html";
44

5-
export function Fragment({ children }: { children: any[] }) {
6-
return children.join("");
5+
export function Fragment({ children }: { children?: unknown | unknown[] }): JSX.Element {
6+
if (Array.isArray(children)) return children.map(sanitizer).join('\n');
7+
return sanitizer(children)
78
}
89

910
/**
@@ -21,9 +22,10 @@ export const config = {
2122
sanitize: false as Sanitizer,
2223
};
2324

24-
function sanitizer(value: {}) {
25-
if (!config.sanitize) return value.toString();
26-
return config.sanitize(value.toString(), typeof value);
25+
function sanitizer(value: unknown): string {
26+
const str = value || value === 0 ? value.toString() : '';
27+
if (!config.sanitize) return str;
28+
return config.sanitize(str, typeof value);
2729
}
2830

2931
type Sanitizer = false | ((raw: string, originalType: string) => string);
@@ -38,13 +40,13 @@ function expandLiterals(props: Record<string, unknown>) {
3840
}
3941
}
4042

41-
export function jsx(tag: any, { children, ...props }: { children: JSX.Element | JSX.Element[] }): JSX.Element {
43+
export function jsx(tag: any, { children, ...props }: { children?: unknown | unknown[] }): JSX.Element {
4244
expandLiterals(props);
4345
const contents = Array.isArray(children) ? children.map(sanitizer) : [sanitizer(children)];
4446
return createElement(tag, props, ...contents);
4547
}
4648

47-
export function jsxs(tag: any, { children, ...props }: { children: JSX.Element[] }): JSX.Element {
49+
export function jsxs(tag: any, { children, ...props }: { children: unknown[] }): JSX.Element {
4850
expandLiterals(props);
4951
return createElement(tag, props, ...children.map(sanitizer));
5052
}

0 commit comments

Comments
 (0)