@@ -6,16 +6,23 @@ import { unitConvert } from '../utils.js';
66// Main link array, and refs to original functions.
77var linkInfo = [ ] ;
88var orig = {
9- toContainer : Worker . prototype . toContainer ,
9+ toCanvas : Worker . prototype . toCanvas ,
1010 toPdf : Worker . prototype . toPdf ,
1111} ;
1212
13- Worker . prototype . toContainer = function toContainer ( ) {
14- return orig . toContainer . call ( this ) . then ( function toContainer_hyperlink ( ) {
13+ Worker . prototype . toCanvas = function toCanvas ( ) {
14+ return this . then ( function toCanvas_hyperlink ( ) {
15+ // Attach extra behaviour to the html2canvas onclone property.
16+ var oncloneOrig = this . opt . html2canvas . onclone || function ( ) { } ;
17+ this . opt . html2canvas . onclone = onclone_hyperlink . bind ( this , oncloneOrig ) ;
18+ } ) . then ( orig . toCanvas . bind ( this ) ) ;
19+ } ;
20+
21+ function onclone_hyperlink ( oncloneOrig , doc ) {
1522 // Retrieve hyperlink info if the option is enabled.
1623 if ( this . opt . enableLinks ) {
1724 // Find all anchor tags and get the container's bounds for reference.
18- var container = this . prop . container ;
25+ var container = doc . body ;
1926 var links = container . querySelectorAll ( 'a' ) ;
2027 var containerRect = unitConvert ( container . getBoundingClientRect ( ) , this . prop . pageSize . k ) ;
2128 linkInfo = [ ] ;
@@ -37,14 +44,16 @@ Worker.prototype.toContainer = function toContainer() {
3744 }
3845 } , this ) ;
3946 }
40- } ) ;
41- } ;
47+
48+ // Call the original onclone callback.
49+ oncloneOrig ( doc ) ;
50+ }
4251
4352Worker . prototype . toPdf = function toPdf ( ) {
4453 return orig . toPdf . call ( this ) . then ( function toPdf_hyperlink ( ) {
4554 // Add hyperlinks if the option is enabled.
4655 if ( this . opt . enableLinks ) {
47- // Attach each anchor tag based on info from toContainer() .
56+ // Attach each anchor tag based on info from the cloned document .
4857 linkInfo . forEach ( function ( l ) {
4958 this . prop . pdf . setPage ( l . page ) ;
5059 this . prop . pdf . link ( l . left , l . top , l . clientRect . width , l . clientRect . height ,
0 commit comments