Commit 76abf825 authored by Robert Knight's avatar Robert Knight

Resolve relative `PDFViewerApplication.url` values

If PDF.js is launched with a `file` query-string parameter that is a
relative URL, then `window.PDFViewerApplication.url` will be that
relative URL.

When capturing the metadata from the PDF in order to search for
annotations or find relevant groups, we want to use an absolute URL.
This commit uses the existing `normalizeURI` helper to resolve the
possibly-relative URL.

Fixes https://github.com/hypothesis/product-backlog/issues/579
parent bed7375a
'use strict';
const { normalizeURI } = require('../util/url');
/**
* This PDFMetadata service extracts metadata about a loading/loaded PDF
* document from a PDF.js PDFViewerApplication object.
......@@ -74,11 +76,13 @@ function fingerprintToURN(fingerprint) {
}
function getPDFURL(app) {
const url = normalizeURI(app.url);
// Local file:// URLs should not be saved in document metadata.
// Entries in document.link should be URIs. In the case of
// local files, omit the URL.
if (app.url.indexOf('file://') !== 0) {
return app.url;
if (url.indexOf('file://') !== 0) {
return url;
}
return null;
......
......@@ -14,7 +14,7 @@ describe('pdf-metadata', function () {
window.dispatchEvent(event);
return pdfMetadata.getUri().then(function (uri) {
assert.equal(uri, 'http://fake.com');
assert.equal(uri, 'http://fake.com/');
});
});
......@@ -25,7 +25,7 @@ describe('pdf-metadata', function () {
};
var pdfMetadata = new PDFMetadata(fakePDFViewerApplication);
return pdfMetadata.getUri().then(function (uri) {
assert.equal(uri, 'http://fake.com');
assert.equal(uri, 'http://fake.com/');
});
});
......@@ -41,7 +41,7 @@ describe('pdf-metadata', function () {
'dc:title': 'fakeTitle',
},
},
url: 'http://fake.com',
url: 'http://fake.com/',
};
beforeEach(function () {
......@@ -51,7 +51,7 @@ describe('pdf-metadata', function () {
describe('#getUri', function () {
it('returns the non-file URI', function() {
return pdfMetadata.getUri().then(function (uri) {
assert.equal(uri, 'http://fake.com');
assert.equal(uri, 'http://fake.com/');
});
});
......@@ -66,6 +66,20 @@ describe('pdf-metadata', function () {
assert.equal(uri, 'urn:x-pdf:fakeFingerprint');
});
});
it('resolves relative URLs', () => {
var fakePDFViewerApplication = {
url: 'index.php?action=download&file_id=wibble',
documentFingerprint: 'fakeFingerprint',
};
var pdfMetadata = new PDFMetadata(fakePDFViewerApplication);
return pdfMetadata.getUri().then(uri => {
var expected = new URL(fakePDFViewerApplication.url,
document.location.href).toString();
assert.equal(uri, expected);
});
});
});
describe('#getMetadata', function () {
......@@ -81,7 +95,7 @@ describe('pdf-metadata', function () {
fakePDFViewerApplication.metadata.get = sinon.stub().returns('dcTitle');
return pdfMetadata.getMetadata().then(function (actualMetadata) {
assert.deepEqual(expectedMetadata, actualMetadata);
assert.deepEqual(actualMetadata, expectedMetadata);
});
});
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment