Commit e36b238f authored by Robert Knight's avatar Robert Knight

Fix null-safety errors

Various methods in `DocumentMeta` assume that `this.metadata.{dc,
highwire, ...}` properties exist, but this is only true once the
`getDocumentMetadata()` function has been called.

Adjust the constructor so that these fields always exist.

Note that while this avoids an exception if calling certain methods before
`getDocumentMetadata`, it would still be a logical error if that
happened. For the moment such a mistake is just silently ignored.
parent f3722506
......@@ -24,6 +24,43 @@ import Plugin from '../plugin';
import { normalizeURI } from '../util/url';
/**
* Extension of the `Metadata` type with non-optional fields for `dc`, `eprints` etc.
*
* @typedef HTMLDocumentMetadata
* @prop {string} title
* @prop {Object[]} link
* @prop {string} [link.rel]
* @prop {string} [link.type]
* @prop {string} link.href
* @prop {Object.<string, string[]>} dc
* @prop {Object.<string, string[]>} eprints
* @prop {Object.<string, string[]>} facebook
* @prop {Object.<string, string[]>} highwire
* @prop {Object.<string, string[]>} prism
* @prop {Object.<string, string[]>} twitter
* @prop {string} [favicon]
* @prop {string} [documentFingerprint]
*/
/**
* Create an empty `HTMLDocumentMetadata` object.
*
* @return {HTMLDocumentMetadata}
*/
function createMetadata() {
return {
title: document.title,
link: [],
dc: {},
eprints: {},
facebook: {},
highwire: {},
prism: {},
twitter: {},
};
}
/**
* DocumentMeta reads metadata/links from the current HTML document and
* populates the `document` property of new annotations.
......@@ -36,8 +73,7 @@ export default class DocumentMeta extends Plugin {
beforeAnnotationCreated: 'beforeAnnotationCreated',
};
/** @type {Metadata} */
this.metadata = { title: document.title, link: [] };
this.metadata = createMetadata();
this.baseURI = options.baseURI || baseURI;
this.document = options.document || document;
......@@ -90,7 +126,7 @@ export default class DocumentMeta extends Plugin {
* Return metadata for the current page.
*/
getDocumentMetadata() {
this.metadata = { title: document.title, link: [] };
this.metadata = createMetadata();
// first look for some common metadata types
// TODO: look for microdata/rdfa?
......
......@@ -20,7 +20,8 @@
* @prop {string} [link.rel]
* @prop {string} [link.type]
* @prop {string} link.href
* // html pages
*
* // HTML only.
* @prop {Object.<string, string[]>} [dc]
* @prop {Object.<string, string[]>} [eprints]
* @prop {Object.<string, string[]>} [facebook]
......@@ -28,7 +29,8 @@
* @prop {Object.<string, string[]>} [prism]
* @prop {Object.<string, string[]>} [twitter]
* @prop {string} [favicon]
* // pdf files
*
* // HTML + PDF.
* @prop {string} [documentFingerprint]
*/
......
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