Skip to content

Commit cb43e09

Browse files
committed
refactor parsing
1 parent bb1cd21 commit cb43e09

2 files changed

Lines changed: 54 additions & 69 deletions

File tree

src/Parse.js

Lines changed: 54 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -49,23 +49,6 @@ _html2canvas.Parse = function (images, options) {
4949
children,
5050
childrenLen;
5151

52-
53-
function documentWidth () {
54-
return Math.max(
55-
Math.max(doc.body.scrollWidth, doc.documentElement.scrollWidth),
56-
Math.max(doc.body.offsetWidth, doc.documentElement.offsetWidth),
57-
Math.max(doc.body.clientWidth, doc.documentElement.clientWidth)
58-
);
59-
}
60-
61-
function documentHeight () {
62-
return Math.max(
63-
Math.max(doc.body.scrollHeight, doc.documentElement.scrollHeight),
64-
Math.max(doc.body.offsetHeight, doc.documentElement.offsetHeight),
65-
Math.max(doc.body.clientHeight, doc.documentElement.clientHeight)
66-
);
67-
}
68-
6952
images = images || {};
7053

7154
// Test whether we can use ranges to measure bounding boxes
@@ -95,6 +78,22 @@ _html2canvas.Parse = function (images, options) {
9578

9679
var getCSS = _html2canvas.Util.getCSS;
9780

81+
function documentWidth () {
82+
return Math.max(
83+
Math.max(doc.body.scrollWidth, doc.documentElement.scrollWidth),
84+
Math.max(doc.body.offsetWidth, doc.documentElement.offsetWidth),
85+
Math.max(doc.body.clientWidth, doc.documentElement.clientWidth)
86+
);
87+
}
88+
89+
function documentHeight () {
90+
return Math.max(
91+
Math.max(doc.body.scrollHeight, doc.documentElement.scrollHeight),
92+
Math.max(doc.body.offsetHeight, doc.documentElement.offsetHeight),
93+
Math.max(doc.body.clientHeight, doc.documentElement.clientHeight)
94+
);
95+
}
96+
9897
function getCSSInt(element, attribute) {
9998
var val = parseInt(getCSS(element, attribute), 10);
10099
return (isNaN(val)) ? 0 : val; // borders in old IE are throwing 'medium' for demo.html
@@ -840,16 +839,31 @@ _html2canvas.Parse = function (images, options) {
840839
}
841840
}
842841

842+
function setOpacity(ctx, element, parentStack) {
843+
var opacity = getCSS(element, "opacity") * ((parentStack) ? parentStack.opacity : 1);
844+
ctx.setVariable("globalAlpha", opacity);
845+
return opacity;
846+
}
847+
843848
function createStack(element, parentStack, bounds) {
844849

845-
var stack = {
846-
ctx: h2cRenderContext((!parentStack) ? documentWidth() : bounds.width , (!parentStack) ? documentHeight() : bounds.height),
850+
var ctx = h2cRenderContext((!parentStack) ? documentWidth() : bounds.width , (!parentStack) ? documentHeight() : bounds.height),
851+
stack = {
852+
ctx: ctx,
847853
zIndex: setZ(getCSS(element, "zIndex"), (parentStack) ? parentStack.zIndex : null),
848-
opacity: getCSS(element, "opacity") * ((parentStack) ? parentStack.opacity : 1),
854+
opacity: setOpacity(ctx, element, parentStack),
849855
cssPosition: getCSS(element, "position"),
856+
borders: renderBorders(element, ctx, bounds, false),
850857
clip: (parentStack && parentStack.clip) ? _html2canvas.Util.Extend( {}, parentStack.clip ) : null
851858
};
852859

860+
// TODO correct overflow for absolute content residing under a static position
861+
if (options.useOverflow === true && /(hidden|scroll|auto)/.test(getCSS(element, "overflow")) === true && /(BODY)/i.test(element.nodeName) === false){
862+
stack.clip = (stack.clip) ? clipBounds(stack.clip, bounds) : bounds;
863+
}
864+
865+
stack.zIndex.children.push(stack);
866+
853867
return stack;
854868
}
855869

@@ -868,81 +882,54 @@ _html2canvas.Parse = function (images, options) {
868882
return backgroundBounds;
869883
}
870884

871-
function renderElement(el, parentStack){
872-
var bounds = _html2canvas.Util.Bounds(el),
885+
function renderElement(element, parentStack){
886+
var bounds = _html2canvas.Util.Bounds(element),
873887
image,
874-
bgcolor = getCSS(el, "backgroundColor"),
875-
zindex,
876-
stack,
877-
stackLength,
878-
borders,
879-
ctx,
880-
backgroundBounds;
881-
882-
stack = createStack(el, parentStack, bounds);
883-
zindex = stack.zIndex;
884-
885-
// TODO correct overflow for absolute content residing under a static position
886-
887-
if (options.useOverflow === true && /(hidden|scroll|auto)/.test(getCSS(el, "overflow")) === true && /(BODY)/i.test(el.nodeName) === false){
888-
stack.clip = (stack.clip) ? clipBounds(stack.clip, bounds) : bounds;
889-
}
890-
891-
stackLength = zindex.children.push(stack);
892-
893-
ctx = zindex.children[stackLength-1].ctx;
894-
895-
ctx.setVariable("globalAlpha", stack.opacity);
896-
897-
898-
borders = renderBorders(el, ctx, bounds, false);
899-
stack.borders = borders;
900-
901-
if (ignoreElementsRegExp.test(el.nodeName) && options.iframeDefault !== "transparent"){
902-
bgcolor = (options.iframeDefault === "default") ? "#efefef" : options.iframeDefault;
903-
}
904-
888+
bgcolor = (ignoreElementsRegExp.test(element.nodeName)) ? "#efefef" : getCSS(element, "backgroundColor"),
889+
stack = createStack(element, parentStack, bounds),
890+
borders = stack.borders,
891+
ctx = stack.ctx,
905892
backgroundBounds = getBackgroundBounds(borders, bounds, stack.clip);
906893

907894
if (backgroundBounds.height > 0 && backgroundBounds.width > 0){
908895
renderBackgroundColor(ctx, backgroundBounds, bgcolor);
909-
renderBackground(el, backgroundBounds, ctx);
896+
renderBackground(element, backgroundBounds, ctx);
910897
}
911898

912-
switch(el.nodeName){
899+
switch(element.nodeName){
913900
case "IMG":
914-
if ((image = loadImage(el.getAttribute('src')))) {
915-
renderImage(ctx, el, image, bounds, borders);
901+
if ((image = loadImage(element.getAttribute('src')))) {
902+
renderImage(ctx, element, image, bounds, borders);
916903
} else {
917-
h2clog("html2canvas: Error loading <img>:" + el.getAttribute('src'));
904+
h2clog("html2canvas: Error loading <img>:" + element.getAttribute('src'));
918905
}
919906
break;
920907
case "INPUT":
921908
// TODO add all relevant type's, i.e. HTML5 new stuff
922909
// todo add support for placeholder attribute for browsers which support it
923-
if (/^(text|url|email|submit|button|reset)$/.test(el.type) && el.value.length > 0){
924-
renderFormValue(el, bounds, stack);
910+
if (/^(text|url|email|submit|button|reset)$/.test(element.type) && element.value.length > 0){
911+
renderFormValue(element, bounds, stack);
925912
}
926913
break;
927914
case "TEXTAREA":
928-
if (el.value.length > 0){
929-
renderFormValue(el, bounds, stack);
915+
if (element.value.length > 0){
916+
renderFormValue(element, bounds, stack);
930917
}
931918
break;
932919
case "SELECT":
933-
if (el.options.length > 0){
934-
renderFormValue(el, bounds, stack);
920+
if (element.options.length > 0){
921+
renderFormValue(element, bounds, stack);
935922
}
936923
break;
937924
case "LI":
938-
renderListItem(el, stack, backgroundBounds);
925+
renderListItem(element, stack, backgroundBounds);
939926
break;
940927
case "CANVAS":
941-
renderImage(ctx, el, el, bounds, borders);
928+
renderImage(ctx, element, element, bounds, borders);
942929
break;
943930
}
944931

945-
return zindex.children[stackLength - 1];
932+
return stack;
946933
}
947934

948935
function isElementVisible(element) {
@@ -1051,7 +1038,6 @@ _html2canvas.Parse = function (images, options) {
10511038
stack.backgroundColor = getCSS(document.documentElement, "backgroundColor");
10521039

10531040
return stack;
1054-
10551041
};
10561042

10571043
function h2czContext(zindex) {

src/Util.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ window.html2canvas = function(elements, opts) {
1414

1515
// parse options
1616
svgRendering: false, // use svg powered rendering where available (FF11+)
17-
iframeDefault: "default",
1817
ignoreElements: "IFRAME|OBJECT|PARAM",
1918
useOverflow: true,
2019
letterRendering: false,

0 commit comments

Comments
 (0)