@@ -53,7 +53,10 @@ import { MetadataParser } from "./metadata_parser.js";
53
53
import { StructTreeRoot } from "./struct_tree.js" ;
54
54
55
55
function fetchDestination ( dest ) {
56
- return isDict ( dest ) ? dest . get ( "D" ) : dest ;
56
+ if ( dest instanceof Dict ) {
57
+ dest = dest . get ( "D" ) ;
58
+ }
59
+ return Array . isArray ( dest ) ? dest : null ;
57
60
}
58
61
59
62
class Catalog {
@@ -515,22 +518,41 @@ class Catalog {
515
518
dests = Object . create ( null ) ;
516
519
if ( obj instanceof NameTree ) {
517
520
for ( const [ key , value ] of obj . getAll ( ) ) {
518
- dests [ key ] = fetchDestination ( value ) ;
521
+ const dest = fetchDestination ( value ) ;
522
+ if ( dest ) {
523
+ dests [ key ] = dest ;
524
+ }
519
525
}
520
526
} else if ( obj instanceof Dict ) {
521
527
obj . forEach ( function ( key , value ) {
522
- if ( value ) {
523
- dests [ key ] = fetchDestination ( value ) ;
528
+ const dest = fetchDestination ( value ) ;
529
+ if ( dest ) {
530
+ dests [ key ] = dest ;
524
531
}
525
532
} ) ;
526
533
}
527
534
return shadow ( this , "destinations" , dests ) ;
528
535
}
529
536
530
- getDestination ( destinationId ) {
537
+ getDestination ( id ) {
531
538
const obj = this . _readDests ( ) ;
532
- if ( obj instanceof NameTree || obj instanceof Dict ) {
533
- return fetchDestination ( obj . get ( destinationId ) || null ) ;
539
+ if ( obj instanceof NameTree ) {
540
+ const dest = fetchDestination ( obj . get ( id ) ) ;
541
+ if ( dest ) {
542
+ return dest ;
543
+ }
544
+ // Fallback to checking the *entire* NameTree, in an attempt to handle
545
+ // corrupt PDF documents with out-of-order NameTrees (fixes issue 10272).
546
+ const allDest = this . destinations [ id ] ;
547
+ if ( allDest ) {
548
+ warn ( `Found "${ id } " at an incorrect position in the NameTree.` ) ;
549
+ return allDest ;
550
+ }
551
+ } else if ( obj instanceof Dict ) {
552
+ const dest = fetchDestination ( obj . get ( id ) ) ;
553
+ if ( dest ) {
554
+ return dest ;
555
+ }
534
556
}
535
557
return null ;
536
558
}
0 commit comments