summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlorian Bruhin <me@the-compiler.org>2022-11-29 08:23:42 +0100
committerFlorian Bruhin <me@the-compiler.org>2022-11-29 08:57:29 +0100
commit68dbde61fd998334066a500d77fb14a4e0400d2c (patch)
tree6bf9454c82264d9af145ac2fef8477f2cd3a8848
parent725ed95d8af99b522e5b7c2669e92742a345e41a (diff)
downloadqutebrowser-68dbde61fd998334066a500d77fb14a4e0400d2c.tar.gz
qutebrowser-68dbde61fd998334066a500d77fb14a4e0400d2c.zip
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)
-rw-r--r--qutebrowser/browser/webengine/webenginetab.py6
-rw-r--r--qutebrowser/javascript/.eslintrc.yaml2
-rw-r--r--qutebrowser/javascript/quirks/array_at.user.js58
-rw-r--r--tests/unit/javascript/test_js_quirks.py6
4 files changed, 70 insertions, 2 deletions
diff --git a/qutebrowser/browser/webengine/webenginetab.py b/qutebrowser/browser/webengine/webenginetab.py
index 5c5d9ccfc..3ff1547d3 100644
--- a/qutebrowser/browser/webengine/webenginetab.py
+++ b/qutebrowser/browser/webengine/webenginetab.py
@@ -1211,7 +1211,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 43fd1b6e6..566304c27 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 52b9a090f..35ce37f16 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 = []