-
Notifications
You must be signed in to change notification settings - Fork 6
[codex] fix mobile search on older iOS #449
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5049cef
fix: polyfill RegExp.escape for mobile search
tomusdrw d1b86d9
test: auto-update visual regression snapshots
github-actions[bot] 14dd68a
Merge remote-tracking branch 'origin/main' into issue-446
tomusdrw 805b44e
Merge remote-tracking branch 'origin/issue-446' into issue-446
tomusdrw 7e5b111
fix: handle sparse pdf search matches
tomusdrw 537618d
fix: polyfill ReadableStream async iteration for Safari
tomusdrw 3357552
chore: remove RegExp.escape polyfill
tomusdrw 747e76b
fix: preserve cleared search state
tomusdrw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
| import { getMatchSummary } from "./Search"; | ||
|
|
||
| describe("getMatchSummary", () => { | ||
| it("summarizes page matches", () => { | ||
| expect(getMatchSummary([[2, 8], [], [1]])).toEqual({ | ||
| count: 3, | ||
| pagesAndCount: [ | ||
| { pageIndex: 0, firstMatchIndex: 2, count: 2 }, | ||
| { pageIndex: 2, firstMatchIndex: 1, count: 1 }, | ||
| ], | ||
| }); | ||
| }); | ||
|
|
||
| it("ignores sparse and undefined page entries while pdf.js is still calculating matches", () => { | ||
| const pageMatches: Array<number[] | undefined> = []; | ||
| pageMatches[0] = [4]; | ||
| pageMatches[2] = undefined; | ||
| pageMatches[4] = [1, 3, 5]; | ||
|
|
||
| expect(getMatchSummary(pageMatches)).toEqual({ | ||
| count: 4, | ||
| pagesAndCount: [ | ||
| { pageIndex: 0, firstMatchIndex: 4, count: 1 }, | ||
| { pageIndex: 4, firstMatchIndex: 1, count: 3 }, | ||
| ], | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| import { afterEach, describe, expect, it } from "vitest"; | ||
| import { installReadableStreamAsyncIteratorPolyfill } from "./readableStreamAsyncIteratorPolyfill"; | ||
|
|
||
| const readableStreamPrototype = ReadableStream.prototype as ReadableStream<unknown> & { | ||
| values?: (options?: { preventCancel?: boolean }) => AsyncIterableIterator<unknown>; | ||
| [Symbol.asyncIterator]?: () => AsyncIterableIterator<unknown>; | ||
| }; | ||
|
|
||
| const originalValues = Object.getOwnPropertyDescriptor(readableStreamPrototype, "values"); | ||
| const originalAsyncIterator = Object.getOwnPropertyDescriptor(readableStreamPrototype, Symbol.asyncIterator); | ||
|
|
||
| function restoreProperty(property: "values" | typeof Symbol.asyncIterator, descriptor: PropertyDescriptor | undefined) { | ||
| if (descriptor) { | ||
| Object.defineProperty(readableStreamPrototype, property, descriptor); | ||
| } else { | ||
| delete readableStreamPrototype[property]; | ||
| } | ||
| } | ||
|
|
||
| async function collectStreamValues<T>(stream: ReadableStream<T>): Promise<T[]> { | ||
| const values: T[] = []; | ||
| for await (const value of stream) { | ||
| values.push(value); | ||
| } | ||
| return values; | ||
| } | ||
|
|
||
| describe("installReadableStreamAsyncIteratorPolyfill", () => { | ||
| afterEach(() => { | ||
| restoreProperty("values", originalValues); | ||
| restoreProperty(Symbol.asyncIterator, originalAsyncIterator); | ||
| }); | ||
|
|
||
| it("adds async iteration support when ReadableStream.values and Symbol.asyncIterator are missing", async () => { | ||
| Reflect.deleteProperty(readableStreamPrototype, "values"); | ||
| Reflect.deleteProperty(readableStreamPrototype, Symbol.asyncIterator); | ||
|
|
||
| installReadableStreamAsyncIteratorPolyfill(); | ||
|
|
||
| expect(typeof readableStreamPrototype.values).toBe("function"); | ||
| expect(typeof readableStreamPrototype[Symbol.asyncIterator]).toBe("function"); | ||
| expect( | ||
| await collectStreamValues( | ||
| new ReadableStream<string>({ | ||
| start(controller) { | ||
| controller.enqueue("hello"); | ||
| controller.enqueue("safari"); | ||
| controller.close(); | ||
| }, | ||
| }), | ||
| ), | ||
| ).toEqual(["hello", "safari"]); | ||
| }); | ||
|
|
||
| it("does not overwrite native implementations", () => { | ||
| const values = function* values() {}; | ||
| const asyncIterator = function* asyncIterator() {}; | ||
|
|
||
| Object.defineProperty(readableStreamPrototype, "values", { | ||
| configurable: true, | ||
| writable: true, | ||
| value: values, | ||
| }); | ||
| Object.defineProperty(readableStreamPrototype, Symbol.asyncIterator, { | ||
| configurable: true, | ||
| writable: true, | ||
| value: asyncIterator, | ||
| }); | ||
|
|
||
| installReadableStreamAsyncIteratorPolyfill(); | ||
|
|
||
| expect(readableStreamPrototype.values).toBe(values); | ||
| expect(readableStreamPrototype[Symbol.asyncIterator]).toBe(asyncIterator); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| type ReadableStreamIteratorOptions = { | ||
| preventCancel?: boolean; | ||
| }; | ||
|
|
||
| type ReadableStreamPrototypeWithIterator = ReadableStream<unknown> & { | ||
| values?: <T>(this: ReadableStream<T>, options?: ReadableStreamIteratorOptions) => AsyncIterableIterator<T>; | ||
| [Symbol.asyncIterator]?: <T>(this: ReadableStream<T>) => AsyncIterableIterator<T>; | ||
| }; | ||
|
|
||
| function readableStreamValues<T>( | ||
| this: ReadableStream<T>, | ||
| options: ReadableStreamIteratorOptions = {}, | ||
| ): AsyncIterableIterator<T> { | ||
| const reader = this.getReader(); | ||
| let isDone = false; | ||
|
|
||
| const iterator: AsyncIterableIterator<T> = { | ||
| async next() { | ||
| if (isDone) { | ||
| return { done: true, value: undefined as T }; | ||
| } | ||
|
|
||
| const result = await reader.read(); | ||
| if (result.done) { | ||
| isDone = true; | ||
| reader.releaseLock(); | ||
| return { done: true, value: undefined as T }; | ||
| } | ||
|
|
||
| return { done: false, value: result.value }; | ||
| }, | ||
|
|
||
| async return(value?: unknown) { | ||
| if (!isDone) { | ||
| isDone = true; | ||
| try { | ||
| if (!options.preventCancel) { | ||
| await reader.cancel(value); | ||
| } | ||
| } finally { | ||
| reader.releaseLock(); | ||
| } | ||
| } | ||
|
|
||
| return { done: true, value: value as T }; | ||
| }, | ||
|
|
||
| [Symbol.asyncIterator]() { | ||
| return this; | ||
| }, | ||
| }; | ||
|
|
||
| return iterator; | ||
| } | ||
|
|
||
| /** | ||
| * Polyfill `ReadableStream` async iteration for Safari versions that expose streams but not | ||
| * `ReadableStream.prototype[Symbol.asyncIterator]` / `.values()`. | ||
| * | ||
| * pdf.js' `PDFPageProxy.getTextContent` consumes text streams with `for await (... of stream)`. | ||
| * Without this API Safari throws `TypeError: undefined is not a function` and search indexing | ||
| * fails with "Unable to get text content for page ...". | ||
| */ | ||
| export function installReadableStreamAsyncIteratorPolyfill(): void { | ||
| if (typeof ReadableStream === "undefined" || typeof Symbol.asyncIterator === "undefined") { | ||
| return; | ||
| } | ||
|
|
||
| const prototype = ReadableStream.prototype as ReadableStreamPrototypeWithIterator; | ||
|
|
||
| if (typeof prototype.values !== "function") { | ||
| Object.defineProperty(prototype, "values", { | ||
| configurable: true, | ||
| writable: true, | ||
| value: readableStreamValues, | ||
| }); | ||
| } | ||
|
|
||
| if (typeof prototype[Symbol.asyncIterator] !== "function") { | ||
| Object.defineProperty(prototype, Symbol.asyncIterator, { | ||
| configurable: true, | ||
| writable: true, | ||
| value: prototype.values, | ||
| }); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.