Commit cdeec51a authored by Robert Knight's avatar Robert Knight

Only load PDF.js style overrides in PDF.js

Only load the `pdfjs-overrides.css` bundle when the client is loaded in
PDF.js.

This fixes an issue where a global styling change that was recently
added specifically for PDF.js [1] ended up affecting other web pages
[2].

[1] https://github.com/hypothesis/client/pull/4208
[2] https://github.com/hypothesis/product-backlog/issues/1270#issuecomment-1039647361
parent ca62bd5f
......@@ -28,6 +28,10 @@ const commonPolyfills = [
* A mapping from canonical asset path to cache-busted asset path
*/
/**
* @typedef {Window & { PDFViewerApplication?: object }} MaybePDFWindow
*/
/**
* Mark an element as having been added by the boot script.
*
......@@ -197,17 +201,23 @@ export function bootHypothesisClient(doc, config) {
const polyfills = polyfillBundles(commonPolyfills);
// Override styling in certain document viewers.
const viewerStyles = [];
if (
/** @type {MaybePDFWindow} */ (window).PDFViewerApplication !== undefined
) {
viewerStyles.push('styles/pdfjs-overrides.css');
}
injectAssets(
doc,
config,
[
// Vendor code and polyfills
...polyfills,
...viewerStyles,
'scripts/annotator.bundle.js',
'styles/highlights.css',
'styles/pdfjs-overrides.css',
],
// Force re-evaluation of JS module scripts, so that the annotator entry
......
......@@ -104,12 +104,29 @@ describe('bootstrap', () => {
const expectedAssets = [
'scripts/annotator.bundle.1234.js#ts=123',
'styles/highlights.1234.css',
'styles/pdfjs-overrides.1234.css',
].map(assetUrl);
assert.deepEqual(findAssets(iframe.contentDocument), expectedAssets);
});
it('loads styling overrides in PDF.js', () => {
clock.tick(123); // Set timestamp used by module cache-busting fragment.
window.PDFViewerApplication = {};
try {
runBoot('annotator');
const expectedAssets = [
'scripts/annotator.bundle.1234.js#ts=123',
'styles/highlights.1234.css',
'styles/pdfjs-overrides.1234.css',
].map(assetUrl);
assert.deepEqual(findAssets(iframe.contentDocument), expectedAssets);
} finally {
delete window.PDFViewerApplication;
}
});
it('preloads assets used wihin shadow roots in the annotation layer', () => {
runBoot('annotator');
......
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