diff --git a/qutebrowser/html/error.html b/qutebrowser/html/error.html index 80bd0cf61..ef50682ef 100644 --- a/qutebrowser/html/error.html +++ b/qutebrowser/html/error.html @@ -73,13 +73,13 @@ function searchFor(uri) {
- ![]() ![]() |
Unable to load pageError while opening {{ url }}:{{ error }} - + diff --git a/qutebrowser/utils/jinja.py b/qutebrowser/utils/jinja.py index f12184290..a8d746b58 100644 --- a/qutebrowser/utils/jinja.py +++ b/qutebrowser/utils/jinja.py @@ -22,6 +22,8 @@ import os import os.path import traceback +import mimetypes +import base64 import jinja2 import jinja2.exceptions @@ -74,6 +76,16 @@ def resource_url(path): return QUrl.fromLocalFile(image).toString(QUrl.FullyEncoded) +def data_url(path): + """Get a data: url for the broken qutebrowser logo.""" + data = utils.read_file(path, binary=True) + filename = utils.resource_filename(path) + mimetype = mimetypes.guess_type(filename) + assert mimetype is not None, path + b64 = base64.b64encode(data).decode('ascii') + return 'data:{};charset=utf-8;base64,{}'.format(mimetype[0], b64) + + def render(template, **kwargs): """Render the given template and pass the given arguments to it.""" try: @@ -89,3 +101,4 @@ def render(template, **kwargs): _env = jinja2.Environment(loader=Loader('html'), autoescape=_guess_autoescape) _env.globals['resource_url'] = resource_url _env.globals['file_url'] = urlutils.file_url +_env.globals['data_url'] = data_url diff --git a/tests/unit/utils/test_jinja.py b/tests/unit/utils/test_jinja.py index cea237d22..eb7a621fc 100644 --- a/tests/unit/utils/test_jinja.py +++ b/tests/unit/utils/test_jinja.py @@ -34,21 +34,42 @@ from qutebrowser.utils import utils, jinja def patch_read_file(monkeypatch): """pytest fixture to patch utils.read_file.""" real_read_file = utils.read_file + real_resource_filename = utils.resource_filename - def _read_file(path): + def _read_file(path, binary=False): """A read_file which returns a simple template if the path is right.""" if path == os.path.join('html', 'test.html'): + assert not binary return """Hello {{var}}""" elif path == os.path.join('html', 'test2.html'): + assert not binary return """{{ resource_url('utils/testfile') }}""" + elif path == os.path.join('html', 'test3.html'): + assert not binary + return """{{ data_url('testfile.txt') }}""" + elif path == 'testfile.txt': + assert binary + return b'foo' elif path == os.path.join('html', 'undef.html'): + assert not binary return """{{ does_not_exist() }}""" elif path == os.path.join('html', 'undef_error.html'): + assert not binary return real_read_file(path) else: raise IOError("Invalid path {}!".format(path)) + def _resource_filename(path): + if path == 'utils/testfile': + return real_resource_filename(path) + elif path == 'testfile.txt': + return path + else: + raise IOError("Invalid path {}!".format(path)) + monkeypatch.setattr('qutebrowser.utils.jinja.utils.read_file', _read_file) + monkeypatch.setattr('qutebrowser.utils.jinja.utils.resource_filename', + _resource_filename) def test_simple_template(): @@ -75,6 +96,15 @@ def test_resource_url(): assert f.read().splitlines()[0] == "Hello World!" +def test_data_url(): + """Test data_url() which can be used from templates.""" + data = jinja.render('test3.html') + print(data) + url = QUrl(data) + assert url.isValid() + assert data == 'data:text/plain;charset=utf-8;base64,Zm9v' # 'foo' + + def test_not_found(): """Test with a template which does not exist.""" with pytest.raises(jinja2.TemplateNotFound) as excinfo: |