diff --git a/src/display/metadata.js b/src/display/metadata.js
index 5c7922bf4..eaa3ebdc9 100644
--- a/src/display/metadata.js
+++ b/src/display/metadata.js
@@ -35,7 +35,9 @@ class Metadata {
}
_repair(data) {
- return data.replace(/>\\376\\377([^<]+)/g, function(all, codes) {
+ // Start by removing any "junk" before the first tag (see issue 10395).
+ return data.replace(/^([^<]+)/, '').replace(/>\\376\\377([^<]+)/g,
+ function(all, codes) {
let bytes = codes.replace(/\\([0-3])([0-7])([0-7])/g,
function(code, d1, d2, d3) {
return String.fromCharCode(d1 * 64 + d2 * 8 + d3 * 1);
@@ -104,7 +106,8 @@ class Metadata {
}
get(name) {
- return this._metadata[name] || null;
+ const data = this._metadata[name];
+ return (typeof data !== 'undefined' ? data : null);
}
getAll() {
diff --git a/test/unit/metadata_spec.js b/test/unit/metadata_spec.js
index 01a992702..b88d31c1d 100644
--- a/test/unit/metadata_spec.js
+++ b/test/unit/metadata_spec.js
@@ -128,6 +128,56 @@ describe('metadata', function() {
expect(isEmptyObj(metadata.getAll())).toEqual(true);
});
+ it('should gracefully handle "junk" before the actual metadata (issue 10395)',
+ function() {
+ const data = '' +
+ '' +
+ '' +
+ '' +
+ 'PDFKit.NET 4.0.102.0' +
+ '' +
+ '1.7' +
+ '' +
+ '2018-12-27T13:50:36-08:00' +
+ '2018-12-27T13:50:38-08:00' +
+ '' +
+ '2018-12-27T13:50:38-08:00' +
+ '' +
+ '' +
+ '' +
+ '' +
+ '' +
+ '' +
+ 'application/pdf' +
+ '';
+ const metadata = new Metadata(data);
+
+ expect(metadata.has('dc:title')).toBeTruthy();
+ expect(metadata.has('dc:qux')).toBeFalsy();
+
+ expect(metadata.get('dc:title')).toEqual('');
+ expect(metadata.get('dc:qux')).toEqual(null);
+
+ expect(metadata.getAll()).toEqual({
+ 'dc:creator': '',
+ 'dc:description': '',
+ 'dc:format': 'application/pdf',
+ 'dc:subject': '',
+ 'dc:title': '',
+ 'pdf:keywords': '',
+ 'pdf:pdfversion': '1.7',
+ 'pdf:producer': 'PDFKit.NET 4.0.102.0',
+ 'xap:createdate': '2018-12-27T13:50:36-08:00',
+ 'xap:creatortool': '',
+ 'xap:metadatadate': '2018-12-27T13:50:38-08:00',
+ 'xap:modifydate': '2018-12-27T13:50:38-08:00',
+ });
+ });
+
it('should correctly handle metadata containing "&apos" (issue 10407)',
function() {
const data = '' +