refactored parsing init

This commit is contained in:
Niklas von Hertzen 2012-12-30 16:06:59 +02:00
parent d93e36d768
commit 0dd2c24ab4

View File

@ -8,11 +8,6 @@ _html2canvas.Parse = function (images, options) {
support = _html2canvas.Util.Support(options, doc), support = _html2canvas.Util.Support(options, doc),
ignoreElementsRegExp = new RegExp("(" + options.ignoreElements + ")"), ignoreElementsRegExp = new RegExp("(" + options.ignoreElements + ")"),
body = doc.body, body = doc.body,
stack,
ctx,
i,
children,
childrenLen,
getCSS = _html2canvas.Util.getCSS; getCSS = _html2canvas.Util.getCSS;
images = images || {}; images = images || {};
@ -165,7 +160,7 @@ _html2canvas.Parse = function (images, options) {
} }
} }
function renderTextDecoration(text_decoration, bounds, metrics, color) { function renderTextDecoration(ctx, text_decoration, bounds, metrics, color) {
switch(text_decoration) { switch(text_decoration) {
case "underline": case "underline":
// Draws a line at the baseline of the font // Draws a line at the baseline of the font
@ -243,7 +238,7 @@ _html2canvas.Parse = function (images, options) {
var bounds = getTextBounds(state, text, textDecoration, (index < textList.length - 1)); var bounds = getTextBounds(state, text, textDecoration, (index < textList.length - 1));
if (bounds) { if (bounds) {
drawText(text, bounds.left, bounds.bottom, ctx); drawText(text, bounds.left, bounds.bottom, ctx);
renderTextDecoration(textDecoration, bounds, metrics, color); renderTextDecoration(ctx, textDecoration, bounds, metrics, color);
} }
}); });
} }
@ -317,6 +312,7 @@ _html2canvas.Parse = function (images, options) {
function renderListItem(element, stack, elBounds) { function renderListItem(element, stack, elBounds) {
var x, var x,
text, text,
ctx = stack.ctx,
type = getCSS(element, "listStyleType"), type = getCSS(element, "listStyleType"),
listBounds; listBounds;
@ -850,8 +846,6 @@ _html2canvas.Parse = function (images, options) {
if (isElementVisible(el)) { if (isElementVisible(el)) {
stack = renderElement(el, stack) || stack; stack = renderElement(el, stack) || stack;
ctx = stack.ctx;
if (!ignoreElementsRegExp.test(el.nodeName)) { if (!ignoreElementsRegExp.test(el.nodeName)) {
_html2canvas.Util.Children(el).forEach(function(node) { _html2canvas.Util.Children(el).forEach(function(node) {
if (node.nodeType === 1) { if (node.nodeType === 1) {
@ -864,90 +858,89 @@ _html2canvas.Parse = function (images, options) {
} }
} }
stack = renderElement(element, null); function svgDOMRender(body, stack) {
var img = new Image(),
docWidth = documentWidth(),
docHeight = documentHeight(),
html = "";
/* function parseDOM(el) {
SVG powered HTML rendering, non-tainted canvas available from FF 11+ onwards var children = _html2canvas.Util.Children( el ),
*/ len = children.length,
attr,
a,
alen,
elm,
i;
for ( i = 0; i < len; i+=1 ) {
elm = children[ i ];
if ( elm.nodeType === 3 ) {
// Text node
html += elm.nodeValue.replace(/</g,"&lt;").replace(/>/g,"&gt;");
} else if ( elm.nodeType === 1 ) {
// Element
if ( !/^(script|meta|title)$/.test(elm.nodeName.toLowerCase()) ) {
if ( support.svgRendering ) { html += "<" + elm.nodeName.toLowerCase();
(function( body ){
var img = new Image(),
docWidth = documentWidth(),
docHeight = documentHeight(),
html = "";
function parseDOM( el ) { // add attributes
var children = _html2canvas.Util.Children( el ), if ( elm.hasAttributes() ) {
len = children.length, attr = elm.attributes;
attr, alen = attr.length;
a, for ( a = 0; a < alen; a+=1 ) {
alen, html += " " + attr[ a ].name + '="' + attr[ a ].value + '"';
elm,
i;
for ( i = 0; i < len; i+=1 ) {
elm = children[ i ];
if ( elm.nodeType === 3 ) {
// Text node
html += elm.nodeValue.replace(/</g,"&lt;").replace(/>/g,"&gt;");
} else if ( elm.nodeType === 1 ) {
// Element
if ( !/^(script|meta|title)$/.test(elm.nodeName.toLowerCase()) ) {
html += "<" + elm.nodeName.toLowerCase();
// add attributes
if ( elm.hasAttributes() ) {
attr = elm.attributes;
alen = attr.length;
for ( a = 0; a < alen; a+=1 ) {
html += " " + attr[ a ].name + '="' + attr[ a ].value + '"';
}
} }
html += '>';
parseDOM( elm );
html += "</" + elm.nodeName.toLowerCase() + ">";
} }
}
html += '>';
parseDOM( elm );
html += "</" + elm.nodeName.toLowerCase() + ">";
}
} }
} }
parseDOM(body); }
img.src = [
"data:image/svg+xml,",
"<svg xmlns='http://www.w3.org/2000/svg' version='1.1' width='" + docWidth + "' height='" + docHeight + "'>",
"<foreignObject width='" + docWidth + "' height='" + docHeight + "'>",
"<html xmlns='http://www.w3.org/1999/xhtml' style='margin:0;'>",
html.replace(/\#/g,"%23"),
"</html>",
"</foreignObject>",
"</svg>"
].join("");
img.onload = function() { parseDOM(body);
stack.svgRender = img; img.src = [
}; "data:image/svg+xml,",
"<svg xmlns='http://www.w3.org/2000/svg' version='1.1' width='" + docWidth + "' height='" + docHeight + "'>",
"<foreignObject width='" + docWidth + "' height='" + docHeight + "'>",
"<html xmlns='http://www.w3.org/1999/xhtml' style='margin:0;'>",
html.replace(/\#/g,"%23"),
"</html>",
"</foreignObject>",
"</svg>"
].join("");
})(document.documentElement); img.onload = function() {
stack.svgRender = img;
};
} }
function init() {
var stack = renderElement(element, null);
// parse every child element if (support.svgRendering) {
for (i = 0, children = element.children, childrenLen = children.length; i < childrenLen; i+=1){ svgDOMRender(document.documentElement, stack);
parseElement(children[i], stack); }
Array.prototype.slice.call(element.children, 0).forEach(function(childElement) {
parseElement(childElement, stack);
});
stack.backgroundColor = getCSS(document.documentElement, "backgroundColor");
return stack;
} }
stack.backgroundColor = getCSS(document.documentElement, "backgroundColor"); return init();
return stack;
}; };
function h2czContext(zindex) { function h2czContext(zindex) {