Skip to content

Commit

Permalink
Work around unsupported RegExp lookbehind in WebKit. (#85)
Browse files Browse the repository at this point in the history
* Work around unsupported RegExp lookbehind in WebKit.

* Update static/find/find.js

Co-authored-by: Mario Carneiro <di.gama@gmail.com>

Co-authored-by: Mario Carneiro <di.gama@gmail.com>
  • Loading branch information
Ruben-VandeVelde and digama0 committed Oct 20, 2022
1 parent d7fe046 commit 153982f
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions static/find/find.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,27 @@

import { DeclarationDataCenter } from "../declaration-data.js";

function leanFriendlyRegExp(c) {
try {
return new RegExp("(?<!«[^»]*)" + c);
} catch (e) {
if (e instanceof SyntaxError) {
// Lookbehind is not implemented yet in WebKit: https://bugs.webkit.org/show_bug.cgi?id=174931
// Fall back to less friendly regex.
return new RegExp(c);
}
throw e;
}
}

/**
* We don't use browser's default hash and searchParams in case Lean declaration name
* can be like `«#»`, rather we manually handle the `window.location.href` with regex.
*/
const LEAN_FRIENDLY_URL_REGEX = /^[^?#]+(?:\?((?:[^«#»]|«.*»)*))?(?:#(.*))?$/;
const LEAN_FRIENDLY_AND_SEPARATOR = /(?<!«[^»]*)&/;
const LEAN_FRIENDLY_EQUAL_SEPARATOR = /(?<!«[^»]*)=/;
const LEAN_FRIENDLY_SLASH_SEPARATOR = /(?<!«[^»]*)\//;
const LEAN_FRIENDLY_AND_SEPARATOR = leanFriendlyRegExp("&");
const LEAN_FRIENDLY_EQUAL_SEPARATOR = leanFriendlyRegExp("=");
const LEAN_FRIENDLY_SLASH_SEPARATOR = leanFriendlyRegExp("/");

const [_, query, fragment] = LEAN_FRIENDLY_URL_REGEX.exec(window.location.href);
const queryParams = new Map(
Expand Down

0 comments on commit 153982f

Please sign in to comment.