Commit Graph

231 Commits

Author SHA1 Message Date
Sam Magura
1c2d200918 [api-minor] Use new Worker() syntax in webpack entrypoint
This requires Webpack 5 and will break for anyone using Webpack 4.
worker-loader no longer needs to be installed.
2022-09-13 11:12:00 -04:00
Jonas Jenwald
6e31799948 [api-minor] Add the Babel targets-option to avoid transpiling code for unsupported browsers
Currently we simply use the Babel `preset-env` in the `legacy`-builds of the PDF.js library. This has the side-effect of transpiling the code for *very old* browsers/environments, including ones that we (since many years) no longer support which unnecessarily bloats the size of the `legacy`-builds.

For the CSS files we're only targeting *the supported browsers*, and it's thus possible to extend that to also apply to Babel.
One of the most significant changes, with this patch, is that we'll no longer polyfill `async`/`await` in the `legacy`-builds. However, this shouldn't be an issue given the browsers that we currently support in PDF.js; please refer to:
 - https://github.com/mozilla/pdf.js/wiki/Frequently-Asked-Questions#faq-support
 - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function#browser_compatibility
2022-08-19 22:19:43 +02:00
Jonas Jenwald
37ebc28756 Use more for...of loops in the code-base
Note that these cases, which are all in older code, were found using the [`unicorn/no-for-loop`](https://github.com/sindresorhus/eslint-plugin-unicorn/blob/main/docs/rules/no-for-loop.md) ESLint plugin rule.
However, note that I've opted not to enable this rule by default since there's still *some* cases where I do think that it makes sense to allow "regular" for-loops.
2022-07-17 16:18:54 +02:00
Jonas Jenwald
c21f4faaf8 Reduce unnecessary usage of Array.prototype.concat()
There are obviously cases where using `concat` makes perfect sense, since that method doesn't change any of the existing Arrays; see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/concat

However, in a few cases throughout the code-base that's not an issue and using `concat` only leads to unnecessary intermediate allocations. With modern JavaScript we can thus replace those with a combination of `push` and spread-syntax, which wasn't originally possible when the code was written.
2022-06-19 13:40:52 +02:00
Jonas Jenwald
4902ad8923 Use modern DOM methods a bit more (PR 15031 follow-up)
Apparently the ESLint rule added in PR 15031 wasn't able to catch all cases that can be converted, which is probably not all that surprising given how some of these call-sites look.

 - Use `Element.prepend()` to insert nodes before all other ones in the element, rather than using `firstChild` with `insertBefore`-calls; see https://developer.mozilla.org/en-US/docs/Web/API/Element/prepend

 - Fix one *incorrect* `insertBefore` call, in the AnnotationLayer-code.
   Initially the patch simply changed that to an `Element.before()`-call, however that broke one of the integration-tests. It turns out that the `index` may try to access a non-existent select-child, which triggers undefined behaviour; note the warning in https://developer.mozilla.org/en-US/docs/Web/API/Node/insertBefore#parameters
2022-06-13 10:47:37 +02:00
Jonas Jenwald
9ac4536693 Enable the unicorn/prefer-at ESLint plugin rule (PR 15008 follow-up)
Please find additional information here:
 - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/at
 - https://github.com/sindresorhus/eslint-plugin-unicorn/blob/main/docs/rules/prefer-at.md
2022-06-09 21:21:19 +02:00
Jonas Jenwald
397f2e63d0 Handle CSS-comments better in the preprocess-function (PR 14963 follow-up)
This fixes another oversight, please see the updated tests.
2022-06-02 16:06:47 +02:00
Jonas Jenwald
65fe0130f4 Handle CSS-comments correctly in the preprocess-function (PR 14886 follow-up)
I overlooked this in PR 14886, sorry about that!
2022-05-28 08:41:25 +02:00
Jonas Jenwald
ec6575db00 Avoid the preprocess-function adding consecutive blank lines
When pre-processor blocks are being removed, since they don't apply to the current build target, we may currently end up with consecutive blank lines.
While this is obviously not a big issue, it's nonetheless undesirable and we can adjust the `writeLine` function to prevent that.
2022-05-11 14:21:16 +02:00
Jonas Jenwald
527251d62b Update the preprocess-function to avoid adding trailing new-lines (issue 14902)
*This is a follow-up to PR 14886, which "broke" this.*

In addition to fixing the issue, using an Array and `join`-ing it at the end may also be a tiny bit more efficient than using a growing string.
2022-05-11 12:36:00 +02:00
Jonas Jenwald
d1f13a6af3 Use the regular preprocess-function for the CSS files as well
An old shortcoming of the `preprocessCSS`-function is its complete lack of support for our "normal" defines, which makes it very difficult to have build-specific CSS rules. Recently we've started using specially crafted comments to remove CSS rules from the MOZCENTRAL build, but (ab)using the `preprocessCSS`-function in this way really doesn't feel great.
However, it turns out to be surprisingly simple to instead use the "regular" `preprocess`-function for the CSS files as well. The only special-handling that's still necessary is the helper-function for dealing with CSS-imports, but apart from that everything seems to just work.

One reason, as far as I can tell, for having a separate `preprocessCSS`-function was likely that we originally used *lots* of vendor-prefixed CSS rules in our CSS files. With improvements over the years, especially thanks to Autoprefixer and PostCSS, we've been able to remove *almost* all non-standard CSS rules and the need for special-casing the CSS parsing has mostly vanished.

*Please note:* As part of testing this patch I've diffed the output of `gulp generic`, `gulp mozcentral`, and `gulp chromium` against the `master`-branch to check that there was no obvious breakage.
2022-05-07 22:45:52 +02:00
Calixte Denizet
2dba91e0ed Update quickjs to revision 2788d71e823b522b178db3b3660ce93689534e6d
- The date parser in quickjs is not optimal so use the navigator one.
2022-05-01 15:53:50 +02:00
Jonas Jenwald
5180bafb8f [gulpfile.js] Use the regular defines in the preprocessCSS function
Rather than *manually* specifying a "mode", we can simply use the regular `defines` directly instead. To improve consistency, in the `external/builder/builder.js` file, a couple of parameters are also re-named.
2022-03-16 22:39:48 +01:00
Jonas Jenwald
b89595fd20 [api-minor] Remove the, in legacy builds, bundled ReadableStream polyfill
According to the MDN compatibility data, see https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream#browser_compatibility, all browsers that we support have native `ReadableStream` implementations (since quite some time too).

Hence only Node.js is now lagging behind w.r.t. `ReadableStream` support, and its experimental implementation doesn't really help us given the life-span of the LTS releases (see https://en.wikipedia.org/wiki/Node.js#Releases).
It seems quite unfortunate to bundle a `ReadableStream` polyfill in the `legacy` builds when it's unnecessary in browsers, given its overall size, but fortunately we can avoid that by simply listing `web-streams-polyfill` as a dependency for the `pdfjs-dist` library.
2022-02-13 10:15:58 +01:00
Calixte Denizet
d9921a2bd5 Update quickjs sandbox
- compiled with the latest emscripten:
  - Digest:sha256:a28bd5ddf32c2b145b51503ddddb3c02804ab624d661abd54173b1f2dc6cbf06
2022-01-29 21:30:39 +01:00
Jonas Jenwald
00bd549e82 Update the year in the license_header files
This also includes a couple of files that are included as-is in the `pdfjs-dist` library.
2022-01-27 19:24:31 +01:00
Jonas Jenwald
00f8fab8a5 Add support for modern ECMAScript class features
With ESLint 8 we should now finally be able to start using modern `class` features, see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/Public_class_fields and https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/Private_class_fields

However, while both ESLint and Acorn now support this, it unfortunately turns out that Escodegen (which we use during building) still lack the necessary support. Looking at https://github.com/estools/escodegen there's not been any updates since last year, and there's also open PRs adding support for these new `class` features.

To avoid blocking usage of these `class` features in the PDF.js code-base, in particular *private* fields/methods, this patch thus proposes that we (hopefully temporarily) switch to an `escodegen` fork that has the necessary support; please see https://www.npmjs.com/package/@javascript-obfuscator/escodegen

While I have no reason to doubt the security of the `escodegen` fork, this patch nonetheless pins the version number. Furthermore, I've also diffed the output of the two `.js`-files in this forked package against the original files without finding anything that looks immediately "dangerous".
2021-10-22 22:01:17 +02:00
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