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

Fix customTextRenderer not working on documents without marked content #1531

Merged
merged 2 commits into from
Jun 12, 2023
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
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