Skip to content

Commit

Permalink
Fix customTextRenderer not working on documents without marked content (
Browse files Browse the repository at this point in the history
#1531)

Fixes #1530
  • Loading branch information
MattL75 authored Jun 12, 2023
1 parent b370645 commit 11027c1
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
Binary file added __mocks__/_untagged.pdf
Binary file not shown.
23 changes: 23 additions & 0 deletions src/Page/TextLayer.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import type { TextContent } from 'pdfjs-dist/types/src/display/api';
import type { PageContextType } from '../shared/types';

const pdfFile = loadPDF('./__mocks__/_pdf.pdf');
const untaggedPdfFile = loadPDF('./__mocks__/_untagged.pdf');

function renderWithContext(children: React.ReactNode, context: Partial<PageContextType>) {
const { rerender, ...otherResult } = render(
Expand Down Expand Up @@ -248,5 +249,27 @@ describe('TextLayer', () => {

expect(container).toHaveTextContent('Test value');
});

it('renders text content properly given customTextRenderer and untagged document', async () => {
const { func: onRenderTextLayerSuccess, promise: onRenderTextLayerSuccessPromise } =
makeAsyncCallback();

const customTextRenderer = () => 'Test value';

const untaggedDoc = await pdfjs.getDocument({ data: untaggedPdfFile.arrayBuffer }).promise;
const untaggedPage = await untaggedDoc.getPage(1);

const { container } = renderWithContext(<TextLayer />, {
customTextRenderer,
onRenderTextLayerSuccess,
page: untaggedPage,
});

expect.assertions(1);

await onRenderTextLayerSuccessPromise;

expect(container).toHaveTextContent('Test value');
});
});
});
8 changes: 6 additions & 2 deletions src/Page/TextLayer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,11 @@ export default function TextLayer() {
layer.append(end);
endElement.current = end;

const layerChildrenDeep = layer.querySelectorAll('.markedContent > *:not(.markedContent');
const hasMarkedContent = Boolean(layer.querySelector('.markedContent'));

const layerChildren = hasMarkedContent
? layer.querySelectorAll('.markedContent > *:not(.markedContent')
: layer.children;

if (customTextRenderer) {
let index = 0;
Expand All @@ -211,7 +215,7 @@ export default function TextLayer() {
return;
}

const child = layerChildrenDeep[index];
const child = layerChildren[index];

if (!child) {
return;
Expand Down

0 comments on commit 11027c1

Please sign in to comment.