Skip to content

Commit

Permalink
Do not use document.title as a fallback for title in PDFs
Browse files Browse the repository at this point in the history
If a PDF has no embedded title, don't use `document.title` as a
fallback. In top-level frames PDF.js sets the title to the filename in the URL,
so this worked out OK. When PDF.js was embedded in an iframe however it
did not modify `document.title` and it would typically be set to a
generic default such as "PDF.js viewer" (the default title of the PDF.js
viewer application). The one case where we might actually want to use
`document.title` is in a custom viewer where the publisher has taken
care to set it appropriately based on some other source. This is a tiny
minority of usage currently.

Fixes #3372
  • Loading branch information
robertknight committed May 11, 2021
1 parent 87956ee commit dd02808
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/annotator/integrations/pdf-metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export class PDFMetadata {
*/
getMetadata() {
return this._loaded.then(app => {
let title = document.title;
let title = '';

if (
app.metadata &&
Expand Down
13 changes: 13 additions & 0 deletions src/annotator/integrations/test/pdf-metadata-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -320,5 +320,18 @@ describe('PDFMetadata', function () {

assert.equal(metadata.title, 'Some title');
});

it('returns an empty string if the PDF has no title', async () => {
const { pdfMetadata } = createPDFMetadata({
url: 'https://example.com/test.pdf',
});

// Earlier versions of the client used `document.title` as a fallback,
// but we changed this. See https://github.com/hypothesis/client/issues/3372.
document.title = 'Ignore me';
const metadata = await pdfMetadata.getMetadata();

assert.equal(metadata.title, '');
});
});
});

0 comments on commit dd02808

Please sign in to comment.