pdfjs-dist 2.6.347 → 2.7.570
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of pdfjs-dist might be problematic. Click here for more details.
- package/README.md +3 -3
- package/bower.json +1 -1
- package/build/pdf.js +2050 -1087
- package/build/pdf.js.map +1 -1
- package/build/pdf.min.js +1 -1
- package/build/pdf.worker.js +18219 -10436
- package/build/pdf.worker.js.map +1 -1
- package/build/pdf.worker.min.js +1 -1
- package/es5/build/pdf.d.ts +1 -0
- package/es5/build/pdf.js +6623 -6654
- package/es5/build/pdf.js.map +1 -1
- package/es5/build/pdf.min.js +1 -1
- package/es5/build/pdf.worker.js +20815 -13888
- package/es5/build/pdf.worker.js.map +1 -1
- package/es5/build/pdf.worker.min.js +1 -1
- package/es5/image_decoders/pdf.image_decoders.js +3817 -4946
- package/es5/image_decoders/pdf.image_decoders.js.map +1 -1
- package/es5/image_decoders/pdf.image_decoders.min.js +1 -1
- package/es5/web/pdf_viewer.css +18 -15
- package/es5/web/pdf_viewer.js +1094 -514
- package/es5/web/pdf_viewer.js.map +1 -1
- package/image_decoders/pdf.image_decoders.js +774 -168
- package/image_decoders/pdf.image_decoders.js.map +1 -1
- package/image_decoders/pdf.image_decoders.min.js +1 -1
- package/lib/core/annotation.js +556 -108
- package/lib/core/cff_parser.js +7 -1
- package/lib/core/charsets.js +1 -1
- package/lib/core/cmap.js +20 -1
- package/lib/core/core_utils.js +162 -3
- package/lib/core/crypto.js +1 -1
- package/lib/core/default_appearance.js +132 -0
- package/lib/core/document.js +115 -9
- package/lib/core/encodings.js +1 -1
- package/lib/core/evaluator.js +168 -74
- package/lib/core/fonts.js +97 -11
- package/lib/core/function.js +5 -10
- package/lib/core/glyphlist.js +11 -4529
- package/lib/core/image_utils.js +30 -1
- package/lib/core/jpg.js +1 -1
- package/lib/core/jpx.js +5 -5
- package/lib/core/murmurhash3.js +1 -1
- package/lib/core/obj.js +123 -39
- package/lib/core/pattern.js +4 -4
- package/lib/core/primitives.js +24 -5
- package/lib/core/standard_fonts.js +1 -1
- package/lib/core/stream.js +5 -1
- package/lib/core/unicode.js +15 -1387
- package/lib/core/worker.js +58 -17
- package/lib/core/writer.js +68 -4
- package/lib/display/annotation_layer.js +712 -119
- package/lib/display/annotation_storage.js +21 -4
- package/lib/display/api.js +88 -18
- package/lib/display/canvas.js +414 -375
- package/lib/display/display_utils.js +11 -4
- package/lib/display/fetch_stream.js +3 -3
- package/lib/display/font_loader.js +2 -3
- package/lib/display/metadata.js +54 -20
- package/lib/display/node_stream.js +1 -1
- package/lib/display/optional_content_config.js +1 -1
- package/lib/display/pattern_helper.js +109 -113
- package/lib/display/svg.js +5 -5
- package/lib/display/text_layer.js +54 -54
- package/lib/display/transport_stream.js +4 -4
- package/lib/display/webgl.js +65 -68
- package/lib/examples/node/domstubs.js +9 -4
- package/lib/pdf.js +2 -2
- package/lib/pdf.sandbox.js +311 -0
- package/lib/pdf.worker.js +2 -2
- package/lib/shared/scripting_utils.js +84 -0
- package/lib/shared/util.js +129 -14
- package/lib/{display → shared}/xml_parser.js +112 -4
- package/lib/test/unit/annotation_spec.js +831 -109
- package/lib/test/unit/annotation_storage_spec.js +28 -10
- package/lib/test/unit/api_spec.js +190 -160
- package/lib/test/unit/bidi_spec.js +6 -6
- package/lib/test/unit/cff_parser_spec.js +73 -73
- package/lib/test/unit/clitests_helper.js +2 -0
- package/lib/test/unit/cmap_spec.js +48 -74
- package/lib/test/unit/core_utils_spec.js +34 -0
- package/lib/test/unit/crypto_spec.js +162 -199
- package/lib/test/unit/custom_spec.js +7 -18
- package/lib/test/unit/default_appearance_spec.js +54 -0
- package/lib/test/unit/display_svg_spec.js +24 -19
- package/lib/test/unit/display_utils_spec.js +1 -1
- package/lib/test/unit/document_spec.js +187 -20
- package/lib/test/unit/evaluator_spec.js +30 -30
- package/lib/test/unit/function_spec.js +165 -165
- package/lib/test/unit/jasmine-boot.js +52 -53
- package/lib/test/unit/metadata_spec.js +2 -2
- package/lib/test/unit/murmurhash3_spec.js +29 -16
- package/lib/test/unit/network_spec.js +21 -21
- package/lib/test/unit/pdf_find_controller_spec.js +131 -69
- package/lib/test/unit/pdf_find_utils_spec.js +10 -10
- package/lib/test/unit/scripting_spec.js +1104 -0
- package/lib/test/unit/stream_spec.js +8 -8
- package/lib/test/unit/test_utils.js +16 -19
- package/lib/test/unit/testreporter.js +11 -4
- package/lib/test/unit/type1_parser_spec.js +23 -23
- package/lib/test/unit/ui_utils_spec.js +78 -35
- package/lib/test/unit/unicode_spec.js +7 -7
- package/lib/test/unit/util_spec.js +26 -3
- package/lib/test/unit/writer_spec.js +16 -1
- package/lib/test/unit/xml_spec.js +117 -0
- package/lib/web/annotation_layer_builder.js +18 -6
- package/lib/web/app.js +579 -161
- package/lib/web/app_options.js +14 -0
- package/lib/web/base_tree_viewer.js +50 -0
- package/lib/web/base_viewer.js +350 -14
- package/lib/web/chromecom.js +9 -1
- package/lib/web/debugger.js +1 -2
- package/lib/web/download_manager.js +0 -15
- package/lib/web/firefox_print_service.js +6 -4
- package/lib/web/firefoxcom.js +84 -69
- package/lib/web/generic_scripting.js +55 -0
- package/lib/web/genericcom.js +9 -1
- package/lib/web/grab_to_pan.js +1 -1
- package/lib/web/interfaces.js +9 -3
- package/lib/web/pdf_attachment_viewer.js +1 -3
- package/lib/web/pdf_cursor_tools.js +20 -13
- package/lib/web/pdf_document_properties.js +48 -61
- package/lib/web/pdf_find_bar.js +1 -3
- package/lib/web/pdf_find_controller.js +58 -12
- package/lib/web/pdf_history.js +43 -21
- package/lib/web/pdf_layer_viewer.js +1 -9
- package/lib/web/pdf_link_service.js +108 -78
- package/lib/web/pdf_outline_viewer.js +166 -10
- package/lib/web/pdf_page_view.js +14 -14
- package/lib/web/pdf_presentation_mode.js +21 -31
- package/lib/web/pdf_rendering_queue.js +8 -1
- package/lib/web/pdf_sidebar.js +62 -107
- package/lib/web/pdf_sidebar_resizer.js +11 -21
- package/lib/web/pdf_single_page_viewer.js +8 -0
- package/lib/web/pdf_thumbnail_view.js +26 -26
- package/lib/web/pdf_thumbnail_viewer.js +13 -2
- package/lib/web/pdf_viewer.component.js +2 -2
- package/lib/web/pdf_viewer.js +3 -1
- package/lib/web/preferences.js +33 -44
- package/lib/web/text_layer_builder.js +2 -9
- package/lib/web/ui_utils.js +78 -46
- package/lib/web/viewer_compatibility.js +1 -2
- package/package.json +4 -1
- package/types/display/annotation_layer.d.ts +18 -3
- package/types/display/api.d.ts +110 -54
- package/types/display/canvas.d.ts +1 -1
- package/types/display/display_utils.d.ts +96 -95
- package/types/display/fetch_stream.d.ts +2 -2
- package/types/display/metadata.d.ts +4 -0
- package/types/display/pattern_helper.d.ts +1 -1
- package/types/display/text_layer.d.ts +7 -7
- package/types/display/transport_stream.d.ts +1 -1
- package/types/shared/scripting_utils.d.ts +12 -0
- package/types/shared/util.d.ts +281 -250
- package/types/shared/xml_parser.d.ts +64 -0
- package/web/pdf_viewer.css +18 -15
- package/web/pdf_viewer.js +809 -408
- package/web/pdf_viewer.js.map +1 -1
- package/webpack.js +1 -1
- package/types/display/xml_parser.d.ts +0 -35
package/lib/pdf.worker.js
CHANGED
@@ -0,0 +1,84 @@
|
|
1
|
+
/**
|
2
|
+
* @licstart The following is the entire license notice for the
|
3
|
+
* Javascript code in this page
|
4
|
+
*
|
5
|
+
* Copyright 2020 Mozilla Foundation
|
6
|
+
*
|
7
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
8
|
+
* you may not use this file except in compliance with the License.
|
9
|
+
* You may obtain a copy of the License at
|
10
|
+
*
|
11
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
12
|
+
*
|
13
|
+
* Unless required by applicable law or agreed to in writing, software
|
14
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
15
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
16
|
+
* See the License for the specific language governing permissions and
|
17
|
+
* limitations under the License.
|
18
|
+
*
|
19
|
+
* @licend The above is the entire license notice for the
|
20
|
+
* Javascript code in this page
|
21
|
+
*/
|
22
|
+
"use strict";
|
23
|
+
|
24
|
+
Object.defineProperty(exports, "__esModule", {
|
25
|
+
value: true
|
26
|
+
});
|
27
|
+
exports.ColorConverters = void 0;
|
28
|
+
|
29
|
+
function makeColorComp(n) {
|
30
|
+
return Math.floor(Math.max(0, Math.min(1, n)) * 255).toString(16).padStart(2, "0");
|
31
|
+
}
|
32
|
+
|
33
|
+
class ColorConverters {
|
34
|
+
static CMYK_G([c, y, m, k]) {
|
35
|
+
return ["G", 1 - Math.min(1, 0.3 * c + 0.59 * m + 0.11 * y + k)];
|
36
|
+
}
|
37
|
+
|
38
|
+
static G_CMYK([g]) {
|
39
|
+
return ["CMYK", 0, 0, 0, 1 - g];
|
40
|
+
}
|
41
|
+
|
42
|
+
static G_RGB([g]) {
|
43
|
+
return ["RGB", g, g, g];
|
44
|
+
}
|
45
|
+
|
46
|
+
static G_HTML([g]) {
|
47
|
+
const G = makeColorComp(g);
|
48
|
+
return `#${G}${G}${G}`;
|
49
|
+
}
|
50
|
+
|
51
|
+
static RGB_G([r, g, b]) {
|
52
|
+
return ["G", 0.3 * r + 0.59 * g + 0.11 * b];
|
53
|
+
}
|
54
|
+
|
55
|
+
static RGB_HTML([r, g, b]) {
|
56
|
+
const R = makeColorComp(r);
|
57
|
+
const G = makeColorComp(g);
|
58
|
+
const B = makeColorComp(b);
|
59
|
+
return `#${R}${G}${B}`;
|
60
|
+
}
|
61
|
+
|
62
|
+
static T_HTML() {
|
63
|
+
return "#00000000";
|
64
|
+
}
|
65
|
+
|
66
|
+
static CMYK_RGB([c, y, m, k]) {
|
67
|
+
return ["RGB", 1 - Math.min(1, c + k), 1 - Math.min(1, m + k), 1 - Math.min(1, y + k)];
|
68
|
+
}
|
69
|
+
|
70
|
+
static CMYK_HTML(components) {
|
71
|
+
return this.RGB_HTML(this.CMYK_RGB(components));
|
72
|
+
}
|
73
|
+
|
74
|
+
static RGB_CMYK([r, g, b]) {
|
75
|
+
const c = 1 - r;
|
76
|
+
const m = 1 - g;
|
77
|
+
const y = 1 - b;
|
78
|
+
const k = Math.min(c, m, y);
|
79
|
+
return ["CMYK", c, m, y, k];
|
80
|
+
}
|
81
|
+
|
82
|
+
}
|
83
|
+
|
84
|
+
exports.ColorConverters = ColorConverters;
|
package/lib/shared/util.js
CHANGED
@@ -29,28 +29,33 @@ exports.arraysToBytes = arraysToBytes;
|
|
29
29
|
exports.assert = assert;
|
30
30
|
exports.bytesToString = bytesToString;
|
31
31
|
exports.createPromiseCapability = createPromiseCapability;
|
32
|
+
exports.createValidAbsoluteUrl = createValidAbsoluteUrl;
|
33
|
+
exports.encodeToXmlString = encodeToXmlString;
|
32
34
|
exports.escapeString = escapeString;
|
33
35
|
exports.getModificationDate = getModificationDate;
|
34
36
|
exports.getVerbosityLevel = getVerbosityLevel;
|
35
37
|
exports.info = info;
|
36
38
|
exports.isArrayBuffer = isArrayBuffer;
|
37
39
|
exports.isArrayEqual = isArrayEqual;
|
40
|
+
exports.isAscii = isAscii;
|
38
41
|
exports.isBool = isBool;
|
39
42
|
exports.isNum = isNum;
|
40
|
-
exports.isString = isString;
|
41
43
|
exports.isSameOrigin = isSameOrigin;
|
42
|
-
exports.
|
44
|
+
exports.isString = isString;
|
45
|
+
exports.objectFromEntries = objectFromEntries;
|
46
|
+
exports.objectSize = objectSize;
|
43
47
|
exports.removeNullCharacters = removeNullCharacters;
|
44
48
|
exports.setVerbosityLevel = setVerbosityLevel;
|
45
49
|
exports.shadow = shadow;
|
46
50
|
exports.string32 = string32;
|
47
51
|
exports.stringToBytes = stringToBytes;
|
48
52
|
exports.stringToPDFString = stringToPDFString;
|
53
|
+
exports.stringToUTF16BEString = stringToUTF16BEString;
|
49
54
|
exports.stringToUTF8String = stringToUTF8String;
|
55
|
+
exports.unreachable = unreachable;
|
50
56
|
exports.utf8StringToString = utf8StringToString;
|
51
57
|
exports.warn = warn;
|
52
|
-
exports.
|
53
|
-
exports.IsEvalSupportedCached = exports.IsLittleEndianCached = exports.createObjectURL = exports.FormatError = exports.Util = exports.UnknownErrorException = exports.UnexpectedResponseException = exports.TextRenderingMode = exports.StreamType = exports.PermissionFlag = exports.PasswordResponses = exports.PasswordException = exports.MissingPDFException = exports.InvalidPDFException = exports.AbortException = exports.CMapCompressionType = exports.ImageKind = exports.FontType = exports.AnnotationType = exports.AnnotationStateModelType = exports.AnnotationReviewState = exports.AnnotationReplyType = exports.AnnotationMarkedState = exports.AnnotationFlag = exports.AnnotationFieldFlag = exports.AnnotationBorderStyleType = exports.UNSUPPORTED_FEATURES = exports.VerbosityLevel = exports.OPS = exports.IDENTITY_MATRIX = exports.FONT_IDENTITY_MATRIX = exports.BaseException = void 0;
|
58
|
+
exports.VerbosityLevel = exports.Util = exports.UNSUPPORTED_FEATURES = exports.UnknownErrorException = exports.UnexpectedResponseException = exports.TextRenderingMode = exports.StreamType = exports.PermissionFlag = exports.PasswordResponses = exports.PasswordException = exports.PageActionEventType = exports.OPS = exports.MissingPDFException = exports.IsLittleEndianCached = exports.IsEvalSupportedCached = exports.InvalidPDFException = exports.ImageKind = exports.IDENTITY_MATRIX = exports.FormatError = exports.FontType = exports.FONT_IDENTITY_MATRIX = exports.DocumentActionEventType = exports.createObjectURL = exports.CMapCompressionType = exports.BaseException = exports.AnnotationType = exports.AnnotationStateModelType = exports.AnnotationReviewState = exports.AnnotationReplyType = exports.AnnotationMarkedState = exports.AnnotationFlag = exports.AnnotationFieldFlag = exports.AnnotationBorderStyleType = exports.AnnotationActionEventType = exports.AbortException = void 0;
|
54
59
|
|
55
60
|
require("./compatibility.js");
|
56
61
|
|
@@ -183,6 +188,36 @@ const AnnotationBorderStyleType = {
|
|
183
188
|
UNDERLINE: 5
|
184
189
|
};
|
185
190
|
exports.AnnotationBorderStyleType = AnnotationBorderStyleType;
|
191
|
+
const AnnotationActionEventType = {
|
192
|
+
E: "Mouse Enter",
|
193
|
+
X: "Mouse Exit",
|
194
|
+
D: "Mouse Down",
|
195
|
+
U: "Mouse Up",
|
196
|
+
Fo: "Focus",
|
197
|
+
Bl: "Blur",
|
198
|
+
PO: "PageOpen",
|
199
|
+
PC: "PageClose",
|
200
|
+
PV: "PageVisible",
|
201
|
+
PI: "PageInvisible",
|
202
|
+
K: "Keystroke",
|
203
|
+
F: "Format",
|
204
|
+
V: "Validate",
|
205
|
+
C: "Calculate"
|
206
|
+
};
|
207
|
+
exports.AnnotationActionEventType = AnnotationActionEventType;
|
208
|
+
const DocumentActionEventType = {
|
209
|
+
WC: "WillClose",
|
210
|
+
WS: "WillSave",
|
211
|
+
DS: "DidSave",
|
212
|
+
WP: "WillPrint",
|
213
|
+
DP: "DidPrint"
|
214
|
+
};
|
215
|
+
exports.DocumentActionEventType = DocumentActionEventType;
|
216
|
+
const PageActionEventType = {
|
217
|
+
O: "PageOpen",
|
218
|
+
C: "PageClose"
|
219
|
+
};
|
220
|
+
exports.PageActionEventType = PageActionEventType;
|
186
221
|
const StreamType = {
|
187
222
|
UNKNOWN: "UNKNOWN",
|
188
223
|
FLATE: "FLATE",
|
@@ -591,6 +626,14 @@ function string32(value) {
|
|
591
626
|
return String.fromCharCode(value >> 24 & 0xff, value >> 16 & 0xff, value >> 8 & 0xff, value & 0xff);
|
592
627
|
}
|
593
628
|
|
629
|
+
function objectSize(obj) {
|
630
|
+
return Object.keys(obj).length;
|
631
|
+
}
|
632
|
+
|
633
|
+
function objectFromEntries(iterable) {
|
634
|
+
return Object.assign(Object.create(null), Object.fromEntries(iterable));
|
635
|
+
}
|
636
|
+
|
594
637
|
function isLittleEndian() {
|
595
638
|
const buffer8 = new Uint8Array(4);
|
596
639
|
buffer8[0] = 1;
|
@@ -622,14 +665,11 @@ const IsEvalSupportedCached = {
|
|
622
665
|
|
623
666
|
};
|
624
667
|
exports.IsEvalSupportedCached = IsEvalSupportedCached;
|
625
|
-
const
|
668
|
+
const hexNumbers = [...Array(256).keys()].map(n => n.toString(16).padStart(2, "0"));
|
626
669
|
|
627
670
|
class Util {
|
628
|
-
static
|
629
|
-
|
630
|
-
rgbBuf[3] = g;
|
631
|
-
rgbBuf[5] = b;
|
632
|
-
return rgbBuf.join("");
|
671
|
+
static makeHexColor(r, g, b) {
|
672
|
+
return `#${hexNumbers[r]}${hexNumbers[g]}${hexNumbers[b]}`;
|
633
673
|
}
|
634
674
|
|
635
675
|
static transform(m1, m2) {
|
@@ -751,7 +791,31 @@ function stringToPDFString(str) {
|
|
751
791
|
}
|
752
792
|
|
753
793
|
function escapeString(str) {
|
754
|
-
return str.replace(/([
|
794
|
+
return str.replace(/([()\\\n\r])/g, match => {
|
795
|
+
if (match === "\n") {
|
796
|
+
return "\\n";
|
797
|
+
} else if (match === "\r") {
|
798
|
+
return "\\r";
|
799
|
+
}
|
800
|
+
|
801
|
+
return `\\${match}`;
|
802
|
+
});
|
803
|
+
}
|
804
|
+
|
805
|
+
function isAscii(str) {
|
806
|
+
return /^[\x00-\x7F]*$/.test(str);
|
807
|
+
}
|
808
|
+
|
809
|
+
function stringToUTF16BEString(str) {
|
810
|
+
const buf = ["\xFE\xFF"];
|
811
|
+
|
812
|
+
for (let i = 0, ii = str.length; i < ii; i++) {
|
813
|
+
const char = str.charCodeAt(i);
|
814
|
+
buf.push(String.fromCharCode(char >> 8 & 0xff));
|
815
|
+
buf.push(String.fromCharCode(char & 0xff));
|
816
|
+
}
|
817
|
+
|
818
|
+
return buf.join("");
|
755
819
|
}
|
756
820
|
|
757
821
|
function stringToUTF8String(str) {
|
@@ -788,8 +852,8 @@ function isArrayEqual(arr1, arr2) {
|
|
788
852
|
});
|
789
853
|
}
|
790
854
|
|
791
|
-
function getModificationDate(date = new Date(
|
792
|
-
const buffer = [date.getUTCFullYear().toString(), (date.getUTCMonth() + 1).toString().padStart(2, "0"),
|
855
|
+
function getModificationDate(date = new Date()) {
|
856
|
+
const buffer = [date.getUTCFullYear().toString(), (date.getUTCMonth() + 1).toString().padStart(2, "0"), date.getUTCDate().toString().padStart(2, "0"), date.getUTCHours().toString().padStart(2, "0"), date.getUTCMinutes().toString().padStart(2, "0"), date.getUTCSeconds().toString().padStart(2, "0")];
|
793
857
|
return buffer.join("");
|
794
858
|
}
|
795
859
|
|
@@ -843,4 +907,55 @@ const createObjectURL = function createObjectURLClosure() {
|
|
843
907
|
};
|
844
908
|
}();
|
845
909
|
|
846
|
-
exports.createObjectURL = createObjectURL;
|
910
|
+
exports.createObjectURL = createObjectURL;
|
911
|
+
const XMLEntities = {
|
912
|
+
0x3c: "<",
|
913
|
+
0x3e: ">",
|
914
|
+
0x26: "&",
|
915
|
+
0x22: """,
|
916
|
+
0x27: "'"
|
917
|
+
};
|
918
|
+
|
919
|
+
function encodeToXmlString(str) {
|
920
|
+
const buffer = [];
|
921
|
+
let start = 0;
|
922
|
+
|
923
|
+
for (let i = 0, ii = str.length; i < ii; i++) {
|
924
|
+
const char = str.codePointAt(i);
|
925
|
+
|
926
|
+
if (0x20 <= char && char <= 0x7e) {
|
927
|
+
const entity = XMLEntities[char];
|
928
|
+
|
929
|
+
if (entity) {
|
930
|
+
if (start < i) {
|
931
|
+
buffer.push(str.substring(start, i));
|
932
|
+
}
|
933
|
+
|
934
|
+
buffer.push(entity);
|
935
|
+
start = i + 1;
|
936
|
+
}
|
937
|
+
} else {
|
938
|
+
if (start < i) {
|
939
|
+
buffer.push(str.substring(start, i));
|
940
|
+
}
|
941
|
+
|
942
|
+
buffer.push(`&#x${char.toString(16).toUpperCase()};`);
|
943
|
+
|
944
|
+
if (char > 0xd7ff && (char < 0xe000 || char > 0xfffd)) {
|
945
|
+
i++;
|
946
|
+
}
|
947
|
+
|
948
|
+
start = i + 1;
|
949
|
+
}
|
950
|
+
}
|
951
|
+
|
952
|
+
if (buffer.length === 0) {
|
953
|
+
return str;
|
954
|
+
}
|
955
|
+
|
956
|
+
if (start < str.length) {
|
957
|
+
buffer.push(str.substring(start, str.length));
|
958
|
+
}
|
959
|
+
|
960
|
+
return buffer.join("");
|
961
|
+
}
|
@@ -24,7 +24,10 @@
|
|
24
24
|
Object.defineProperty(exports, "__esModule", {
|
25
25
|
value: true
|
26
26
|
});
|
27
|
-
exports.SimpleXMLParser = void 0;
|
27
|
+
exports.SimpleXMLParser = exports.SimpleDOMNode = void 0;
|
28
|
+
|
29
|
+
var _util = require("./util.js");
|
30
|
+
|
28
31
|
const XMLParserErrorCode = {
|
29
32
|
NoError: 0,
|
30
33
|
EndOfDocument: -1,
|
@@ -58,9 +61,9 @@ class XMLParserBase {
|
|
58
61
|
_resolveEntities(s) {
|
59
62
|
return s.replace(/&([^;]+);/g, (all, entity) => {
|
60
63
|
if (entity.substring(0, 2) === "#x") {
|
61
|
-
return String.
|
64
|
+
return String.fromCodePoint(parseInt(entity.substring(2), 16));
|
62
65
|
} else if (entity.substring(0, 1) === "#") {
|
63
|
-
return String.
|
66
|
+
return String.fromCodePoint(parseInt(entity.substring(1), 10));
|
64
67
|
}
|
65
68
|
|
66
69
|
switch (entity) {
|
@@ -365,14 +368,111 @@ class SimpleDOMNode {
|
|
365
368
|
return this.childNodes && this.childNodes.length > 0;
|
366
369
|
}
|
367
370
|
|
371
|
+
searchNode(paths, pos) {
|
372
|
+
if (pos >= paths.length) {
|
373
|
+
return this;
|
374
|
+
}
|
375
|
+
|
376
|
+
const component = paths[pos];
|
377
|
+
const stack = [];
|
378
|
+
let node = this;
|
379
|
+
|
380
|
+
while (true) {
|
381
|
+
if (component.name === node.nodeName) {
|
382
|
+
if (component.pos === 0) {
|
383
|
+
const res = node.searchNode(paths, pos + 1);
|
384
|
+
|
385
|
+
if (res !== null) {
|
386
|
+
return res;
|
387
|
+
}
|
388
|
+
} else if (stack.length === 0) {
|
389
|
+
return null;
|
390
|
+
} else {
|
391
|
+
const [parent] = stack.pop();
|
392
|
+
let siblingPos = 0;
|
393
|
+
|
394
|
+
for (const child of parent.childNodes) {
|
395
|
+
if (component.name === child.nodeName) {
|
396
|
+
if (siblingPos === component.pos) {
|
397
|
+
return child.searchNode(paths, pos + 1);
|
398
|
+
}
|
399
|
+
|
400
|
+
siblingPos++;
|
401
|
+
}
|
402
|
+
}
|
403
|
+
|
404
|
+
return node.searchNode(paths, pos + 1);
|
405
|
+
}
|
406
|
+
}
|
407
|
+
|
408
|
+
if (node.childNodes && node.childNodes.length !== 0) {
|
409
|
+
stack.push([node, 0]);
|
410
|
+
node = node.childNodes[0];
|
411
|
+
} else if (stack.length === 0) {
|
412
|
+
return null;
|
413
|
+
} else {
|
414
|
+
while (stack.length !== 0) {
|
415
|
+
const [parent, currentPos] = stack.pop();
|
416
|
+
const newPos = currentPos + 1;
|
417
|
+
|
418
|
+
if (newPos < parent.childNodes.length) {
|
419
|
+
stack.push([parent, newPos]);
|
420
|
+
node = parent.childNodes[newPos];
|
421
|
+
break;
|
422
|
+
}
|
423
|
+
}
|
424
|
+
|
425
|
+
if (stack.length === 0) {
|
426
|
+
return null;
|
427
|
+
}
|
428
|
+
}
|
429
|
+
}
|
430
|
+
}
|
431
|
+
|
432
|
+
dump(buffer) {
|
433
|
+
if (this.nodeName === "#text") {
|
434
|
+
buffer.push((0, _util.encodeToXmlString)(this.nodeValue));
|
435
|
+
return;
|
436
|
+
}
|
437
|
+
|
438
|
+
buffer.push(`<${this.nodeName}`);
|
439
|
+
|
440
|
+
if (this.attributes) {
|
441
|
+
for (const attribute of this.attributes) {
|
442
|
+
buffer.push(` ${attribute.name}="${(0, _util.encodeToXmlString)(attribute.value)}"`);
|
443
|
+
}
|
444
|
+
}
|
445
|
+
|
446
|
+
if (this.hasChildNodes()) {
|
447
|
+
buffer.push(">");
|
448
|
+
|
449
|
+
for (const child of this.childNodes) {
|
450
|
+
child.dump(buffer);
|
451
|
+
}
|
452
|
+
|
453
|
+
buffer.push(`</${this.nodeName}>`);
|
454
|
+
} else if (this.nodeValue) {
|
455
|
+
buffer.push(`>${(0, _util.encodeToXmlString)(this.nodeValue)}</${this.nodeName}>`);
|
456
|
+
} else {
|
457
|
+
buffer.push("/>");
|
458
|
+
}
|
459
|
+
}
|
460
|
+
|
368
461
|
}
|
369
462
|
|
463
|
+
exports.SimpleDOMNode = SimpleDOMNode;
|
464
|
+
|
370
465
|
class SimpleXMLParser extends XMLParserBase {
|
371
|
-
constructor(
|
466
|
+
constructor({
|
467
|
+
hasAttributes = false,
|
468
|
+
lowerCaseName = false
|
469
|
+
}) {
|
372
470
|
super();
|
373
471
|
this._currentFragment = null;
|
374
472
|
this._stack = null;
|
375
473
|
this._errorCode = XMLParserErrorCode.NoError;
|
474
|
+
this._hasAttributes = hasAttributes;
|
475
|
+
this._lowerCaseName = lowerCaseName;
|
376
476
|
}
|
377
477
|
|
378
478
|
parseFromString(data) {
|
@@ -422,9 +522,17 @@ class SimpleXMLParser extends XMLParserBase {
|
|
422
522
|
}
|
423
523
|
|
424
524
|
onBeginElement(name, attributes, isEmpty) {
|
525
|
+
if (this._lowerCaseName) {
|
526
|
+
name = name.toLowerCase();
|
527
|
+
}
|
528
|
+
|
425
529
|
const node = new SimpleDOMNode(name);
|
426
530
|
node.childNodes = [];
|
427
531
|
|
532
|
+
if (this._hasAttributes) {
|
533
|
+
node.attributes = attributes;
|
534
|
+
}
|
535
|
+
|
428
536
|
this._currentFragment.push(node);
|
429
537
|
|
430
538
|
if (isEmpty) {
|