From 6e89107a9c750bd60c5af472590662c2d675d596 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Tue, 29 Nov 2022 08:23:42 +0100 Subject: Add a quirk for Array.at Trivial to polyfill, needed by various pages in the wild, and only natively available with Qt 6.2. Closes #7501 Also see #7237 and 726d5e614b1f80c339084eba7d1c240bf467fa9b (cherry picked from commit ce64e5d899666bd469bf07bd0fecbc75d4bfed3a) --- qutebrowser/browser/webengine/webenginetab.py | 6 ++- qutebrowser/javascript/.eslintrc.yaml | 2 +- qutebrowser/javascript/quirks/array_at.user.js | 58 ++++++++++++++++++++++++++ tests/unit/javascript/test_js_quirks.py | 6 +++ 4 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 qutebrowser/javascript/quirks/array_at.user.js diff --git a/qutebrowser/browser/webengine/webenginetab.py b/qutebrowser/browser/webengine/webenginetab.py index 7d355d10e..75705eeb3 100644 --- a/qutebrowser/browser/webengine/webenginetab.py +++ b/qutebrowser/browser/webengine/webenginetab.py @@ -1220,7 +1220,11 @@ class _WebEngineScripts(QObject): _Quirk( 'object_fromentries', predicate=versions.webengine < utils.VersionNumber(5, 13), - ) + ), + _Quirk( + 'array_at', + predicate=versions.webengine < utils.VersionNumber(6, 2), + ), ] for quirk in quirks: diff --git a/qutebrowser/javascript/.eslintrc.yaml b/qutebrowser/javascript/.eslintrc.yaml index 939500aa3..0a7d7c5d8 100644 --- a/qutebrowser/javascript/.eslintrc.yaml +++ b/qutebrowser/javascript/.eslintrc.yaml @@ -29,7 +29,7 @@ rules: init-declarations: "off" no-plusplus: "off" no-extra-parens: "off" - id-length: ["error", {"exceptions": ["i", "k", "v", "x", "y"]}] + id-length: ["error", {"exceptions": ["i", "n", "k", "v", "x", "y"]}] object-shorthand: "off" max-statements: ["error", {"max": 40}] quotes: ["error", "double", {"avoidEscape": true}] diff --git a/qutebrowser/javascript/quirks/array_at.user.js b/qutebrowser/javascript/quirks/array_at.user.js new file mode 100644 index 000000000..0badebd77 --- /dev/null +++ b/qutebrowser/javascript/quirks/array_at.user.js @@ -0,0 +1,58 @@ +// Based on: https://github.com/tc39/proposal-relative-indexing-method#polyfill + +/* +Copyright (c) 2020 Tab Atkins Jr. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +/* eslint-disable no-invalid-this */ + +"use strict"; + +(function() { + function at(idx) { + // ToInteger() abstract op + let n = Math.trunc(idx) || 0; + // Allow negative indexing from the end + if (n < 0) { + n += this.length; + } + // OOB access is guaranteed to return undefined + if (n < 0 || n >= this.length) { + return undefined; + } + // Otherwise, this is just normal property access + return this[n]; + } + + const TypedArray = Reflect.getPrototypeOf(Int8Array); + for (const type of [Array, String, TypedArray]) { + Object.defineProperty( + type.prototype, + "at", + { + "value": at, + "writable": true, + "enumerable": false, + "configurable": true, + } + ); + } +})(); diff --git a/tests/unit/javascript/test_js_quirks.py b/tests/unit/javascript/test_js_quirks.py index 7036dcfc9..03c3c1493 100644 --- a/tests/unit/javascript/test_js_quirks.py +++ b/tests/unit/javascript/test_js_quirks.py @@ -61,6 +61,12 @@ from qutebrowser.utils import usertypes {'0': 'a', '1': 'b'}, id='object-fromentries', ), + pytest.param( + QUrl("https://test.qutebrowser.org/linkedin"), + '[1, 2, 3].at(1)', + 2, + id='array-at', + ), ]) def test_js_quirks(config_stub, js_tester_webengine, base_url, source, expected): config_stub.val.content.site_specific_quirks.skip = [] -- cgit v1.2.3-54-g00ecf