Add urlutils.data_url
This commit is contained in:
parent
e1928ad991
commit
964ddb472b
@ -37,7 +37,7 @@ from PyQt5.QtWidgets import QTabWidget, QTabBar
|
||||
|
||||
from qutebrowser.commands import cmdutils
|
||||
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
|
||||
@ -1177,8 +1177,7 @@ class UserStyleSheet(File):
|
||||
if path is not None and os.path.exists(path):
|
||||
return QUrl.fromLocalFile(path)
|
||||
else:
|
||||
data = base64.b64encode(value.encode('utf-8')).decode('ascii')
|
||||
return QUrl("data:text/css;charset=utf-8;base64,{}".format(data))
|
||||
return urlutils.data_url('text/css', value.encode('utf-8'))
|
||||
|
||||
def validate(self, value):
|
||||
self._basic_validation(value)
|
||||
|
@ -82,8 +82,7 @@ def data_url(path):
|
||||
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)
|
||||
return urlutils.data_url(mimetype[0], data).toString()
|
||||
|
||||
|
||||
def render(template, **kwargs):
|
||||
|
@ -20,6 +20,7 @@
|
||||
"""Utils regarding URL handling."""
|
||||
|
||||
import re
|
||||
import base64
|
||||
import os.path
|
||||
import ipaddress
|
||||
import posixpath
|
||||
@ -580,3 +581,11 @@ def file_url(path):
|
||||
path: The absolute path to the local file
|
||||
"""
|
||||
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
|
||||
|
@ -19,7 +19,6 @@
|
||||
|
||||
"""Tests for the global page history."""
|
||||
|
||||
import base64
|
||||
import logging
|
||||
|
||||
import pytest
|
||||
@ -28,7 +27,7 @@ from hypothesis import strategies
|
||||
from PyQt5.QtCore import QUrl
|
||||
|
||||
from qutebrowser.browser import history
|
||||
from qutebrowser.utils import objreg
|
||||
from qutebrowser.utils import objreg, urlutils
|
||||
|
||||
|
||||
class FakeWebHistory:
|
||||
@ -375,9 +374,8 @@ def hist_interface():
|
||||
|
||||
|
||||
def test_history_interface(qtbot, webview, hist_interface):
|
||||
html = "<a href='about:blank'>foo</a>"
|
||||
data = base64.b64encode(html.encode('utf-8')).decode('ascii')
|
||||
url = QUrl("data:text/html;charset=utf-8;base64,{}".format(data))
|
||||
html = b"<a href='about:blank'>foo</a>"
|
||||
url = urlutils.data_url('text/html', html)
|
||||
with qtbot.waitSignal(webview.loadFinished):
|
||||
webview.load(url)
|
||||
|
||||
|
@ -1350,7 +1350,7 @@ class TestFileAndUserStyleSheet:
|
||||
def test_transform_userstylesheet_base64(self, monkeypatch):
|
||||
"""Test transform with a data string."""
|
||||
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
|
||||
|
||||
|
||||
|
@ -102,7 +102,7 @@ def test_data_url():
|
||||
print(data)
|
||||
url = QUrl(data)
|
||||
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():
|
||||
|
@ -731,3 +731,8 @@ class TestIncDecNumber:
|
||||
|
||||
def test_file_url():
|
||||
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')
|
||||
|
Loading…
Reference in New Issue
Block a user