diff options
author | Florian Bruhin <me@the-compiler.org> | 2023-12-04 10:49:30 +0100 |
---|---|---|
committer | Florian Bruhin <me@the-compiler.org> | 2023-12-04 10:49:30 +0100 |
commit | 6e6e9d285e491394cbfc24088e358cf4319c058d (patch) | |
tree | 5ad52d208808c07cb0103dce54823dfddc9b0294 | |
parent | 5e847cd4eab75dcd8bcdd2fc531b77285efc2f30 (diff) | |
parent | 1d29cf641ba25556ca148dc0a20abe0b90469874 (diff) | |
download | qutebrowser-6e6e9d285e491394cbfc24088e358cf4319c058d.tar.gz qutebrowser-6e6e9d285e491394cbfc24088e358cf4319c058d.zip |
Merge branch 'pdfjs-fix'
-rw-r--r-- | qutebrowser/browser/pdfjs.py | 28 | ||||
-rw-r--r-- | qutebrowser/utils/version.py | 2 | ||||
-rw-r--r-- | tests/unit/browser/test_pdfjs.py | 27 | ||||
-rw-r--r-- | tests/unit/utils/test_version.py | 4 |
4 files changed, 50 insertions, 11 deletions
diff --git a/qutebrowser/browser/pdfjs.py b/qutebrowser/browser/pdfjs.py index 467f3c605..841285deb 100644 --- a/qutebrowser/browser/pdfjs.py +++ b/qutebrowser/browser/pdfjs.py @@ -61,11 +61,11 @@ def generate_pdfjs_page(filename, url): html = html.replace('</body>', '</body><script>{}</script>'.format(script)) # WORKAROUND for the fact that PDF.js tries to use the Fetch API even with - # qute:// URLs. - pdfjs_script = '<script src="../build/pdf.js"></script>' - html = html.replace(pdfjs_script, - '<script>window.Response = undefined;</script>\n' + - pdfjs_script) + # qute:// URLs, this is probably no longer needed in PDFjs 4+. See #4235 + html = html.replace( + '<head>', + '<head>\n<script>window.Response = undefined;</script>\n' + ) return html @@ -202,10 +202,24 @@ def _read_from_system(system_path, names): return (None, None) +def get_pdfjs_js_path(): + """Checks for pdf.js main module availability and returns the path if available.""" + paths = ['build/pdf.js', 'build/pdf.mjs'] + for path in paths: + try: + get_pdfjs_res(path) + except PDFJSNotFound: + pass + else: + return path + + raise PDFJSNotFound(" or ".join(paths)) + + def is_available(): - """Return true if a pdfjs installation is available.""" + """Return true if certain parts of a pdfjs installation are available.""" try: - get_pdfjs_res('build/pdf.js') + get_pdfjs_js_path() get_pdfjs_res('web/viewer.html') except PDFJSNotFound: return False diff --git a/qutebrowser/utils/version.py b/qutebrowser/utils/version.py index a139d01c5..59da5b5f0 100644 --- a/qutebrowser/utils/version.py +++ b/qutebrowser/utils/version.py @@ -480,7 +480,7 @@ def _pdfjs_version() -> str: A string with the version number. """ try: - pdfjs_file, file_path = pdfjs.get_pdfjs_res_and_path('build/pdf.js') + pdfjs_file, file_path = pdfjs.get_pdfjs_res_and_path(pdfjs.get_pdfjs_js_path()) except pdfjs.PDFJSNotFound: return 'no' else: diff --git a/tests/unit/browser/test_pdfjs.py b/tests/unit/browser/test_pdfjs.py index fe2fea9a0..cb5c26229 100644 --- a/tests/unit/browser/test_pdfjs.py +++ b/tests/unit/browser/test_pdfjs.py @@ -193,6 +193,33 @@ def test_is_available(available, mocker): assert pdfjs.is_available() == available +@pytest.mark.parametrize('found_file', [ + "build/pdf.js", + "build/pdf.mjs", +]) +def test_get_pdfjs_js_path(found_file: str, monkeypatch: pytest.MonkeyPatch): + def fake_pdfjs_res(requested): + if requested.endswith(found_file): + return + raise pdfjs.PDFJSNotFound(requested) + + monkeypatch.setattr(pdfjs, 'get_pdfjs_res', fake_pdfjs_res) + assert pdfjs.get_pdfjs_js_path() == found_file + + +def test_get_pdfjs_js_path_none(monkeypatch: pytest.MonkeyPatch): + def fake_pdfjs_res(requested): + raise pdfjs.PDFJSNotFound(requested) + + monkeypatch.setattr(pdfjs, 'get_pdfjs_res', fake_pdfjs_res) + + with pytest.raises( + pdfjs.PDFJSNotFound, + match="Path 'build/pdf.js or build/pdf.mjs' not found" + ): + pdfjs.get_pdfjs_js_path() + + @pytest.mark.parametrize('mimetype, url, enabled, expected', [ # PDF files ('application/pdf', 'http://www.example.com', True, True), diff --git a/tests/unit/utils/test_version.py b/tests/unit/utils/test_version.py index d902f8c53..38134b40e 100644 --- a/tests/unit/utils/test_version.py +++ b/tests/unit/utils/test_version.py @@ -885,9 +885,7 @@ class TestPDFJSVersion: def test_real_file(self, data_tmpdir): """Test against the real file if pdfjs was found.""" - try: - pdfjs.get_pdfjs_res_and_path('build/pdf.js') - except pdfjs.PDFJSNotFound: + if not pdfjs.is_available(): pytest.skip("No pdfjs found") ver = version._pdfjs_version() assert ver.split()[0] not in ['no', 'unknown'], ver |