@@ -347,7 +347,6 @@ const inject = {
347
347
return false ;
348
348
}
349
349
cfg . $target = this . createTarget ( cfg . target ) ;
350
- cfg . $injected = cfg . $target ;
351
350
}
352
351
return true ;
353
352
} ,
@@ -455,23 +454,36 @@ const inject = {
455
454
/* Called after the XHR has succeeded and we have a new $source
456
455
* element to inject.
457
456
*/
458
- if ( cfg . sourceMod === "content" ) {
459
- $source = $source . contents ( ) ;
460
- }
461
- const $src = $source . safeClone ( ) ;
462
- for ( const img of dom . querySelectorAllAndMe ( $src [ 0 ] , "img" ) ) {
463
- $ ( img ) . on ( "load" , ( e ) => {
464
- $ ( e . currentTarget ) . trigger ( "pat-inject-content-loaded" ) ;
465
- } ) ;
457
+ const wrapper = document . createElement ( "div" ) ;
458
+ if ( $source . length > 0 ) {
459
+ if ( cfg . sourceMod === "content" ) {
460
+ wrapper . innerHTML = $source [ 0 ] . innerHTML ;
461
+ } else {
462
+ wrapper . innerHTML = $source [ 0 ] . outerHTML ;
463
+ }
464
+
465
+ for ( const img of wrapper . querySelectorAll ( "img" ) ) {
466
+ events . add_event_listener (
467
+ img ,
468
+ "load" ,
469
+ "inject_img_load" ,
470
+ ( e ) => {
471
+ $ ( e . currentTarget ) . trigger ( "pat-inject-content-loaded" ) ;
472
+ } ,
473
+ { once : true }
474
+ ) ;
475
+ }
466
476
}
467
477
468
- const $injected = cfg . $injected || $src ;
478
+ // Copy, because after insertion wrapper.children is empty.
479
+ const source_nodes = [ ...wrapper . childNodes ] ;
480
+
469
481
// Now the injection actually happens.
470
- if ( this . _inject ( trigger , $src , $ ( target ) , cfg ) ) {
482
+ if ( this . _inject ( trigger , source_nodes , target , cfg ) ) {
471
483
// Update history
472
484
this . _update_history ( cfg , trigger , title ) ;
473
485
// Post-injection
474
- this . _afterInjection ( $el , $injected , cfg ) ;
486
+ this . _afterInjection ( $el , $ ( source_nodes ) , cfg ) ;
475
487
}
476
488
} ,
477
489
@@ -488,9 +500,12 @@ const inject = {
488
500
history . pushState ( { url : url } , "" , url ) ;
489
501
// Also inject title element if we have one
490
502
if ( title ) {
491
- this . _inject ( trigger , title , $ ( "title" ) , {
492
- action : "element" ,
493
- } ) ;
503
+ const title_el = document . querySelector ( "title" ) ;
504
+ if ( title_el ) {
505
+ this . _inject ( trigger , title , title_el , {
506
+ action : "element" ,
507
+ } ) ;
508
+ }
494
509
}
495
510
} ,
496
511
@@ -761,45 +776,45 @@ const inject = {
761
776
}
762
777
} ,
763
778
764
- _inject ( trigger , $source , $target , cfg ) {
765
- // action to jquery method mapping, except for "content"
766
- // and "element"
767
- const method = {
768
- contentbefore : "prepend" ,
769
- contentafter : "append" ,
770
- elementbefore : "before" ,
771
- elementafter : "after" ,
772
- } [ cfg . action ] ;
773
-
779
+ _inject ( trigger , source , target , cfg ) {
774
780
if ( cfg . source === "none" ) {
775
- $target . replaceWith ( "" ) ;
781
+ // Special case. Clear the target after ajax call.
782
+ target . replaceWith ( "" ) ;
776
783
return true ;
777
784
}
778
- if ( $ source. length === 0 ) {
779
- log . warn ( "Aborting injection, source not found:" , $ source) ;
785
+ if ( source . length === 0 ) {
786
+ log . warn ( "Aborting injection, source not found:" , source ) ;
780
787
$ ( trigger ) . trigger ( "pat-inject-missingSource" , {
781
788
url : cfg . url ,
782
789
selector : cfg . source ,
783
790
} ) ;
784
791
return false ;
785
792
}
786
- if ( cfg . target === "none" )
793
+ if ( cfg . target === "none" ) {
787
794
// Special case. Don't do anything, we don't want any result
788
795
return true ;
789
- if ( $target . length === 0 ) {
790
- log . warn ( "Aborting injection, target not found:" , $target ) ;
796
+ }
797
+ if ( ! target ) {
798
+ log . warn ( "Aborting injection, target not found:" , target ) ;
791
799
$ ( trigger ) . trigger ( "pat-inject-missingTarget" , {
792
800
selector : cfg . target ,
793
801
} ) ;
794
802
return false ;
795
803
}
796
- if ( cfg . action === "content" ) {
797
- $target . empty ( ) . append ( $source ) ;
798
- } else if ( cfg . action === "element" ) {
799
- $target . replaceWith ( $source ) ;
800
- } else {
801
- $target [ method ] ( $source ) ;
802
- }
804
+
805
+ // cfg.action to DOM method mapping
806
+ const method = {
807
+ content : "replaceChildren" ,
808
+ contentafter : "append" ,
809
+ contentbefore : "prepend" ,
810
+ element : "replaceWith" ,
811
+ elementafter : "after" ,
812
+ elementbefore : "before" ,
813
+ } [ cfg . action ] ;
814
+
815
+ // Inject the content HERE!
816
+ target [ method ] ( ...source ) ;
817
+
803
818
return true ;
804
819
} ,
805
820
0 commit comments