@@ -45,17 +45,31 @@ describe("document", function () {
45
45
} ) ;
46
46
47
47
describe ( "PDFDocument" , function ( ) {
48
- const pdfManager = {
49
- get docId ( ) {
50
- return "d0" ;
51
- } ,
52
- } ;
53
48
const stream = new StringStream ( "Dummy_PDF_data" ) ;
54
49
55
50
function getDocument ( acroForm , xref = new XRefMock ( ) ) {
51
+ const catalog = { acroForm } ;
52
+ const pdfManager = {
53
+ get docId ( ) {
54
+ return "d0" ;
55
+ } ,
56
+ ensureCatalog ( prop , args ) {
57
+ return pdfManager . ensure ( catalog , prop , args ) ;
58
+ } ,
59
+ ensure ( obj , prop , args ) {
60
+ return new Promise ( function ( resolve ) {
61
+ const value = obj [ prop ] ;
62
+ if ( typeof value === "function" ) {
63
+ resolve ( value . apply ( obj , args ) ) ;
64
+ } else {
65
+ resolve ( value ) ;
66
+ }
67
+ } ) ;
68
+ } ,
69
+ } ;
56
70
const pdfDocument = new PDFDocument ( pdfManager , stream ) ;
57
71
pdfDocument . xref = xref ;
58
- pdfDocument . catalog = { acroForm } ;
72
+ pdfDocument . catalog = catalog ;
59
73
return pdfDocument ;
60
74
}
61
75
@@ -174,5 +188,56 @@ describe("document", function () {
174
188
pdfDocument = getDocument ( acroForm ) ;
175
189
expect ( pdfDocument . calculationOrderIds ) . toEqual ( null ) ;
176
190
} ) ;
191
+
192
+ it ( "should get field objects array or null" , async function ( ) {
193
+ const acroForm = new Dict ( ) ;
194
+
195
+ let pdfDocument = getDocument ( acroForm ) ;
196
+ let fields = await pdfDocument . fieldObjects ;
197
+ expect ( fields ) . toEqual ( null ) ;
198
+
199
+ acroForm . set ( "Fields" , [ ] ) ;
200
+ pdfDocument = getDocument ( acroForm ) ;
201
+ fields = await pdfDocument . fieldObjects ;
202
+ expect ( fields ) . toEqual ( null ) ;
203
+
204
+ const kid1Ref = Ref . get ( 314 , 0 ) ;
205
+ const kid11Ref = Ref . get ( 159 , 0 ) ;
206
+ const kid2Ref = Ref . get ( 265 , 0 ) ;
207
+ const parentRef = Ref . get ( 358 , 0 ) ;
208
+
209
+ const allFields = Object . create ( null ) ;
210
+ for ( const name of [ "parent" , "kid1" , "kid2" , "kid11" ] ) {
211
+ const buttonWidgetDict = new Dict ( ) ;
212
+ buttonWidgetDict . set ( "Type" , Name . get ( "Annot" ) ) ;
213
+ buttonWidgetDict . set ( "Subtype" , Name . get ( "Widget" ) ) ;
214
+ buttonWidgetDict . set ( "FT" , Name . get ( "Btn" ) ) ;
215
+ buttonWidgetDict . set ( "T" , name ) ;
216
+ allFields [ name ] = buttonWidgetDict ;
217
+ }
218
+
219
+ allFields . kid1 . set ( "Kids" , [ kid11Ref ] ) ;
220
+ allFields . parent . set ( "Kids" , [ kid1Ref , kid2Ref ] ) ;
221
+
222
+ const xref = new XRefMock ( [
223
+ { ref : parentRef , data : allFields . parent } ,
224
+ { ref : kid1Ref , data : allFields . kid1 } ,
225
+ { ref : kid11Ref , data : allFields . kid11 } ,
226
+ { ref : kid2Ref , data : allFields . kid2 } ,
227
+ ] ) ;
228
+
229
+ acroForm . set ( "Fields" , [ parentRef ] ) ;
230
+ pdfDocument = getDocument ( acroForm , xref ) ;
231
+ fields = await pdfDocument . fieldObjects ;
232
+
233
+ for ( const [ name , objs ] of Object . entries ( fields ) ) {
234
+ fields [ name ] = objs . map ( obj => obj . id ) ;
235
+ }
236
+
237
+ expect ( fields [ "parent.kid1" ] ) . toEqual ( [ "314R" ] ) ;
238
+ expect ( fields [ "parent.kid1.kid11" ] ) . toEqual ( [ "159R" ] ) ;
239
+ expect ( fields [ "parent.kid2" ] ) . toEqual ( [ "265R" ] ) ;
240
+ expect ( fields . parent ) . toEqual ( [ "358R" ] ) ;
241
+ } ) ;
177
242
} ) ;
178
243
} ) ;
0 commit comments