@@ -83,17 +83,6 @@ var html2pdf = (function(html2canvas, jsPDF) {
8383 } ;
8484
8585 html2pdf . parseInput = function ( source , opt ) {
86- // Parse the source element/string.
87- if ( ! source ) {
88- throw 'Missing source element or string.' ;
89- } else if ( objType ( source ) === 'string' ) {
90- source = createElement ( 'div' , { innerHTML : source } ) ;
91- } else if ( objType ( source ) === 'element' ) {
92- source = source . cloneNode ( true ) ;
93- } else {
94- throw 'Invalid source - please specify an HTML Element or string.' ;
95- }
96-
9786 // Parse the opt object.
9887 opt . jsPDF = opt . jsPDF || { } ;
9988 opt . html2canvas = opt . html2canvas || { } ;
@@ -121,6 +110,17 @@ var html2pdf = (function(html2canvas, jsPDF) {
121110 throw 'Invalid margin array.' ;
122111 }
123112
113+ // Parse the source element/string.
114+ if ( ! source ) {
115+ throw 'Missing source element or string.' ;
116+ } else if ( objType ( source ) === 'string' ) {
117+ source = createElement ( 'div' , { innerHTML : source } ) ;
118+ } else if ( objType ( source ) === 'element' ) {
119+ source = cloneNode ( source , opt . html2canvas . javascriptEnabled ) ;
120+ } else {
121+ throw 'Invalid source - please specify an HTML Element or string.' ;
122+ }
123+
124124 // Return the parsed input (opt is modified in-place, no need to return).
125125 return source ;
126126 } ;
@@ -246,6 +246,37 @@ var html2pdf = (function(html2canvas, jsPDF) {
246246 return el ;
247247 } ;
248248
249+ // Deep-clone a node and preserve contents/properties.
250+ var cloneNode = function ( node , javascriptEnabled ) {
251+ // Recursively clone the node.
252+ var clone = node . nodeType === 3 ? document . createTextNode ( node . nodeValue ) : node . cloneNode ( false ) ;
253+ for ( var child = node . firstChild ; child ; child = child . nextSibling ) {
254+ if ( javascriptEnabled === true || child . nodeType !== 1 || child . nodeName !== 'SCRIPT' ) {
255+ clone . appendChild ( cloneNode ( child , javascriptEnabled ) ) ;
256+ }
257+ }
258+
259+ if ( node . nodeType === 1 ) {
260+ // Preserve contents/properties of special nodes.
261+ if ( node . nodeName === 'CANVAS' ) {
262+ clone . width = node . width ;
263+ clone . height = node . height ;
264+ clone . getContext ( '2d' ) . drawImage ( node , 0 , 0 ) ;
265+ } else if ( node . nodeName === 'TEXTAREA' || node . nodeName === 'SELECT' ) {
266+ clone . value = node . value ;
267+ }
268+
269+ // Preserve the node's scroll position when it loads.
270+ clone . addEventListener ( 'load' , function ( ) {
271+ clone . scrollTop = node . scrollTop ;
272+ clone . scrollLeft = node . scrollLeft ;
273+ } , true ) ;
274+ }
275+
276+ // Return the cloned node.
277+ return clone ;
278+ }
279+
249280 // Convert units using the conversion value 'k' from jsPDF.
250281 var unitConvert = function ( obj , k ) {
251282 var newObj = { } ;
0 commit comments