mirror of
https://github.com/niklasvh/html2canvas.git
synced 2023-08-10 21:13:10 +03:00
Fix scroll to top with pages using hashtag anchors
This commit is contained in:
parent
0579804cbb
commit
1e826e32ae
12
dist/html2canvas.js
vendored
12
dist/html2canvas.js
vendored
@ -712,6 +712,7 @@ function createWindowClone(ownerDocument, containerDocument, width, height, opti
|
||||
cloneCanvasContents(ownerDocument, documentClone);
|
||||
clearInterval(interval);
|
||||
if (options.type === "view") {
|
||||
restoreOwnerScroll(ownerDocument, x, y);
|
||||
container.contentWindow.scrollTo(x, y);
|
||||
}
|
||||
resolve(container);
|
||||
@ -724,16 +725,19 @@ function createWindowClone(ownerDocument, containerDocument, width, height, opti
|
||||
|
||||
documentClone.open();
|
||||
documentClone.write("<!DOCTYPE html><html></html>");
|
||||
|
||||
// Chrome scrolls the parent document for some reason after the write to the cloned window???
|
||||
if (x !== ownerDocument.defaultView.pageXOffset || y !== ownerDocument.defaultView.pageYOffset) {
|
||||
ownerDocument.defaultView.scrollTo(x, y);
|
||||
}
|
||||
restoreOwnerScroll(ownerDocument, x, y);
|
||||
documentClone.replaceChild(options.javascriptEnabled === true ? documentClone.adoptNode(documentElement) : removeScriptNodes(documentClone.adoptNode(documentElement)), documentClone.documentElement);
|
||||
documentClone.close();
|
||||
});
|
||||
}
|
||||
|
||||
function restoreOwnerScroll(ownerDocument, x, y) {
|
||||
if (x !== ownerDocument.defaultView.pageXOffset || y !== ownerDocument.defaultView.pageYOffset) {
|
||||
ownerDocument.defaultView.scrollTo(x, y);
|
||||
}
|
||||
}
|
||||
|
||||
function loadUrlDocument(src, proxy, document, width, height, options) {
|
||||
return new Proxy(src, proxy, window.document).then(documentFromHTML(src)).then(function(doc) {
|
||||
return createWindowClone(doc, document, width, height, options);
|
||||
|
4
dist/html2canvas.min.js
vendored
4
dist/html2canvas.min.js
vendored
File diff suppressed because one or more lines are too long
12
src/core.js
12
src/core.js
@ -144,6 +144,7 @@ function createWindowClone(ownerDocument, containerDocument, width, height, opti
|
||||
cloneCanvasContents(ownerDocument, documentClone);
|
||||
clearInterval(interval);
|
||||
if (options.type === "view") {
|
||||
restoreOwnerScroll(ownerDocument, x, y);
|
||||
container.contentWindow.scrollTo(x, y);
|
||||
}
|
||||
resolve(container);
|
||||
@ -156,16 +157,19 @@ function createWindowClone(ownerDocument, containerDocument, width, height, opti
|
||||
|
||||
documentClone.open();
|
||||
documentClone.write("<!DOCTYPE html><html></html>");
|
||||
|
||||
// Chrome scrolls the parent document for some reason after the write to the cloned window???
|
||||
if (x !== ownerDocument.defaultView.pageXOffset || y !== ownerDocument.defaultView.pageYOffset) {
|
||||
ownerDocument.defaultView.scrollTo(x, y);
|
||||
}
|
||||
restoreOwnerScroll(ownerDocument, x, y);
|
||||
documentClone.replaceChild(options.javascriptEnabled === true ? documentClone.adoptNode(documentElement) : removeScriptNodes(documentClone.adoptNode(documentElement)), documentClone.documentElement);
|
||||
documentClone.close();
|
||||
});
|
||||
}
|
||||
|
||||
function restoreOwnerScroll(ownerDocument, x, y) {
|
||||
if (x !== ownerDocument.defaultView.pageXOffset || y !== ownerDocument.defaultView.pageYOffset) {
|
||||
ownerDocument.defaultView.scrollTo(x, y);
|
||||
}
|
||||
}
|
||||
|
||||
function loadUrlDocument(src, proxy, document, width, height, options) {
|
||||
return new Proxy(src, proxy, window.document).then(documentFromHTML(src)).then(function(doc) {
|
||||
return createWindowClone(doc, document, width, height, options);
|
||||
|
64
tests/mocha/scrolling.html
Normal file
64
tests/mocha/scrolling.html
Normal file
@ -0,0 +1,64 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head lang="en">
|
||||
<meta charset="UTF-8">
|
||||
<title>Scrolling tests</title>
|
||||
<link rel="stylesheet" href="lib/mocha.css" />
|
||||
<script src="../../dist/html2canvas.js"></script>
|
||||
<script src="../assets/jquery-1.6.2.js"></script>
|
||||
<script src="lib/expect.js"></script>
|
||||
<script src="lib/mocha.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<div id="mocha"></div>
|
||||
<script>mocha.setup('bdd')</script>
|
||||
<div style="height: 2100px;"></div>
|
||||
<div style="height: 500px;background: green;"><a name="content">content</a></div>
|
||||
<script>
|
||||
describe("Scrolling", function() {
|
||||
it("with random scroll", function (done) {
|
||||
$(window).scrollTop(123);
|
||||
setTimeout(function() {
|
||||
html2canvas(document.body, {type: 'view'}).then(function () {
|
||||
expect($(window).scrollTop()).to.equal(123);
|
||||
done();
|
||||
}).catch(function (error) {
|
||||
done(error);
|
||||
});
|
||||
}, 0);
|
||||
});
|
||||
|
||||
it("with url anchor", function (done) {
|
||||
window.location.hash = "#content";
|
||||
setTimeout(function() {
|
||||
var top = $(window).scrollTop();
|
||||
html2canvas(document.body, {type: 'view', logging: true}).then(function (canvas) {
|
||||
var currentTop = $(window).scrollTop();
|
||||
window.location.hash = "";
|
||||
expect(currentTop).to.be.greaterThan(2000);
|
||||
expect(currentTop).to.equal(top);
|
||||
done();
|
||||
}).catch(function (error) {
|
||||
done(error);
|
||||
});
|
||||
}, 0);
|
||||
});
|
||||
});
|
||||
|
||||
after(function() {
|
||||
if (history) {
|
||||
history.pushState("", document.title, window.location.pathname + window.location.search);
|
||||
}
|
||||
});
|
||||
|
||||
mocha.checkLeaks();
|
||||
mocha.globals(['jQuery']);
|
||||
if (window.mochaPhantomJS) {
|
||||
mochaPhantomJS.run();
|
||||
}
|
||||
else {
|
||||
mocha.run();
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in New Issue
Block a user