Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
coopwire-hypothesis
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
孙灵跃 Leon Sun
coopwire-hypothesis
Commits
c7778409
Unverified
Commit
c7778409
authored
Nov 19, 2018
by
Robert Knight
Committed by
GitHub
Nov 19, 2018
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #818 from hypothesis/support-newer-pdfjs
Support recent versions of PDF.js
parents
186b59dd
d2241159
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
128 additions
and
44 deletions
+128
-44
pdf-metadata.js
src/annotator/plugin/pdf-metadata.js
+11
-4
pdf-metadata-test.js
src/annotator/plugin/test/pdf-metadata-test.js
+117
-40
No files found.
src/annotator/plugin/pdf-metadata.js
View file @
c7778409
...
...
@@ -38,13 +38,20 @@ class PDFMetadata {
this
.
_loaded
=
new
Promise
(
resolve
=>
{
const
finish
=
()
=>
{
window
.
removeEventListener
(
'documentload'
,
finish
);
window
.
removeEventListener
(
'documentloaded'
,
finish
);
resolve
(
app
);
};
if
(
app
.
do
cumentFingerprint
)
{
if
(
app
.
do
wnloadComplete
)
{
resolve
(
app
);
}
else
{
// Listen for either the `documentload` (older PDF.js) or
// `documentloaded` (newer PDF.js) events which signal that the document
// has been downloaded and the first page has been rendered.
//
// See https://github.com/mozilla/pdf.js/commit/7bc4bfcc8b7f52b14107f0a551becdf01643c5c2
window
.
addEventListener
(
'documentload'
,
finish
);
window
.
addEventListener
(
'documentloaded'
,
finish
);
}
});
}
...
...
@@ -61,7 +68,7 @@ class PDFMetadata {
return
this
.
_loaded
.
then
(
app
=>
{
let
uri
=
getPDFURL
(
app
);
if
(
!
uri
)
{
uri
=
fingerprintToURN
(
app
.
documentF
ingerprint
);
uri
=
fingerprintToURN
(
app
.
pdfDocument
.
f
ingerprint
);
}
return
uri
;
});
...
...
@@ -86,7 +93,7 @@ class PDFMetadata {
}
const
link
=
[
{
href
:
fingerprintToURN
(
app
.
documentF
ingerprint
)},
{
href
:
fingerprintToURN
(
app
.
pdfDocument
.
f
ingerprint
)},
];
const
url
=
getPDFURL
(
app
);
...
...
@@ -97,7 +104,7 @@ class PDFMetadata {
return
{
title
:
title
,
link
:
link
,
documentFingerprint
:
app
.
documentF
ingerprint
,
documentFingerprint
:
app
.
pdfDocument
.
f
ingerprint
,
};
});
}
...
...
src/annotator/plugin/test/pdf-metadata-test.js
View file @
c7778409
...
...
@@ -2,27 +2,107 @@
const
PDFMetadata
=
require
(
'../pdf-metadata'
);
describe
(
'pdf-metadata'
,
function
()
{
/**
* Fake implementation of PDF.js `window.PDFViewerApplication.metadata`.
*/
class
FakeMetadata
{
/**
* Initialize the metadata dictionary.
*
* @param {Object} metadata - A key/value dictionary of metadata fields.
*/
constructor
(
metadata
)
{
this
.
_metadata
=
metadata
;
}
get
(
key
)
{
return
this
.
_metadata
[
key
];
}
has
(
key
)
{
return
this
.
_metadata
.
hasOwnProperty
(
key
);
}
}
/**
* Fake implementation of PDF.js `window.PDFViewerApplication.pdfDocument`.
*/
class
FakePDFDocumentProxy
{
constructor
({
fingerprint
})
{
this
.
fingerprint
=
fingerprint
;
}
}
/**
* Fake implementation of PDF.js `window.PDFViewerApplication` entry point.
*
* This fake only implements the parts that concern document metadata.
*/
class
FakePDFViewerApplication
{
/**
* Initialize the "PDF viewer" as it would be when loading a document or
* when a document fails to load.
*/
constructor
(
url
=
''
)
{
this
.
url
=
url
;
this
.
documentInfo
=
undefined
;
this
.
metadata
=
undefined
;
this
.
pdfDocument
=
null
;
}
/**
* Simulate completion of PDF document loading.
*/
finishLoading
({
url
,
fingerprint
,
metadata
,
title
,
eventName
=
'documentload'
})
{
const
event
=
document
.
createEvent
(
'Event'
);
event
.
initEvent
(
eventName
,
false
,
false
);
window
.
dispatchEvent
(
event
);
this
.
url
=
url
;
this
.
downloadComplete
=
true
;
this
.
documentInfo
=
{};
if
(
typeof
title
!==
undefined
)
{
this
.
documentInfo
.
Title
=
title
;
}
if
(
metadata
)
{
this
.
metadata
=
new
FakeMetadata
(
metadata
);
}
this
.
pdfDocument
=
new
FakePDFDocumentProxy
({
fingerprint
});
}
}
describe
(
'annotator/plugin/pdf-metadata'
,
function
()
{
[
// Event dispatched in older PDF.js versions (pre-7bc4bfcc).
'documentload'
,
// Event dispatched in newer PDF.js versions (post-7bc4bfcc).
'documentloaded'
,
].
forEach
(
eventName
=>
{
it
(
'waits for the PDF to load before returning metadata'
,
function
()
{
const
fakeApp
=
{}
;
const
fakeApp
=
new
FakePDFViewerApplication
;
const
pdfMetadata
=
new
PDFMetadata
(
fakeApp
);
const
event
=
document
.
createEvent
(
'Event'
);
event
.
initEvent
(
'documentload'
,
false
,
false
);
fakeApp
.
url
=
'http://fake.com'
;
fakeApp
.
documentFingerprint
=
'fakeFingerprint'
;
window
.
dispatchEvent
(
event
);
fakeApp
.
finishLoading
({
eventName
,
url
:
'http://fake.com'
,
fingerprint
:
'fakeFingerprint'
,
}
);
return
pdfMetadata
.
getUri
().
then
(
function
(
uri
)
{
assert
.
equal
(
uri
,
'http://fake.com/'
);
});
});
});
it
(
'does not wait for the PDF to load if it has already loaded'
,
function
()
{
const
fakePDFViewerApplication
=
{
const
fakePDFViewerApplication
=
new
FakePDFViewerApplication
;
fakePDFViewerApplication
.
finishLoading
({
url
:
'http://fake.com'
,
documentF
ingerprint
:
'fakeFingerprint'
,
};
f
ingerprint
:
'fakeFingerprint'
,
}
)
;
const
pdfMetadata
=
new
PDFMetadata
(
fakePDFViewerApplication
);
return
pdfMetadata
.
getUri
().
then
(
function
(
uri
)
{
assert
.
equal
(
uri
,
'http://fake.com/'
);
...
...
@@ -31,18 +111,15 @@ describe('pdf-metadata', function () {
describe
(
'metadata sources'
,
function
()
{
let
pdfMetadata
;
const
fakePDFViewerApplication
=
{
documentFingerprint
:
'fakeFingerprint'
,
documentInfo
:
{
Title
:
'fakeTitle'
,
},
const
fakePDFViewerApplication
=
new
FakePDFViewerApplication
;
fakePDFViewerApplication
.
finishLoading
({
fingerprint
:
'fakeFingerprint'
,
title
:
'fakeTitle'
,
metadata
:
{
metadata
:
{
'dc:title'
:
'fakeTitle'
,
},
'dc:title'
:
'dcFakeTitle'
,
},
url
:
'http://fake.com/'
,
};
}
)
;
beforeEach
(
function
()
{
pdfMetadata
=
new
PDFMetadata
(
fakePDFViewerApplication
);
...
...
@@ -56,10 +133,11 @@ describe('pdf-metadata', function () {
});
it
(
'returns the fingerprint as a URN when the PDF URL is a local file'
,
function
()
{
const
fakePDFViewerApplication
=
{
const
fakePDFViewerApplication
=
new
FakePDFViewerApplication
;
fakePDFViewerApplication
.
finishLoading
({
url
:
'file:///test.pdf'
,
documentF
ingerprint
:
'fakeFingerprint'
,
};
f
ingerprint
:
'fakeFingerprint'
,
}
)
;
const
pdfMetadata
=
new
PDFMetadata
(
fakePDFViewerApplication
);
return
pdfMetadata
.
getUri
().
then
(
function
(
uri
)
{
...
...
@@ -68,10 +146,11 @@ describe('pdf-metadata', function () {
});
it
(
'resolves relative URLs'
,
()
=>
{
const
fakePDFViewerApplication
=
{
const
fakePDFViewerApplication
=
new
FakePDFViewerApplication
;
fakePDFViewerApplication
.
finishLoading
({
url
:
'index.php?action=download&file_id=wibble'
,
documentF
ingerprint
:
'fakeFingerprint'
,
};
f
ingerprint
:
'fakeFingerprint'
,
}
)
;
const
pdfMetadata
=
new
PDFMetadata
(
fakePDFViewerApplication
);
return
pdfMetadata
.
getUri
().
then
(
uri
=>
{
...
...
@@ -85,15 +164,12 @@ describe('pdf-metadata', function () {
describe
(
'#getMetadata'
,
function
()
{
it
(
'gets the title from the dc:title field'
,
function
()
{
const
expectedMetadata
=
{
title
:
'dcTitle'
,
link
:
[{
href
:
'urn:x-pdf:'
+
fakePDFViewerApplication
.
documentF
ingerprint
},
title
:
'dc
Fake
Title'
,
link
:
[{
href
:
'urn:x-pdf:'
+
fakePDFViewerApplication
.
pdfDocument
.
f
ingerprint
},
{
href
:
fakePDFViewerApplication
.
url
}],
documentFingerprint
:
fakePDFViewerApplication
.
documentF
ingerprint
,
documentFingerprint
:
fakePDFViewerApplication
.
pdfDocument
.
f
ingerprint
,
};
fakePDFViewerApplication
.
metadata
.
has
=
sinon
.
stub
().
returns
(
true
);
fakePDFViewerApplication
.
metadata
.
get
=
sinon
.
stub
().
returns
(
'dcTitle'
);
return
pdfMetadata
.
getMetadata
().
then
(
function
(
actualMetadata
)
{
assert
.
deepEqual
(
actualMetadata
,
expectedMetadata
);
});
...
...
@@ -102,9 +178,9 @@ describe('pdf-metadata', function () {
it
(
'gets the title from the documentInfo.Title field'
,
function
()
{
const
expectedMetadata
=
{
title
:
fakePDFViewerApplication
.
documentInfo
.
Title
,
link
:
[{
href
:
'urn:x-pdf:'
+
fakePDFViewerApplication
.
documentF
ingerprint
},
link
:
[{
href
:
'urn:x-pdf:'
+
fakePDFViewerApplication
.
pdfDocument
.
f
ingerprint
},
{
href
:
fakePDFViewerApplication
.
url
}],
documentFingerprint
:
fakePDFViewerApplication
.
documentF
ingerprint
,
documentFingerprint
:
fakePDFViewerApplication
.
pdfDocument
.
f
ingerprint
,
};
fakePDFViewerApplication
.
metadata
.
has
=
sinon
.
stub
().
returns
(
false
);
...
...
@@ -116,12 +192,13 @@ describe('pdf-metadata', function () {
it
(
'does not save file:// URLs in document metadata'
,
function
()
{
let
pdfMetadata
;
const
fakePDFViewerApplication
=
{
documentFingerprint
:
'fakeFingerprint'
,
const
fakePDFViewerApplication
=
new
FakePDFViewerApplication
;
fakePDFViewerApplication
.
finishLoading
({
fingerprint
:
'fakeFingerprint'
,
url
:
'file://fake.pdf'
,
};
}
)
;
const
expectedMetadata
=
{
link
:
[{
href
:
'urn:x-pdf:'
+
fakePDFViewerApplication
.
documentF
ingerprint
}],
link
:
[{
href
:
'urn:x-pdf:'
+
fakePDFViewerApplication
.
pdfDocument
.
f
ingerprint
}],
};
pdfMetadata
=
new
PDFMetadata
(
fakePDFViewerApplication
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment