Skip to content

Commit 54e9625

Browse files
authored
Fix for tspan (#2149)
* fix tspan * add changeset
1 parent a14ca9e commit 54e9625

4 files changed

Lines changed: 49 additions & 4 deletions

File tree

.changeset/nasty-dolphins-hide.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@react-pdf/renderer': patch
3+
---
4+
5+
Fix "Invalid" error when a text label is a child of a Tspan element

packages/renderer/src/renderer.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@ import propsEqual from './utils/propsEqual';
1212
const emptyObject = {};
1313

1414
const appendChild = (parentInstance, child) => {
15-
const isParentLink = parentInstance.type === 'LINK';
16-
const isParentText = parentInstance.type === 'TEXT';
15+
const isParentText =
16+
parentInstance.type === 'TEXT' ||
17+
parentInstance.type === 'LINK' ||
18+
parentInstance.type === 'TSPAN';
1719
const isChildTextInstance = child.type === 'TEXT_INSTANCE';
18-
const isOrphanTextInstance =
19-
isChildTextInstance && !isParentText && !isParentLink;
20+
const isOrphanTextInstance = isChildTextInstance && !isParentText;
2021

2122
// Ignore orphan text instances.
2223
// Caused by cases such as <>{name && <Text>{name}</Text>}</>
1.77 KB
Loading
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/* eslint-disable react/jsx-one-expression-per-line */
2+
import React from 'react';
3+
import { Document, Page, Svg, Font, Text, Tspan } from '..';
4+
import renderToImage from './renderComponent';
5+
6+
// pdf.js does not render default fonts in node and I use Open Sans (:
7+
Font.register({
8+
family: 'Open Sans',
9+
src: 'https://fonts.gstatic.com/s/opensans/v17/mem8YaGs126MiZpBA-UFVZ0e.ttf',
10+
});
11+
12+
const mount = async children => {
13+
const image = await renderToImage(
14+
<Document>
15+
<Page style={{ border: '1 solid #e2e2e2' }} size={[54, 47]}>
16+
{children}
17+
</Page>
18+
</Document>,
19+
);
20+
21+
return image;
22+
};
23+
24+
describe('Svg', () => {
25+
test('should render Tspan component', async () => {
26+
const image = await mount(
27+
<Svg width={52} viewBox="0 0 52 45">
28+
<Text x={2} y={20} style={{ fontFamily: 'Open Sans' }}>
29+
hello{' '}
30+
<Tspan x={2} y={35} fill="red">
31+
world
32+
</Tspan>
33+
</Text>
34+
</Svg>,
35+
);
36+
37+
expect(image).toMatchImageSnapshot();
38+
});
39+
});

0 commit comments

Comments
 (0)