Skip to content

Commit

Permalink
Replace css_browser_selector with our own implementation (#2854)
Browse files Browse the repository at this point in the history
* Replace css_browser_selector

* Add classes to root element instead of body

* Export addBrowserClasses() in reviewer_extras for mobile clients (dae)

* Avoid setting .mac on iOS devices (dae)
  • Loading branch information
abdnh authored Nov 30, 2023
1 parent 1bca451 commit dc3bd3e
Show file tree
Hide file tree
Showing 11 changed files with 51 additions and 28 deletions.
1 change: 0 additions & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ In the qt folder:
* winpaths.py: MIT.
* MathJax: Apache 2.
* jQuery and jQuery-UI: MIT.
* browsersel.js: CC BY 2.5.
* plot.js: MIT.
* protobuf.js: BSD 3 clause

Expand Down
1 change: 0 additions & 1 deletion build/configure/src/aqt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,6 @@ fn build_vendor_js(build: &mut Build) -> Result<()> {
inputs: inputs![
":node_modules:jquery",
":node_modules:jquery-ui",
":node_modules:css-browser-selector",
":node_modules:bootstrap-dist",
"qt/aqt/data/web/js/vendor/plot.js"
],
Expand Down
3 changes: 0 additions & 3 deletions build/configure/src/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,6 @@ fn setup_node(build: &mut Build) -> Result<()> {
"jquery-ui" => vec![
"jquery-ui-dist/jquery-ui.min.js".into()
],
"css-browser-selector" => vec![
"css-browser-selector/css_browser_selector.min.js".into(),
],
"bootstrap-dist" => vec![
"bootstrap/dist/js/bootstrap.bundle.min.js".into(),
],
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@
"bootstrap-icons": "^1.10.5",
"canvas": "npm:empty-npm-package",
"codemirror": "^5.63.1",
"css-browser-selector": "^0.6.5",
"d3": "^7.0.0",
"fabric": "^5.3.0",
"intl-pluralrules": "^2.0.0",
Expand Down
6 changes: 1 addition & 5 deletions qt/aqt/mediasrv.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ def _extract_internal_request(
if ext == ".css":
additional_prefix = "css/"
elif ext == ".js":
if base in ("browsersel", "jquery-ui", "jquery", "plot"):
if base in ("jquery-ui", "jquery", "plot"):
additional_prefix = "js/vendor/"
else:
additional_prefix = "js/"
Expand All @@ -355,10 +355,6 @@ def _extract_internal_request(
base = "jquery-ui.min"
additional_prefix = "js/vendor/"

elif base == "browsersel":
base = "css_browser_selector.min"
additional_prefix = "js/vendor/"

if additional_prefix:
oldpath = path
path = f"{prefix}{additional_prefix}{base}{ext}"
Expand Down
8 changes: 0 additions & 8 deletions ts/licenses.json
Original file line number Diff line number Diff line change
Expand Up @@ -166,13 +166,6 @@
"path": "node_modules/commander",
"licenseFile": "node_modules/commander/LICENSE"
},
"[email protected]": {
"licenses": "CC-BY-SA-2.5",
"repository": "https://github.com/verbatim/css_browser_selector",
"publisher": "Rafael Lima",
"path": "node_modules/css-browser-selector",
"licenseFile": "node_modules/css-browser-selector/README.mkdn"
},
"[email protected]": {
"licenses": "MIT",
"repository": "https://github.com/NV/CSSOM",
Expand Down Expand Up @@ -840,4 +833,3 @@
"licenseFile": "node_modules/xmlchars/LICENSE"
}
}

44 changes: 44 additions & 0 deletions ts/reviewer/browser_selector.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright: Ankitects Pty Ltd and contributors
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html

export function addBrowserClasses() {
const ua = navigator.userAgent.toLowerCase();

function addClass(className: string) {
document.documentElement.classList.add(className);
}

function test(regex: RegExp): boolean {
return regex.test(ua);
}

if (test(/ipad/)) {
addClass("ipad");
} else if (test(/iphone/)) {
addClass("iphone");
} else if (test(/android/)) {
addClass("android");
}

if (test(/ipad|iphone|ipod/)) {
addClass("ios");
}

if (test(/ipad|iphone|ipod|android/)) {
addClass("mobile");
} else if (test(/linux/)) {
addClass("linux");
} else if (test(/windows/)) {
addClass("win");
} else if (test(/mac/)) {
addClass("mac");
}

if (test(/firefox\//)) {
addClass("firefox");
} else if (test(/chrome\//)) {
addClass("chrome");
} else if (test(/safari\//)) {
addClass("safari");
}
}
5 changes: 3 additions & 2 deletions ts/reviewer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
@typescript-eslint/no-explicit-any: "off",
*/

import "css-browser-selector/css_browser_selector.min";

export { default as $, default as jQuery } from "jquery/dist/jquery";

import { imageOcclusionAPI } from "../image-occlusion/review";
import { mutateNextCardStates } from "./answering";
import { addBrowserClasses } from "./browser_selector";

globalThis.anki = globalThis.anki || {};
globalThis.anki.mutateNextCardStates = mutateNextCardStates;
Expand Down Expand Up @@ -264,6 +263,8 @@ document.addEventListener("focusout", (event) => {
}
});

addBrowserClasses();

registerPackage("anki/reviewer", {
// If you append a function to this each time the question or answer
// is shown, it will be called before MathJax has been rendered.
Expand Down
2 changes: 2 additions & 0 deletions ts/reviewer/reviewer_extras.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@

import { imageOcclusionAPI } from "../image-occlusion/review";
import { mutateNextCardStates } from "./answering";
import { addBrowserClasses } from "./browser_selector";

globalThis.anki = globalThis.anki || {};
globalThis.anki.mutateNextCardStates = mutateNextCardStates;
globalThis.anki.imageOcclusion = imageOcclusionAPI;
globalThis.anki.setupImageCloze = imageOcclusionAPI.setup; // deprecated
globalThis.anki.addBrowserClasses = addBrowserClasses;
3 changes: 1 addition & 2 deletions ts/reviewer/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
"include": ["*"],
// "references": [{ "path": "../lib" }],
"compilerOptions": {
// css-browser-selector fails if our output bundle is strict
"alwaysStrict": false,
"alwaysStrict": true,
"composite": false,
"types": ["jest"]
}
Expand Down
5 changes: 0 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1900,11 +1900,6 @@ cross-spawn@^7.0.0, cross-spawn@^7.0.1, cross-spawn@^7.0.2, cross-spawn@^7.0.3:
shebang-command "^2.0.0"
which "^2.0.1"

css-browser-selector@^0.6.5:
version "0.6.5"
resolved "https://registry.yarnpkg.com/css-browser-selector/-/css-browser-selector-0.6.5.tgz#d2b06c01ff7d7cb455e2006b6b6558d39372ca60"
integrity sha512-Cfn76BCNJb7GAd57nKT0abzW1yZCpk2+5TgjZrOt01RErG1lAxDs5rQ6tnh3PD4Zfg4pIcsQ/ED/QK+MeNVgYw==

css-tree@^2.3.1:
version "2.3.1"
resolved "https://registry.yarnpkg.com/css-tree/-/css-tree-2.3.1.tgz#10264ce1e5442e8572fc82fbe490644ff54b5c20"
Expand Down

0 comments on commit dc3bd3e

Please sign in to comment.