Skip to content

Commit 2cc9823

Browse files
committed
Fixed small bug
Found why the DOMParser was not globally available - CJS works as ESM now.
1 parent 9f45d87 commit 2cc9823

5 files changed

Lines changed: 1923 additions & 2110 deletions

File tree

base/display/metadata.js

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,6 @@
1818

1919
'use strict';
2020

21-
// since this gets evaluated in pdfparser and DOMParser somehow does not exist in a CJS environment
22-
// we catch the error early and use @xmldom/xmldom DOMParser
23-
24-
// Not exactly sure how DOMParser is available globally in a node environment in the first place
25-
// feel free to change this if a better method is to be found
26-
try {
27-
var DomParser = DOMParser;
28-
} catch (error) {
29-
if (typeof module === 'object' && typeof module.exports === 'object') {
30-
// This is a CommonJS (CJS) environment
31-
DomParser = require('@xmldom/xmldom').DOMParser;
32-
} else {
33-
throw error;
34-
}
35-
}
36-
3721
var Metadata = (PDFJS.Metadata = (function MetadataClosure() {
3822
function fixMetadata(meta) {
3923
return meta.replace(/>\\376\\377([^<]+)/g, function (all, codes) {
@@ -65,7 +49,7 @@ var Metadata = (PDFJS.Metadata = (function MetadataClosure() {
6549
// Ghostscript produces invalid metadata
6650
meta = fixMetadata(meta);
6751

68-
var parser = new DomParser();
52+
var parser = new DOMParser();
6953
meta = parser.parseFromString(meta, 'application/xml');
7054
} else if (!(meta instanceof Document)) {
7155
error('Metadata: Invalid metadata object');

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@
4747
"parse-e2": "node --trace-deprecation --trace-warnings pdf2json.js -f ./test/pdf/misc/i243_problem_file_anon.pdf -o ./test/target/misc",
4848
"parse-e3": "node --trace-deprecation --trace-warnings pdf2json.js -f ./test/pdf/misc/i200_test.pdf -o ./test/target/misc",
4949
"build:rollup": "rollup -c ./rollup.config.js",
50-
"build": "npm run build:rollup"
50+
"build:add-destructured-imports-to-cjs": "node rollup/addDestructedImports.js",
51+
"build": "npm run build:rollup && npm run build:add-destructured-imports-to-cjs"
5152
},
5253
"engines": {
5354
"node": ">=18.12.1",
@@ -61,6 +62,8 @@
6162
"@xmldom/xmldom": "^0.8.8"
6263
},
6364
"devDependencies": {
65+
"@rollup/plugin-commonjs": "^25.0.4",
66+
"@rollup/plugin-node-resolve": "^15.2.1",
6467
"@rollup/plugin-replace": "^5.0.2",
6568
"@types/node": "^20.3.1",
6669
"lodash": "^4.17.21",

0 commit comments

Comments
 (0)