mirror of
https://github.com/koodo-reader/koodo-reader.git
synced 2026-10-06 04:46:18 -04:00
Refactor sentence splitting to avoid unsupported lookbehind in low-version iOS Safari
- Updated `splitSentences` function in `common.ts` to replace lookbehind regex with a compatible approach for older browsers. - Modified `getSelectionSentence` function in `mouseEvent.ts` to similarly avoid lookbehind, ensuring better compatibility across different environments.
This commit is contained in:
1 parent
b38e2c143c
commit
fd15bdcab2
3 files changed
+16
-6
No files matched your search
Vendored
+1
-1
File diff suppressed because one or more lines are too long.
+9
-4
@@ -1586,10 +1586,15 @@ export const splitSentences = (text: string, maxLength?: number) => {
|
||||
if (sentence.length <= resolvedMaxLength) return [sentence];
|
||||
|
||||
// Try splitting by common punctuation marks (Chinese and Western)
|
||||
const parts = sentence
|
||||
.split(/(?<=[,,;;::、…])/)
|
||||
.map((s) => s.trim())
|
||||
.filter((s) => s.length > 0);
|
||||
// Avoid lookbehind (?<=) which is unsupported on low-version iOS Safari
|
||||
const rawParts = sentence.split(/([,,;;::、…])/);
|
||||
const parts = [] as string[];
|
||||
for (let i = 0; i < rawParts.length; i += 2) {
|
||||
const merged = (rawParts[i] + (rawParts[i + 1] ?? "")).trim();
|
||||
if (merged.length > 0) {
|
||||
parts.push(merged);
|
||||
}
|
||||
}
|
||||
|
||||
if (parts.length > 1) {
|
||||
// Greedily merge parts to minimize the number of resulting chunks
|
||||
|
||||
@@ -72,7 +72,12 @@ export const getSelectionSentence = (
|
||||
let fullText = (el as Element)?.textContent || "";
|
||||
let selectedText = sel.toString().trim();
|
||||
// Split on sentence-ending punctuation to find the sentence
|
||||
let sentences = fullText.split(/(?<=[.!?。!?])\s*/);
|
||||
// Avoid lookbehind (?<=) which is unsupported on low-version iOS Safari
|
||||
const rawParts = fullText.split(/([.!?。!?])\s*/);
|
||||
const sentences: string[] = [];
|
||||
for (let i = 0; i < rawParts.length; i += 2) {
|
||||
sentences.push(rawParts[i] + (rawParts[i + 1] ?? ""));
|
||||
}
|
||||
for (let s of sentences) {
|
||||
if (s.includes(selectedText)) {
|
||||
return s.trim();
|
||||
|
||||
Reference in new issue
Block a user