def _data_url(self, 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 return urlutils.data_url(mimetype[0], data).toString()
def test_data_url_workaround_needed(qapp, qtbot, webengineview, with_slash): """With data URLs, we get rather weird base64 filenames back from QtWebEngine. This test verifies that our workaround for this is still needed, i.e. if we get those base64-filenames rather than a "download.pdf" like with Chromium. """ # https://stackoverflow.com/a/17280876/2085149 pdf_source = [ '%PDF-1.0', '1 0 obj<</Pages 2 0 R>>endobj', '2 0 obj<</Kids[3 0 R]/Count 1>>endobj', '3 0 obj<</MediaBox[0 0 3 3]>>endobj', 'trailer<</Root 1 0 R>>', ] if with_slash: pdf_source.insert(1, '% ?') # this results in a slash in base64 pdf_data = '\n'.join(pdf_source).encode('ascii') base64_data = base64.b64encode(pdf_data).decode('ascii') if with_slash: assert '/' in base64_data expected = base64_data.split('/')[1] else: assert '/' not in base64_data expected = 'pdf' # from the mimetype def check_item(item): assert item.mimeType() == 'application/pdf' assert item.url().scheme() == 'data' assert os.path.basename(item.path()) == expected return True profile = QWebEngineProfile.defaultProfile() profile.setParent(qapp) url = urlutils.data_url('application/pdf', pdf_data) with qtbot.waitSignal(profile.downloadRequested, check_params_cb=check_item): webengineview.load(url)
def _data_url(self, path: str) -> str: """Get a data: url for the broken qutebrowser logo.""" data = resources.read_file_binary(path) mimetype = utils.guess_mimetype(path) return urlutils.data_url(mimetype, data).toString()
def _set_user_stylesheet(settings): """Set the generated user-stylesheet.""" stylesheet = shared.get_user_stylesheet().encode('utf-8') url = urlutils.data_url('text/css;charset=utf-8', stylesheet) settings.setUserStyleSheetUrl(url)
def test_history_interface(self, qtbot, webview, hist_interface): html = b"<a href='about:blank'>foo</a>" url = urlutils.data_url('text/html', html) with qtbot.waitSignal(webview.loadFinished): webview.load(url)
def test_data_url(): url = urlutils.data_url('text/plain', b'foo') assert url == QUrl('data:text/plain;base64,Zm9v')
def test_history_interface(qtbot, webview, hist_interface): html = b"<a href='about:blank'>foo</a>" url = urlutils.data_url('text/html', html) with qtbot.waitSignal(webview.loadFinished): webview.load(url)
def _set_user_stylesheet(settings): """Set the generated user-stylesheet.""" stylesheet = shared.get_user_stylesheet().encode('utf-8') url = urlutils.data_url('text/css;charset=utf-8', stylesheet) settings.setUserStyleSheetUrl(url)
def test_data_url(): url = urlutils.data_url('text/plain', b'foo') assert url == QUrl('data:text/plain;base64,Zm9v')
def pdf_url(self, pdf_bytes): return urlutils.data_url('application/pdf', pdf_bytes)