@@ -1542,85 +1542,63 @@ process.umask = function() { return 0; };
15421542var log = require ( './log' ) ;
15431543var Promise = require ( './promise' ) ;
15441544
1545- var html2canvasCanvasCloneAttribute = "data-html2canvas-canvas-clone" ;
1546- var html2canvasCanvasCloneIndex = 0 ;
1547-
1548- function cloneNodeValues ( document , clone , nodeName ) {
1549- var originalNodes = document . getElementsByTagName ( nodeName ) ;
1550- var clonedNodes = clone . getElementsByTagName ( nodeName ) ;
1551- var count = originalNodes . length ;
1552- for ( var i = 0 ; i < count ; i ++ ) {
1553- clonedNodes [ i ] . value = originalNodes [ i ] . value ;
1554- }
1555- }
1556-
15571545function restoreOwnerScroll ( ownerDocument , x , y ) {
15581546 if ( ownerDocument . defaultView && ( x !== ownerDocument . defaultView . pageXOffset || y !== ownerDocument . defaultView . pageYOffset ) ) {
15591547 ownerDocument . defaultView . scrollTo ( x , y ) ;
15601548 }
15611549}
15621550
1563- function labelCanvasElements ( ownerDocument ) {
1564- [ ] . slice . call ( ownerDocument . querySelectorAll ( "canvas" ) , 0 ) . forEach ( function ( canvas ) {
1565- canvas . setAttribute ( html2canvasCanvasCloneAttribute , "canvas-" + html2canvasCanvasCloneIndex ++ ) ;
1566- } ) ;
1567- }
1568-
1569- function cloneCanvasContents ( ownerDocument , documentClone ) {
1570- [ ] . slice . call ( ownerDocument . querySelectorAll ( "[" + html2canvasCanvasCloneAttribute + "]" ) , 0 ) . forEach ( function ( canvas ) {
1571- try {
1572- var clonedCanvas = documentClone . querySelector ( '[' + html2canvasCanvasCloneAttribute + '="' + canvas . getAttribute ( html2canvasCanvasCloneAttribute ) + '"]' ) ;
1573- if ( clonedCanvas ) {
1574- clonedCanvas . width = canvas . width ;
1575- clonedCanvas . height = canvas . height ;
1576- clonedCanvas . getContext ( "2d" ) . putImageData ( canvas . getContext ( "2d" ) . getImageData ( 0 , 0 , canvas . width , canvas . height ) , 0 , 0 ) ;
1577- }
1578- } catch ( e ) {
1579- log ( "Unable to copy canvas content from" , canvas , e ) ;
1580- }
1581- canvas . removeAttribute ( html2canvasCanvasCloneAttribute ) ;
1582- } ) ;
1583- }
1584-
1585- function removeScriptNodes ( parent ) {
1586- [ ] . slice . call ( parent . childNodes , 0 ) . filter ( isElementNode ) . forEach ( function ( node ) {
1587- if ( node . tagName === "SCRIPT" ) {
1588- parent . removeChild ( node ) ;
1589- } else {
1590- removeScriptNodes ( node ) ;
1551+ function cloneCanvasContents ( canvas , clonedCanvas ) {
1552+ try {
1553+ if ( clonedCanvas ) {
1554+ clonedCanvas . width = canvas . width ;
1555+ clonedCanvas . height = canvas . height ;
1556+ clonedCanvas . getContext ( "2d" ) . putImageData ( canvas . getContext ( "2d" ) . getImageData ( 0 , 0 , canvas . width , canvas . height ) , 0 , 0 ) ;
15911557 }
1592- } ) ;
1593- return parent ;
1594- }
1595-
1596- function isIE9 ( ) {
1597- return document . documentMode && document . documentMode <= 9 ;
1558+ } catch ( e ) {
1559+ log ( "Unable to copy canvas content from" , canvas , e ) ;
1560+ }
15981561}
15991562
1600- // https://github.com/niklasvh/html2canvas/issues/503
1601- function cloneNodeIE9 ( node , javascriptEnabled ) {
1563+ function cloneNode ( node , javascriptEnabled ) {
16021564 var clone = node . nodeType === 3 ? document . createTextNode ( node . nodeValue ) : node . cloneNode ( false ) ;
16031565
16041566 var child = node . firstChild ;
16051567 while ( child ) {
16061568 if ( javascriptEnabled === true || child . nodeType !== 1 || child . nodeName !== 'SCRIPT' ) {
1607- clone . appendChild ( cloneNodeIE9 ( child , javascriptEnabled ) ) ;
1569+ clone . appendChild ( cloneNode ( child , javascriptEnabled ) ) ;
16081570 }
16091571 child = child . nextSibling ;
16101572 }
16111573
1574+ if ( node . nodeType === 1 ) {
1575+ clone . _scrollTop = node . scrollTop ;
1576+ clone . _scrollLeft = node . scrollLeft ;
1577+ if ( node . nodeName === "CANVAS" ) {
1578+ cloneCanvasContents ( node , clone ) ;
1579+ } else if ( node . nodeName === "TEXTAREA" || node . nodeName === "SELECT" ) {
1580+ clone . value = node . value ;
1581+ }
1582+ }
1583+
16121584 return clone ;
16131585}
16141586
1587+ function initNode ( node ) {
1588+ if ( node . nodeType === 1 ) {
1589+ node . scrollTop = node . _scrollTop ;
1590+ node . scrollLeft = node . _scrollLeft ;
16151591
1616-
1617- function isElementNode ( node ) {
1618- return node . nodeType === Node . ELEMENT_NODE ;
1592+ var child = node . firstChild ;
1593+ while ( child ) {
1594+ initNode ( child ) ;
1595+ child = child . nextSibling ;
1596+ }
1597+ }
16191598}
16201599
16211600module . exports = function ( ownerDocument , containerDocument , width , height , options , x , y ) {
1622- labelCanvasElements ( ownerDocument ) ;
1623- var documentElement = isIE9 ( ) ? cloneNodeIE9 ( ownerDocument . documentElement , options . javascriptEnabled ) : ownerDocument . documentElement . cloneNode ( true ) ;
1601+ var documentElement = cloneNode ( ownerDocument . documentElement , options . javascriptEnabled ) ;
16241602 var container = containerDocument . createElement ( "iframe" ) ;
16251603
16261604 container . className = "html2canvas-container" ;
@@ -1637,16 +1615,13 @@ module.exports = function(ownerDocument, containerDocument, width, height, optio
16371615 return new Promise ( function ( resolve ) {
16381616 var documentClone = container . contentWindow . document ;
16391617
1640- cloneNodeValues ( ownerDocument . documentElement , documentElement , "textarea" ) ;
1641- cloneNodeValues ( ownerDocument . documentElement , documentElement , "select" ) ;
1642-
16431618 /* Chrome doesn't detect relative background-images assigned in inline <style> sheets when fetched through getComputedStyle
16441619 if window url is about:blank, we can assign the url to current by writing onto the document
16451620 */
16461621 container . contentWindow . onload = container . onload = function ( ) {
16471622 var interval = setInterval ( function ( ) {
16481623 if ( documentClone . body . childNodes . length > 0 ) {
1649- cloneCanvasContents ( ownerDocument , documentClone ) ;
1624+ initNode ( documentClone . documentElement ) ;
16501625 clearInterval ( interval ) ;
16511626 if ( options . type === "view" ) {
16521627 container . contentWindow . scrollTo ( x , y ) ;
@@ -1660,7 +1635,7 @@ module.exports = function(ownerDocument, containerDocument, width, height, optio
16601635 documentClone . write ( "<!DOCTYPE html><html></html>" ) ;
16611636 // Chrome scrolls the parent document for some reason after the write to the cloned window???
16621637 restoreOwnerScroll ( ownerDocument , x , y ) ;
1663- documentClone . replaceChild ( options . javascriptEnabled === true ? documentClone . adoptNode ( documentElement ) : removeScriptNodes ( documentClone . adoptNode ( documentElement ) ) , documentClone . documentElement ) ;
1638+ documentClone . replaceChild ( documentClone . adoptNode ( documentElement ) , documentClone . documentElement ) ;
16641639 documentClone . close ( ) ;
16651640 } ) ;
16661641} ;
0 commit comments