Add urlutils.data_url

This commit is contained in:
Florian Bruhin 2016-11-15 22:25:51 +01:00
parent e1928ad991
commit 964ddb472b
7 changed files with 22 additions and 12 deletions

View File

@ -37,7 +37,7 @@ from PyQt5.QtWidgets import QTabWidget, QTabBar
from qutebrowser.commands import cmdutils from qutebrowser.commands import cmdutils
from qutebrowser.config import configexc from qutebrowser.config import configexc
from qutebrowser.utils import standarddir, utils from qutebrowser.utils import standarddir, utils, urlutils
SYSTEM_PROXY = object() # Return value for Proxy type SYSTEM_PROXY = object() # Return value for Proxy type
@ -1177,8 +1177,7 @@ class UserStyleSheet(File):
if path is not None and os.path.exists(path): if path is not None and os.path.exists(path):
return QUrl.fromLocalFile(path) return QUrl.fromLocalFile(path)
else: else:
data = base64.b64encode(value.encode('utf-8')).decode('ascii') return urlutils.data_url('text/css', value.encode('utf-8'))
return QUrl("data:text/css;charset=utf-8;base64,{}".format(data))
def validate(self, value): def validate(self, value):
self._basic_validation(value) self._basic_validation(value)

View File

@ -82,8 +82,7 @@ def data_url(path):
filename = utils.resource_filename(path) filename = utils.resource_filename(path)
mimetype = mimetypes.guess_type(filename) mimetype = mimetypes.guess_type(filename)
assert mimetype is not None, path assert mimetype is not None, path
b64 = base64.b64encode(data).decode('ascii') return urlutils.data_url(mimetype[0], data).toString()
return 'data:{};charset=utf-8;base64,{}'.format(mimetype[0], b64)
def render(template, **kwargs): def render(template, **kwargs):

View File

@ -20,6 +20,7 @@
"""Utils regarding URL handling.""" """Utils regarding URL handling."""
import re import re
import base64
import os.path import os.path
import ipaddress import ipaddress
import posixpath import posixpath
@ -580,3 +581,11 @@ def file_url(path):
path: The absolute path to the local file path: The absolute path to the local file
""" """
return QUrl.fromLocalFile(path).toString(QUrl.FullyEncoded) return QUrl.fromLocalFile(path).toString(QUrl.FullyEncoded)
def data_url(mimetype, data):
"""Get a data: QUrl for the given data."""
b64 = base64.b64encode(data).decode('ascii')
url = QUrl('data:{};base64,{}'.format(mimetype, b64))
qtutils.ensure_valid(url)
return url

View File

@ -19,7 +19,6 @@
"""Tests for the global page history.""" """Tests for the global page history."""
import base64
import logging import logging
import pytest import pytest
@ -28,7 +27,7 @@ from hypothesis import strategies
from PyQt5.QtCore import QUrl from PyQt5.QtCore import QUrl
from qutebrowser.browser import history from qutebrowser.browser import history
from qutebrowser.utils import objreg from qutebrowser.utils import objreg, urlutils
class FakeWebHistory: class FakeWebHistory:
@ -375,9 +374,8 @@ def hist_interface():
def test_history_interface(qtbot, webview, hist_interface): def test_history_interface(qtbot, webview, hist_interface):
html = "<a href='about:blank'>foo</a>" html = b"<a href='about:blank'>foo</a>"
data = base64.b64encode(html.encode('utf-8')).decode('ascii') url = urlutils.data_url('text/html', html)
url = QUrl("data:text/html;charset=utf-8;base64,{}".format(data))
with qtbot.waitSignal(webview.loadFinished): with qtbot.waitSignal(webview.loadFinished):
webview.load(url) webview.load(url)

View File

@ -1350,7 +1350,7 @@ class TestFileAndUserStyleSheet:
def test_transform_userstylesheet_base64(self, monkeypatch): def test_transform_userstylesheet_base64(self, monkeypatch):
"""Test transform with a data string.""" """Test transform with a data string."""
b64 = base64.b64encode(b"test").decode('ascii') b64 = base64.b64encode(b"test").decode('ascii')
url = QUrl("data:text/css;charset=utf-8;base64,{}".format(b64)) url = QUrl("data:text/css;base64,{}".format(b64))
assert configtypes.UserStyleSheet().transform("test") == url assert configtypes.UserStyleSheet().transform("test") == url

View File

@ -102,7 +102,7 @@ def test_data_url():
print(data) print(data)
url = QUrl(data) url = QUrl(data)
assert url.isValid() assert url.isValid()
assert data == 'data:text/plain;charset=utf-8;base64,Zm9v' # 'foo' assert data == 'data:text/plain;base64,Zm9v' # 'foo'
def test_not_found(): def test_not_found():

View File

@ -731,3 +731,8 @@ class TestIncDecNumber:
def test_file_url(): def test_file_url():
assert urlutils.file_url('/foo/bar') == 'file:///foo/bar' assert urlutils.file_url('/foo/bar') == 'file:///foo/bar'
def test_data_url():
url = urlutils.data_url('text/plain', b'foo')
assert url == QUrl('data:text/plain;base64,Zm9v')