Commit Graph

214 Commits

Author SHA1 Message Date
Jonas Jenwald
aae8a21286 Revert "For mozcentral use Firefox color theme instead of system theme." since -moz-toolbar-prefers-color-scheme was removed
Reverts mozilla/pdf.js#13314, see https://groups.google.com/g/firefox-dev/c/vajhbYKDpPM

Given that `-moz-toolbar-prefers-color-scheme` was removed in https://bugzilla.mozilla.org/show_bug.cgi?id=1736038, unless we fix this before the next PDF.js update in mozilla-central we'll thus break dark mode in the Firefox built-in PDF Viewer.
2021-10-17 12:29:25 +02:00
Michael Wu
c08b4ea30d Fix Viewer API definitions and include in CI
The Viewer API definitions do not compile because of missing imports and
anonymous objects are typed as `Object`. These issues were not caught
during CI because the test project was not compiling anything from the
Viewer API.

As an example of the first problem:

```
/**
 * @implements MyInterface
 */
export class MyClass {
    ...
}
```

will generate a broken definition that doesn’t import MyInterface:

```
/**
 * @implements MyInterface
 */
export class MyClass implements MyInterface {
    ...
}
```

This can be fixed by adding a typedef jsdoc to specify the import:

```
/** @typedef {import("./otherFile").MyInterface} MyInterface */
```

See https://github.com/jsdoc/jsdoc/issues/1537 and
https://github.com/microsoft/TypeScript/issues/22160 for more details.

As an example of the second problem:

```
/**
 * Gets the size of the specified page, converted from PDF units to inches.
 * @param {Object} An Object containing the properties: {Array} `view`,
 *   {number} `userUnit`, and {number} `rotate`.
 */
function getPageSizeInches({ view, userUnit, rotate }) {
    ...
}
```

generates the broken definition:

```
function getPageSizeInches({ view, userUnit, rotate }: Object) {
    ...
}
```

The jsdoc should specify the type of each nested property:

```
/**
 * Gets the size of the specified page, converted from PDF units to inches.
 * @param {Object} options An object containing the properties: {Array} `view`,
 *   {number} `userUnit`, and {number} `rotate`.
 * @param {number[]} options.view
 * @param {number} options.userUnit
 * @param {number} options.rotate
 */
```
2021-08-25 18:45:46 -04:00
Michael Wu
acfb54a836 Fix pdf_viewer definitions
Current pdf_viewer definitions result in errors like the following when
trying to use them in a ts project:

[error] TypeScript error
node_modules/.pnpm/pdfjs-dist@2.10.377/node_modules/pdfjs-dist/web/pdf_viewer.d.ts:1:15
- error TS2691: An import path cannot end with a '.d.ts' extension.
Consider importing 'pdfjs-dist/types/web/pdf_viewer.component.js'
instead.

1 export * from "pdfjs-dist/types/web/pdf_viewer.component.d.ts";

Import/export statements in typescript should not include file extensions.
2021-08-20 12:23:43 -04:00
Tim van der Meij
0d84f57cfa
Fix inefficient regular expressions in external/cmapscompress/parse.js
The only purpose, according to the README and existing files, is to
parse an integer from those lines, so (\d+) is sufficient for that. This
avoids potential exponential backtracking as flagged by CodeQL. I have
compared the output of the script with and without these changes and the
resulting files are the same.
2021-08-01 12:12:42 +02:00
Rodolfo Rangel
a7c4fb4478 making webpack aware of the change inside of the worker file and hashing its name on build accordingly
removing .worker because it is already added in the [name] part
2021-07-08 13:32:58 -03:00
Jonas Jenwald
819be0e78b Fix the remaining ESLint operator-assignment errors 2021-07-04 15:23:56 +02:00
Jonas Jenwald
901b24e8af Enable the ESLint operator-assignment rule
This patch was generated automatically, using the `gulp lint --fix` command.

Please find additional details about the ESLint rule at https://eslint.org/docs/rules/operator-assignment
2021-07-04 12:57:45 +02:00
Jonas Jenwald
fdf2c59b8f Add a (basic) TypeScript definitions for the viewer components (issue 13267)
Please note that while the `gulp types`/`gulp typestest` tasks (obviously) still work with this patch, I've got no idea if the output is first of all even useable and secondly if it's actually useful in practice.
However, in the interest of closing some (seemingly simple) issues, I suppose that this probably shouldn't hurt (and we'd need TypeScript users to help improve things here).
2021-06-19 16:24:34 +02:00
Jonas Jenwald
16d9fa1bf1 Bundle the license-file for the Liberation fonts in the builds (PR 13517 follow-up)
Currently only the Foxit license-file is included, which is most likely just an oversight as far as I can tell.
Furthermore, to be able the tell the two license-files apart, the Foxit one is also renamed slightly.
2021-06-10 00:11:47 +02:00
Calixte Denizet
34a2fa72c7 XFA - Add Liberation-Sans font as a substitution for some missing fonts
- Some js files contain scale factors for each glyph in order to rescale Liberation to have a final font with the correct width.
  - A lot of XFA have some containers where their dimensions are based on their text content, so using default font from browser can lead to an almost unreadable pdf.
2021-06-09 16:55:45 +02:00
Brendan Dahl
4c1dd47e65 Include and use the 14 standard fonts files. 2021-06-07 11:10:11 -07:00
Jonas Jenwald
198ef044f8 Account for changes in eslint-plugin-import version 2.23.0
The simplest solution, as far as I can tell, to "fix" the new errors reported by the `no-unresolved` rules was to extend the existing whitelisting to cover the new cases. Given that the affected `imports` are only relevant in `gulp server`-mode, this should thus be completely fine.

Please find additional information at:
 - https://github.com/benmosher/eslint-plugin-import/releases/tag/v2.23.0
 - https://github.com/benmosher/eslint-plugin-import/blob/v2.23.0/CHANGELOG.md#2230---2021-05-13
2021-05-16 11:50:55 +02:00
Jonas Jenwald
8943bcd3c3 Account for formatting changes in Prettier version 2.3.0
With the exception of one tweaked `eslint-disable` comment, in `web/generic_scripting.js`, this patch was generated automatically using `gulp lint --fix`.

Please find additional information at:
 - https://github.com/prettier/prettier/releases/tag/2.3.0
 - https://prettier.io/blog/2021/05/09/2.3.0.html
2021-05-16 11:44:05 +02:00
Brendan Dahl
2c713f9cb5 For mozcentral use Firefox color theme instead of system theme.
See: https://bugzilla.mozilla.org/show_bug.cgi?id=1701691
2021-04-28 15:03:45 -07:00
calixteman
762cfd2d1b
[JS] Use heap allocation when initializing quickjs sandbox (#13286)
- In case of large string the sandbox initialization failed because of an OOM
    * so allocate a new string in the heap
    * and free it after use.
  - it requires a quickjs update since we need to export some symbols (stringToNewUTF8 and free).
2021-04-23 12:04:14 +02:00
Brendan Dahl
a31d142253 Cache babel source map.
When source is already cached and you reload,
the source map is lost which makes debugging
async functions difficult.
2021-04-09 10:34:54 -07:00
Calixte Denizet
a256948b30 Update quickjs to revision b5e62895c619d4ffc75c9d822c8d85f1ece77e5b 2021-03-28 12:43:46 +02:00
Jonas Jenwald
96d2a2f795 Enable the ESLint no-var rule in the external/ folder
These changes were done automatically, by using the `gulp lint --fix` command.
2021-03-14 11:43:25 +01:00
Jonas Jenwald
98e658ddf0 Enable linting of the external/cmapscompress/ folder
Given that this our "own" code, there's no good reason as far as I'm concerned to not lint it.
2021-03-13 19:32:58 +01:00
Jonas Jenwald
b9f4d89254 Fix *most* linting/formatting issues in the external/cmapscompress/ folder
These changes were done automatically, by using the `gulp lint --fix` command, in preparation for the next patch.
2021-03-13 19:24:23 +01:00
Jonas Jenwald
f0f307a4b5 Enable the ESLint no-var rule in the external/builder/ folder
As part of testing this, I've diffed the output of `gulp mozcentral` with/without this patch and the *only* difference is the incremented `version`/`build` numbers.
2021-03-13 17:50:13 +01:00
Jonas Jenwald
06494ccdac Replace *most* cases of var with let/const in the external/builder/ folder
These changes were done automatically, by using the `gulp lint --fix` command, in preparation for the next patch.
2021-03-13 17:47:58 +01:00
Tim van der Meij
fb78604383
Merge pull request #13091 from Snuffleupagus/eslint-no-var
Enable the ESLint `no-var` rule globally
2021-03-13 17:39:53 +01:00
Jonas Jenwald
2ffa428d78 Enable the ESLint no-var rule in the importL10n/ folder 2021-03-13 17:23:41 +01:00
Jonas Jenwald
5b5061afa8 Enable the ESLint no-var rule globally
A significant portion of the code-base has now been converted to use `let`/`const`, rather than `var`, hence it should be possible to simply enable the ESLint `no-var` rule globally.
This way we can ensure that new code won't accidentally use `var`, and it also removes the need to manually enable the rule in various folders.

Obviously it makes sense to continue the efforts to replace `var`, but that should probably happen on a file and/or folder basis.

Please note that this patch excludes the following code:
 - The `extensions/` folder, since that seemed easiest for now (and I don't know exactly what the support situation is for the Chromium-extension).

 - The entire `external/` folder is ignored, since most of it's currently excluded from linting.
   For the code that isn't imported from elsewhere (and should be ignored), we should probably (at some point) bring the code up to the same linting/formatting standard as the rest of the code-base.

 - Various files in the `test/` folder are ignored, as necessary, since the way that a lot of this code is loaded will require some care (or perhaps larger re-factoring) when removing `var` usage.
2021-03-13 16:12:53 +01:00
Jonas Jenwald
133158e4d5 Update the year in the license_header files 2021-02-11 17:52:26 +01:00
Jonas Jenwald
0068dba009 [api-minor] Rename -es5 to -legacy, to reduce confusion over what's actually supported (issue 12976)
*Please note that this will also require some edits of the Wiki.*
2021-02-10 16:01:59 +01:00
Jonas Jenwald
4db7330677 Enable ESLint rules that no longer need to be disabled on a directory/file-basis
Given that browsers/environments without native support for both arrow functions and object shorthand properties are no longer supported in PDF.js, please refer to the compatibility information below, we can now enable a fair number of ESLint rules and also simplify/remove some `.eslintrc` files.

With the exception of the `no-alert` cases, all code changes were made automatically by using `gulp lint --fix`.

 - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions#browser_compatibility
 - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer#browser_compatibility
2021-01-22 17:47:03 +01:00
Jonas Jenwald
5520737798 Update the main, and pdfjs-dist, README to also include recent ECMAScript features
This adds "optional chaining" and "nullish coalescing" to the list of features needed when using the *modern*, i.e. non-translated/non-polyfilled, build of the PDF.js library.
2021-01-19 17:49:12 +01:00
Jonas Jenwald
aabf768e53 Try to fix TypeScript definitions for the es5-build in pdfjs-dist (issue 12872)
Given that we're already using the `external/dist/` folder for things we simply want to copy to pdfjs-dist during building, this patch *should* hopefully work since it's based on the suggestion in issue 12827.

As long as this only requires a *single/small* file, to fix the TypeScript definitions in es5-builds, this solution seem acceptable as far as I'm concerned. (Although, please note that I don't know enough about TypeScript to actually test the patch.)
2021-01-08 09:29:47 +01:00
fabien
35b15cc0b5 1. Add filename option in worker-loader package require. Without this option, since version 3.0.0, it tell webpack to generate a worker file named pdf.worker.worker.js instead of the expected pdf.worker.js.
2. Update README of webpack example to mention that a version 3.0.0 or higher of the `worker-loader` package is now required.
2021-01-07 15:14:02 +01:00
Calixte Denizet
8bff4f1ea9 In order to simplify m-c code, move some in pdf.js
* move set/clear|Timeout/Interval and crackURL code in pdf.js
 * remove the "backdoor" in the proxy (used to dispatch event) and so return the dispatch function in the initializer
 * remove listeners if an error occured during sandbox initialization
 * add support for alert and prompt in the sandbox
 * add a function to eval in the global scope
2020-12-17 15:03:26 +01:00
Jonas Jenwald
c42029489e Run gulp lint --fix, to account for changes in Prettier version 2.2.1
Please refer to https://github.com/prettier/prettier/blob/master/CHANGELOG.md#221 for additional details.
2020-11-29 10:01:46 +01:00
Calixte Denizet
8413a5865a Add a README about quickjs-eval.js file (build and license) 2020-11-24 13:54:04 +01:00
Calixte Denizet
c7974e9996 JS -- Add a sandbox based on quickjs
* quickjs-eval.js has been generated using https://github.com/mozilla/pdf.js.quickjs/
 * lazy load of sandbox code
 * Rewrite tests to use the sandbox
 * Add a task `watch-sandbox` which update bundle pdf.sandbox.js on change in the sandbox code
2020-11-19 13:40:46 +01:00
Jonas Jenwald
de628cec59 Some hasJSActions, and general annotation-code, related cleanup in the viewer and API
- Add support for logical assignment operators, i.e. `&&=`, `||=`, and `??=`, with a Babel-plugin. Given that these required incrementing the ECMAScript version in the ESLint and Acorn configurations, and that platform/browser support is still fairly limited, always transpiling them seems appropriate for now.

 - Cache the `hasJSActions` promise in the API, similar to the existing `getAnnotations` caching. With this implemented, the lookup should now be cheap enough that it can be called unconditionally in the viewer.

 - Slightly improve cleanup of resources when destroying the `WorkerTransport`.

 - Remove the `annotationStorage`-property from the `PDFPageView` constructor, since it's not necessary and also brings it more inline with the `BaseViewer`.

 - Update the `BaseViewer.createAnnotationLayerBuilder` method to actaually agree with the `IPDFAnnotationLayerFactory` interface.[1]

 - Slightly tweak a couple of JSDoc comments.

---
[1] We probably ought to re-factor both the `IPDFTextLayerFactory` and `IPDFAnnotationLayerFactory` interfaces to take parameter objects instead, since especially the `IPDFAnnotationLayerFactory` one is becoming quite unwieldy. Given that that would likely be a breaking change for any custom viewer-components implementation, this probably requires careful deprecation.
2020-11-14 13:58:35 +01:00
Jonas Jenwald
9602844368 Enable the ESLint no-useless-escape rule (PR 12551 follow-up)
Note that a number of these cases are covered by existing unit-tests, and a few others only matter for the development/build scripts.
Furthermore, I've also tried to the best of my ability to test each case *manually* to hopefully further reduce the likelihood of this patch introducing any bugs.

Please find additional details about the ESLint rule at https://eslint.org/docs/rules/no-useless-escape
2020-11-07 13:06:24 +01:00
Jonas Jenwald
bc036c05bd Upgrade acorn to version 8
I've run `gulp mozcentral`, `gulp generic`, and `gulp generic-es5` with `master` respectively this patch and then diffed the build output. With the (obvious) exception of increased version/build numbers, there were no actual changes from the updated Acorn version.
2020-10-06 13:53:08 +02:00
Jonas Jenwald
449c7763d5 [api-minor] Only support browsers/environments that have *basic* support for Promise natively
Based on https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise#Browser_compatibility and https://caniuse.com/#feat=promises, all even remotely modern browsers already support *basic* `Promise` functionality natively.

The only reason for keeping the `Promise` polyfill (at all) is to be able to support recent additions to the specification, such as e.g. `finally` and `allSettled`.
Note that this patch will, on its own, remove support for IE 11/Edge (non-Chromium based) in both the general PDF.js library and the default viewer.
2020-09-06 13:45:56 +02:00
IanJiang0817
dee0c29f6f fix webpack config problem caused by breaking changes of worker-loader 2020-08-19 17:02:16 +08:00
Jonas Jenwald
85a67456ed Prevent the (old) preprocessor from appending trailing whitespace when removing closing HTML comments
This can currently be seen in the *built* `web/viewer.html` file, at the line containing "  <script src="viewer.js"></script>  ".
2020-06-11 12:15:18 +02:00
Alex Plumley
3b9031f6a3 Fix pdfjs-dist/webpack causing errors with certain configs
Using `require.resolve("worker-loader")` to check if `worker-loader` is installed causes webpack to include `worker-loader` in the output bundle, which is not the intended effect. Aside from increasing the bundle size unnecessarily, it also causes errors for webpack configs with targets that don't have node's built-in modules.

These errors can be fixed by configuring webpack `externals` to exclude `worker-loader`, but it's more difficult to figure out this solution than to figure out that `worker-loader` needs to be installed (even without this explicit error message).

To solve this, the explicit check for `worker-loader` has been removed. An alternative solution would be to use webpack's `resolveWeak`. Documentation has also been added in `examples/webpack` to help users.
2020-06-03 14:50:41 -04:00
Jonas Jenwald
d7dee0ea1c Remove the hasPrefixedFirefox functionality from the external/builder/builder.js file
This functionality has been completely unused ever since PR 9566 (two years ago).
2020-05-29 17:18:16 +02:00
Jonas Jenwald
ce234ab3c7 Remove the deprecatedInMozcentral functionality from the external/builder/builder.js file
This functionality has been completely unused ever since PR 9629 (two years ago).
2020-05-29 17:14:38 +02:00
Jonas Jenwald
8d56a69e74 Reduce usage of SystemJS, in the development viewer, even further
With these changes SystemJS is now only used, during development, on the worker-thread and in the unit/font-tests, since Firefox is currently missing support for worker modules; please see https://bugzilla.mozilla.org/show_bug.cgi?id=1247687

Hence all the JavaScript files in the `web/` and `src/display/` folders are now loaded *natively* by the browser (during development) using standard `import` statements/calls, thanks to a nice `import-maps` polyfill.

*Please note:* As soon as https://bugzilla.mozilla.org/show_bug.cgi?id=1247687 is fixed in Firefox, we should be able to remove all traces of SystemJS and thus finally be able to use every possible modern JavaScript feature.
2020-05-20 13:36:52 +02:00
Jonas Jenwald
426945b480 Update Prettier to version 2.0
Please note that these changes were done automatically, using `gulp lint --fix`.

Given that the major version number was increased, there's a fair number of (primarily whitespace) changes; please see https://prettier.io/blog/2020/03/21/2.0.0.html
In order to reduce the size of these changes somewhat, this patch maintains the old "arrowParens" style for now (once mozilla-central updates Prettier we can simply choose the same formatting, assuming it will differ here).
2020-04-14 12:28:14 +02:00
Jonas Jenwald
710704508c Fail early, in modern GENERIC builds, if certain required browser functionality is missing (issue 11762)
With two kind of builds now being produced, with/without translation/polyfills, it's unfortunately somewhat easy for users to accidentally pick the wrong one.

In the case where a user would attempt to use a modern build of PDF.js in an older browser, such as e.g. IE11, the failure would be immediate when the code is loaded (given the use of unsupported ECMAScript features).
However in some browsers/environments, in particular Node.js, a modern PDF.js build may load correctly and thus *appear* to function, only to fail for e.g. certain API calls. To hopefully lessen the support burden, and to try and improve things overall, this patch adds checks to ensure that a modern build of PDF.js cannot be used in browsers/environments which lack native support for critical functionality (such as e.g. `ReadableStream`). Hence we'll fail early, with an error message telling users to pick an ES5-compatible build instead.

To ensure that we actually test things better especially w.r.t. usage of the PDF.js library in Node.js environments, the `gulp npm-test` task as used by Node.js/Travis was changed (back) to test an ES5-compatible build.
(Since the bots still test the code as-is, without transpilation/polyfills, this shouldn't really be a problem as far as I can tell.)
As part of these changes there's now both `gulp lib` and `gulp lib-es5` build targets, similar to e.g. the generic builds, which thanks to some re-factoring only required adding a small amount of code.

*Please note:* While it's probably too early to tell if this will be a widespread issue, it's possible that this is the sort of patch that *may* warrant being `git cherry-pick`ed onto the current beta version (v2.4.456).
2020-04-01 19:42:48 +02:00
Jonas Jenwald
1d2f787d6a Enable the ESLint no-shadow rule
This rule is *not* currently enabled in mozilla-central, but it appears commented out[1] in the ESLint definition file; see https://searchfox.org/mozilla-central/rev/c80fa7258c935223fe319c5345b58eae85d4c6ae/tools/lint/eslint/eslint-plugin-mozilla/lib/configs/recommended.js#238-239

Unfortunately this rule is, for fairly obvious reasons, impossible to `--fix` automatically (even partially) and each case thus required careful manual analysis.
Hence this ESLint rule is, by some margin, probably the most difficult one that we've enabled thus far. However, using this rule does seem like a good idea in general since allowing variable shadowing could lead to subtle (and difficult to find) bugs or at the very least confusing code.

Please find additional details about the ESLint rule at https://eslint.org/docs/rules/no-shadow

---
[1] Most likely, a very large number of lint errors have prevented this rule from being enabled thus far.
2020-03-25 11:56:05 +01:00
Jonas Jenwald
c97c778f8f [api-minor] Produce non-translated/non-polyfilled builds by default 2020-02-14 18:12:07 +01:00
Jonas Jenwald
78afefbfb4 Add a README file to the /lib folder in pdfjs-dist (issue 11539) 2020-01-28 11:29:14 +01:00