-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Restore mobile PDF preview compatibility for browsers without Array.prototype.at
#729
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,42 @@ | ||||||||||||||||||||
| const atTargets = [ | ||||||||||||||||||||
| Array, | ||||||||||||||||||||
| String, | ||||||||||||||||||||
| Int8Array, | ||||||||||||||||||||
| Uint8Array, | ||||||||||||||||||||
| Uint8ClampedArray, | ||||||||||||||||||||
| Int16Array, | ||||||||||||||||||||
| Uint16Array, | ||||||||||||||||||||
| Int32Array, | ||||||||||||||||||||
| Uint32Array, | ||||||||||||||||||||
| Float32Array, | ||||||||||||||||||||
| Float64Array, | ||||||||||||||||||||
| globalThis.BigInt64Array, | ||||||||||||||||||||
| globalThis.BigUint64Array, | ||||||||||||||||||||
| ]; | ||||||||||||||||||||
|
Comment on lines
+11
to
+15
|
||||||||||||||||||||
|
|
||||||||||||||||||||
| function installAtPolyfill(target) { | ||||||||||||||||||||
| if (!target?.prototype || typeof target.prototype.at === "function") { | ||||||||||||||||||||
| return; | ||||||||||||||||||||
| } | ||||||||||||||||||||
| Object.defineProperty(target.prototype, "at", { | ||||||||||||||||||||
| value(index) { | ||||||||||||||||||||
| const length = this.length >>> 0; | ||||||||||||||||||||
| let relativeIndex = Number(index); | ||||||||||||||||||||
| if (Number.isNaN(relativeIndex)) { | ||||||||||||||||||||
| relativeIndex = 0; | ||||||||||||||||||||
| } | ||||||||||||||||||||
| relativeIndex = Math.trunc(relativeIndex); | ||||||||||||||||||||
|
Comment on lines
+25
to
+28
|
||||||||||||||||||||
| if (Number.isNaN(relativeIndex)) { | |
| relativeIndex = 0; | |
| } | |
| relativeIndex = Math.trunc(relativeIndex); | |
| if (relativeIndex !== relativeIndex) { | |
| relativeIndex = 0; | |
| } | |
| relativeIndex = | |
| relativeIndex < 0 ? Math.ceil(relativeIndex) : Math.floor(relativeIndex); |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,35 @@ | ||||||
| package cn.keking; | ||||||
|
|
||||||
| import org.junit.jupiter.api.Test; | ||||||
|
|
||||||
| import java.io.IOException; | ||||||
| import java.io.InputStream; | ||||||
| import java.nio.charset.StandardCharsets; | ||||||
|
|
||||||
| import static org.junit.jupiter.api.Assertions.assertNotNull; | ||||||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||||||
|
|
||||||
| public class PdfViewerCompatibilityTests { | ||||||
|
|
||||||
| @Test | ||||||
| void shouldLoadCompatibilityModuleBeforePdfJs() throws IOException { | ||||||
| String viewerHtml = readResource("/static/pdfjs/web/viewer.html"); | ||||||
|
|
||||||
| assertTrue(viewerHtml.contains("<script src=\"compatibility.mjs\" type=\"module\"></script>")); | ||||||
| assertTrue(viewerHtml.indexOf("compatibility.mjs") < viewerHtml.indexOf("../build/pdf.mjs")); | ||||||
| } | ||||||
|
Comment on lines
+15
to
+20
|
||||||
|
|
||||||
| @Test | ||||||
| void shouldLoadCompatibilityModuleInPdfWorker() throws IOException { | ||||||
| String workerScript = readResource("/static/pdfjs/build/pdf.worker.mjs"); | ||||||
|
|
||||||
| assertTrue(workerScript.contains("import \"../web/compatibility.mjs\";")); | ||||||
|
||||||
| assertTrue(workerScript.contains("import \"../web/compatibility.mjs\";")); | |
| assertTrue(workerScript.matches("(?s).*import\\s+[\"']\\.\\./web/compatibility\\.mjs[\"']\\s*;?.*")); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
Stringpolyfill usesthis[resolvedIndex], which returns UTF-16 code units; nativeString.prototype.atis code-point aware (handles surrogate pairs). SinceString.prototype.atis included inatTargets, either implement the code-point semantics or dropStringfrom the targets to avoid introducing subtly incorrect behavior.