Handle UTF-8 byte order marks in Greasemonkey scripts

See e.g. https://github.com/jerone/UserScripts/issues/135
This commit is contained in:
Florian Bruhin 2018-09-12 23:54:32 +02:00
parent d0d73eec04
commit f9327731b8
3 changed files with 22 additions and 1 deletions

View File

@ -59,6 +59,7 @@ Fixed
~~~~~
- Error when passing a substring with spaces to `:tab-take`.
- Greasemonkey scripts which start with an UTF-8 BOM are now handled correctly.
Removed
~~~~~~~

View File

@ -234,7 +234,7 @@ class GreasemonkeyManager(QObject):
if not os.path.isfile(script_filename):
continue
script_path = os.path.join(scripts_dir, script_filename)
with open(script_path, encoding='utf-8') as script_file:
with open(script_path, encoding='utf-8-sig') as script_file:
script = GreasemonkeyScript.parse(script_file.read())
if not script.name:
script.name = script_filename

View File

@ -130,6 +130,26 @@ def test_load_emits_signal(qtbot):
gm_manager.load_scripts()
def test_utf8_bom():
"""Make sure UTF-8 BOMs are stripped from scripts.
If we don't strip them, we'll have a BOM in the middle of the file, causing
QtWebEngine to not catch the "// ==UserScript==" line.
"""
script = textwrap.dedent("""
\N{BYTE ORDER MARK}// ==UserScript==
// @name qutebrowser test userscript
// ==/UserScript==
""".lstrip('\n'))
_save_script(script, 'bom.user.js')
gm_manager = greasemonkey.GreasemonkeyManager()
scripts = gm_manager.all_scripts()
assert len(scripts) == 1
script = scripts[0]
assert '// ==UserScript==' in script.code().splitlines()
def test_required_scripts_are_included(download_stub, tmpdir):
test_require_script = textwrap.dedent("""
// ==UserScript==