mirror of
https://github.com/niklasvh/html2canvas.git
synced 2023-08-10 21:13:10 +03:00
Don't load javascript on cloned content by default
This commit is contained in:
commit
e6d31ada4a
29
dist/html2canvas.js
vendored
29
dist/html2canvas.js
vendored
@ -580,12 +580,13 @@ window.html2canvas = function(nodeList, options) {
|
|||||||
options.async = typeof(options.async) === "undefined" ? true : options.async;
|
options.async = typeof(options.async) === "undefined" ? true : options.async;
|
||||||
options.allowTaint = typeof(options.allowTaint) === "undefined" ? false : options.allowTaint;
|
options.allowTaint = typeof(options.allowTaint) === "undefined" ? false : options.allowTaint;
|
||||||
options.removeContainer = typeof(options.removeContainer) === "undefined" ? true : options.removeContainer;
|
options.removeContainer = typeof(options.removeContainer) === "undefined" ? true : options.removeContainer;
|
||||||
|
options.javascriptEnabled = typeof(options.javascriptEnabled) === "undefined" ? false : options.javascriptEnabled;
|
||||||
|
|
||||||
if (typeof(nodeList) === "string") {
|
if (typeof(nodeList) === "string") {
|
||||||
if (typeof(options.proxy) !== "string") {
|
if (typeof(options.proxy) !== "string") {
|
||||||
return Promise.reject("Proxy must be used when rendering url");
|
return Promise.reject("Proxy must be used when rendering url");
|
||||||
}
|
}
|
||||||
return loadUrlDocument(absoluteUrl(nodeList), options.proxy, document, window.innerWidth, window.innerHeight).then(function(container) {
|
return loadUrlDocument(absoluteUrl(nodeList), options.proxy, document, window.innerWidth, window.innerHeight, options).then(function(container) {
|
||||||
return renderWindow(container.contentWindow.document.documentElement, container, options, window.innerWidth, window.innerHeight);
|
return renderWindow(container.contentWindow.document.documentElement, container, options, window.innerWidth, window.innerHeight);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -627,12 +628,16 @@ function renderWindow(node, container, options, windowWidth, windowHeight) {
|
|||||||
return parser.ready.then(function() {
|
return parser.ready.then(function() {
|
||||||
log("Finished rendering");
|
log("Finished rendering");
|
||||||
var canvas = (options.type !== "view" && (node === clonedWindow.document.body || node === clonedWindow.document.documentElement)) ? renderer.canvas : crop(renderer.canvas, bounds);
|
var canvas = (options.type !== "view" && (node === clonedWindow.document.body || node === clonedWindow.document.documentElement)) ? renderer.canvas : crop(renderer.canvas, bounds);
|
||||||
|
cleanupContainer(container, options);
|
||||||
|
return canvas;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function cleanupContainer(container, options) {
|
||||||
if (options.removeContainer) {
|
if (options.removeContainer) {
|
||||||
container.parentNode.removeChild(container);
|
container.parentNode.removeChild(container);
|
||||||
log("Cleaned up container");
|
log("Cleaned up container");
|
||||||
}
|
}
|
||||||
return canvas;
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function crop(canvas, bounds) {
|
function crop(canvas, bounds) {
|
||||||
@ -698,16 +703,16 @@ function createWindowClone(ownerDocument, containerDocument, width, height, opti
|
|||||||
documentClone.write("<!DOCTYPE html>");
|
documentClone.write("<!DOCTYPE html>");
|
||||||
documentClone.close();
|
documentClone.close();
|
||||||
|
|
||||||
documentClone.replaceChild(removeScriptNodes(documentClone.adoptNode(documentElement)), documentClone.documentElement);
|
documentClone.replaceChild(options.javascriptEnabled === true ? documentClone.adoptNode(documentElement) : removeScriptNodes(documentClone.adoptNode(documentElement)), documentClone.documentElement);
|
||||||
if (options.type === "view") {
|
if (options.type === "view") {
|
||||||
container.contentWindow.scrollTo(window.pageXOffset, window.pageYOffset);
|
container.contentWindow.scrollTo(window.pageXOffset, window.pageYOffset);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function loadUrlDocument(src, proxy, document, width, height) {
|
function loadUrlDocument(src, proxy, document, width, height, options) {
|
||||||
return new Proxy(src, proxy, window.document).then(documentFromHTML(src)).then(function(doc) {
|
return new Proxy(src, proxy, window.document).then(documentFromHTML(src)).then(function(doc) {
|
||||||
return createWindowClone(doc, document, width, height, {});
|
return createWindowClone(doc, document, width, height, options);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -852,12 +857,12 @@ FontMetrics.prototype.getMetrics = function(family, size) {
|
|||||||
return this.data[family + "-" + size];
|
return this.data[family + "-" + size];
|
||||||
};
|
};
|
||||||
|
|
||||||
function FrameContainer(container, sameOrigin, proxy) {
|
function FrameContainer(container, sameOrigin, options) {
|
||||||
this.image = null;
|
this.image = null;
|
||||||
this.src = container;
|
this.src = container;
|
||||||
var self = this;
|
var self = this;
|
||||||
var bounds = getBounds(container);
|
var bounds = getBounds(container);
|
||||||
this.promise = (!sameOrigin ? this.proxyLoad(proxy, bounds) : new Promise(function(resolve) {
|
this.promise = (!sameOrigin ? this.proxyLoad(options.proxy, bounds, options) : new Promise(function(resolve) {
|
||||||
if (container.contentWindow.document.URL === "about:blank" || container.contentWindow.document.documentElement == null) {
|
if (container.contentWindow.document.URL === "about:blank" || container.contentWindow.document.documentElement == null) {
|
||||||
container.contentWindow.onload = container.onload = function() {
|
container.contentWindow.onload = container.onload = function() {
|
||||||
resolve(container);
|
resolve(container);
|
||||||
@ -866,15 +871,15 @@ function FrameContainer(container, sameOrigin, proxy) {
|
|||||||
resolve(container);
|
resolve(container);
|
||||||
}
|
}
|
||||||
})).then(function(container) {
|
})).then(function(container) {
|
||||||
return html2canvas(container.contentWindow.document.documentElement, {type: 'view', proxy: proxy});
|
return html2canvas(container.contentWindow.document.documentElement, {type: 'view', proxy: options.proxy, javascriptEnabled: options.javascriptEnabled, removeContainer: options.removeContainer});
|
||||||
}).then(function(canvas) {
|
}).then(function(canvas) {
|
||||||
return self.image = canvas;
|
return self.image = canvas;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
FrameContainer.prototype.proxyLoad = function(proxy, bounds) {
|
FrameContainer.prototype.proxyLoad = function(proxy, bounds, options) {
|
||||||
var container = this.src;
|
var container = this.src;
|
||||||
return loadUrlDocument(container.src, proxy, container.ownerDocument, bounds.width, bounds.height);
|
return loadUrlDocument(container.src, proxy, container.ownerDocument, bounds.width, bounds.height, options);
|
||||||
};
|
};
|
||||||
|
|
||||||
function GradientContainer(imageData) {
|
function GradientContainer(imageData) {
|
||||||
@ -982,7 +987,7 @@ ImageLoader.prototype.loadImage = function(imageData) {
|
|||||||
} else if (imageData.method === "svg") {
|
} else if (imageData.method === "svg") {
|
||||||
return new SVGNodeContainer(imageData.args[0], this.support.svg);
|
return new SVGNodeContainer(imageData.args[0], this.support.svg);
|
||||||
} else if (imageData.method === "IFRAME") {
|
} else if (imageData.method === "IFRAME") {
|
||||||
return new FrameContainer(imageData.args[0], this.isSameOrigin(imageData.args[0].src), this.options.proxy);
|
return new FrameContainer(imageData.args[0], this.isSameOrigin(imageData.args[0].src), this.options);
|
||||||
} else {
|
} else {
|
||||||
return new DummyImageContainer(imageData);
|
return new DummyImageContainer(imageData);
|
||||||
}
|
}
|
||||||
|
4
dist/html2canvas.min.js
vendored
4
dist/html2canvas.min.js
vendored
File diff suppressed because one or more lines are too long
17
src/core.js
17
src/core.js
@ -12,12 +12,13 @@ window.html2canvas = function(nodeList, options) {
|
|||||||
options.async = typeof(options.async) === "undefined" ? true : options.async;
|
options.async = typeof(options.async) === "undefined" ? true : options.async;
|
||||||
options.allowTaint = typeof(options.allowTaint) === "undefined" ? false : options.allowTaint;
|
options.allowTaint = typeof(options.allowTaint) === "undefined" ? false : options.allowTaint;
|
||||||
options.removeContainer = typeof(options.removeContainer) === "undefined" ? true : options.removeContainer;
|
options.removeContainer = typeof(options.removeContainer) === "undefined" ? true : options.removeContainer;
|
||||||
|
options.javascriptEnabled = typeof(options.javascriptEnabled) === "undefined" ? false : options.javascriptEnabled;
|
||||||
|
|
||||||
if (typeof(nodeList) === "string") {
|
if (typeof(nodeList) === "string") {
|
||||||
if (typeof(options.proxy) !== "string") {
|
if (typeof(options.proxy) !== "string") {
|
||||||
return Promise.reject("Proxy must be used when rendering url");
|
return Promise.reject("Proxy must be used when rendering url");
|
||||||
}
|
}
|
||||||
return loadUrlDocument(absoluteUrl(nodeList), options.proxy, document, window.innerWidth, window.innerHeight).then(function(container) {
|
return loadUrlDocument(absoluteUrl(nodeList), options.proxy, document, window.innerWidth, window.innerHeight, options).then(function(container) {
|
||||||
return renderWindow(container.contentWindow.document.documentElement, container, options, window.innerWidth, window.innerHeight);
|
return renderWindow(container.contentWindow.document.documentElement, container, options, window.innerWidth, window.innerHeight);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -59,12 +60,16 @@ function renderWindow(node, container, options, windowWidth, windowHeight) {
|
|||||||
return parser.ready.then(function() {
|
return parser.ready.then(function() {
|
||||||
log("Finished rendering");
|
log("Finished rendering");
|
||||||
var canvas = (options.type !== "view" && (node === clonedWindow.document.body || node === clonedWindow.document.documentElement)) ? renderer.canvas : crop(renderer.canvas, bounds);
|
var canvas = (options.type !== "view" && (node === clonedWindow.document.body || node === clonedWindow.document.documentElement)) ? renderer.canvas : crop(renderer.canvas, bounds);
|
||||||
|
cleanupContainer(container, options);
|
||||||
|
return canvas;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function cleanupContainer(container, options) {
|
||||||
if (options.removeContainer) {
|
if (options.removeContainer) {
|
||||||
container.parentNode.removeChild(container);
|
container.parentNode.removeChild(container);
|
||||||
log("Cleaned up container");
|
log("Cleaned up container");
|
||||||
}
|
}
|
||||||
return canvas;
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function crop(canvas, bounds) {
|
function crop(canvas, bounds) {
|
||||||
@ -130,16 +135,16 @@ function createWindowClone(ownerDocument, containerDocument, width, height, opti
|
|||||||
documentClone.write("<!DOCTYPE html>");
|
documentClone.write("<!DOCTYPE html>");
|
||||||
documentClone.close();
|
documentClone.close();
|
||||||
|
|
||||||
documentClone.replaceChild(removeScriptNodes(documentClone.adoptNode(documentElement)), documentClone.documentElement);
|
documentClone.replaceChild(options.javascriptEnabled === true ? documentClone.adoptNode(documentElement) : removeScriptNodes(documentClone.adoptNode(documentElement)), documentClone.documentElement);
|
||||||
if (options.type === "view") {
|
if (options.type === "view") {
|
||||||
container.contentWindow.scrollTo(window.pageXOffset, window.pageYOffset);
|
container.contentWindow.scrollTo(window.pageXOffset, window.pageYOffset);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function loadUrlDocument(src, proxy, document, width, height) {
|
function loadUrlDocument(src, proxy, document, width, height, options) {
|
||||||
return new Proxy(src, proxy, window.document).then(documentFromHTML(src)).then(function(doc) {
|
return new Proxy(src, proxy, window.document).then(documentFromHTML(src)).then(function(doc) {
|
||||||
return createWindowClone(doc, document, width, height, {});
|
return createWindowClone(doc, document, width, height, options);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
function FrameContainer(container, sameOrigin, proxy) {
|
function FrameContainer(container, sameOrigin, options) {
|
||||||
this.image = null;
|
this.image = null;
|
||||||
this.src = container;
|
this.src = container;
|
||||||
var self = this;
|
var self = this;
|
||||||
var bounds = getBounds(container);
|
var bounds = getBounds(container);
|
||||||
this.promise = (!sameOrigin ? this.proxyLoad(proxy, bounds) : new Promise(function(resolve) {
|
this.promise = (!sameOrigin ? this.proxyLoad(options.proxy, bounds, options) : new Promise(function(resolve) {
|
||||||
if (container.contentWindow.document.URL === "about:blank" || container.contentWindow.document.documentElement == null) {
|
if (container.contentWindow.document.URL === "about:blank" || container.contentWindow.document.documentElement == null) {
|
||||||
container.contentWindow.onload = container.onload = function() {
|
container.contentWindow.onload = container.onload = function() {
|
||||||
resolve(container);
|
resolve(container);
|
||||||
@ -12,13 +12,13 @@ function FrameContainer(container, sameOrigin, proxy) {
|
|||||||
resolve(container);
|
resolve(container);
|
||||||
}
|
}
|
||||||
})).then(function(container) {
|
})).then(function(container) {
|
||||||
return html2canvas(container.contentWindow.document.documentElement, {type: 'view', proxy: proxy});
|
return html2canvas(container.contentWindow.document.documentElement, {type: 'view', proxy: options.proxy, javascriptEnabled: options.javascriptEnabled, removeContainer: options.removeContainer});
|
||||||
}).then(function(canvas) {
|
}).then(function(canvas) {
|
||||||
return self.image = canvas;
|
return self.image = canvas;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
FrameContainer.prototype.proxyLoad = function(proxy, bounds) {
|
FrameContainer.prototype.proxyLoad = function(proxy, bounds, options) {
|
||||||
var container = this.src;
|
var container = this.src;
|
||||||
return loadUrlDocument(container.src, proxy, container.ownerDocument, bounds.width, bounds.height);
|
return loadUrlDocument(container.src, proxy, container.ownerDocument, bounds.width, bounds.height, options);
|
||||||
};
|
};
|
||||||
|
@ -69,7 +69,7 @@ ImageLoader.prototype.loadImage = function(imageData) {
|
|||||||
} else if (imageData.method === "svg") {
|
} else if (imageData.method === "svg") {
|
||||||
return new SVGNodeContainer(imageData.args[0], this.support.svg);
|
return new SVGNodeContainer(imageData.args[0], this.support.svg);
|
||||||
} else if (imageData.method === "IFRAME") {
|
} else if (imageData.method === "IFRAME") {
|
||||||
return new FrameContainer(imageData.args[0], this.isSameOrigin(imageData.args[0].src), this.options.proxy);
|
return new FrameContainer(imageData.args[0], this.isSameOrigin(imageData.args[0].src), this.options);
|
||||||
} else {
|
} else {
|
||||||
return new DummyImageContainer(imageData);
|
return new DummyImageContainer(imageData);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user