Skip to content

Commit

Permalink
Fix text inside links (#2146)
Browse files Browse the repository at this point in the history
* fix text inside links

* add changeset
  • Loading branch information
jeetiss authored Jan 21, 2023
1 parent 8d233f6 commit 2b05ef7
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/curvy-doors-burn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@react-pdf/renderer': patch
---

render text inside links
4 changes: 3 additions & 1 deletion packages/renderer/src/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ import propsEqual from './utils/propsEqual';
const emptyObject = {};

const appendChild = (parentInstance, child) => {
const isParentLink = parentInstance.type === 'LINK';
const isParentText = parentInstance.type === 'TEXT';
const isChildTextInstance = child.type === 'TEXT_INSTANCE';
const isOrphanTextInstance = isChildTextInstance && !isParentText;
const isOrphanTextInstance =
isChildTextInstance && !isParentText && !isParentLink;

// Ignore orphan text instances.
// Caused by cases such as <>{name && <Text>{name}</Text>}</>
Expand Down
48 changes: 48 additions & 0 deletions packages/renderer/tests/link.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import React from 'react';
import { Document, Page, Link, Font, Text } from '..';
import renderToImage from './renderComponent';

// pdf.js does not render default fonts in node and I use Open Sans (:
Font.register({
family: 'Open Sans',
src: 'https://fonts.gstatic.com/s/opensans/v17/mem8YaGs126MiZpBA-UFVZ0e.ttf',
});

const mount = async children => {
const image = await renderToImage(
<Document>
<Page size={[50, 25]}>{children}</Page>
</Document>,
);

return image;
};

describe('Link', () => {
test('should render text', async () => {
const image = await mount(
<Link
href="https://github.com/wojtekmaj/react-pdf"
style={{ fontFamily: 'Open Sans' }}
>
hello
</Link>,
);

expect(image).toMatchImageSnapshot();
});

test('should render TEXT component', async () => {
const image = await mount(
<Link
href="https://github.com/wojtekmaj/react-pdf"
style={{ fontFamily: 'Open Sans', textDecoration: 'none' }}
>
he
<Text style={{ textDecoration: 'underline' }}>llo</Text>
</Link>,
);

expect(image).toMatchImageSnapshot();
});
});
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 2b05ef7

Please sign in to comment.