From 2b0db917e3238baff90210550875b2b44f48e6db Mon Sep 17 00:00:00 2001 From: Andy Edinborough Date: Fri, 11 Jan 2013 10:46:53 -0600 Subject: [PATCH] don't process pseudo elements for hidden elements; cleanup pseudo elements after render --- src/Core.js | 8 ++++++++ src/Parse.js | 2 +- src/Preload.js | 26 +++++++++++++++++--------- 3 files changed, 26 insertions(+), 10 deletions(-) diff --git a/src/Core.js b/src/Core.js index dc2c5d6..01a6c76 100644 --- a/src/Core.js +++ b/src/Core.js @@ -14,6 +14,14 @@ function h2clog(a) { _html2canvas.Util = {}; +_html2canvas.Util.isElementVisible = function (element) { + return ( + _html2canvas.Util.getCSS( element, 'display' ) !== "none" && + _html2canvas.Util.getCSS( element, 'visibility' ) !== "hidden" && + !element.hasAttribute( "data-html2canvas-ignore" ) + ); +}; + _html2canvas.Util.trimText = (function(native){ return function(input){ if(native) { return native.apply( input ); } diff --git a/src/Parse.js b/src/Parse.js index 956abc2..ee3c60b 100644 --- a/src/Parse.js +++ b/src/Parse.js @@ -170,7 +170,7 @@ _html2canvas.Parse = function (images, options) { textList.forEach(function(word, index) { if (/.*[\u4E00-\u9FA5].*$/.test(word)) { word = word.split(""); - word.unshift(index, 1) + word.unshift(index, 1); textList.splice.apply(textList, word); } }); diff --git a/src/Preload.js b/src/Preload.js index 90d4074..a4ebfbf 100644 --- a/src/Preload.js +++ b/src/Preload.js @@ -212,10 +212,12 @@ _html2canvas.Preload = function( options ) { */ } - - var uid = 0, injectStyle; function injectPseudoElements(el) { + if(!_html2canvas.Util.isElementVisible(el)) { + return; + } + var before = getPseudoElement(el, ':before'), after = getPseudoElement(el, ':after'); if(!before && !after) { @@ -258,6 +260,17 @@ _html2canvas.Preload = function( options ) { } } + function cleanupPseudoElements(){ + if(!injectStyle) { + return; + } + injectStyle.parentNode.removeChild(injectStyle); + injectStyle = undefined; + + [].slice.apply(element.all || element.getElementsByTagName('*')) + .forEach(removePseudoElements); + } + function getPseudoElement(el, which) { var elStyle = window.getComputedStyle(el, which); if(!elStyle || !elStyle.content) { return; } @@ -380,19 +393,14 @@ _html2canvas.Preload = function( options ) { } } - if(injectStyle) { - injectStyle.parentNode.removeChild(injectStyle); - injectStyle = undefined; - - [].slice.apply(element.all || element.getElementsByTagName('*')) - .forEach(removePseudoElements); - } + cleanupPseudoElements(); }, renderingDone: function() { if (timeoutTimer) { window.clearTimeout(timeoutTimer); } + cleanupPseudoElements(); } };