1
0
Fork 0
mirror of https://github.com/futurepress/epub.js.git synced 2025-10-05 15:32:55 +02:00

Fix locationOf for empty hash target in Webkit

Internal hash links were broken in Webkit if they pointed to empty elements. This change fixes that by temporarily inserting a zero-width space into elements before measuring them.
This commit is contained in:
Elliot Dickison 2022-11-14 23:28:38 -08:00 committed by GitHub
parent 0963efe979
commit 8fef3b733d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -667,10 +667,18 @@ class Contents {
let el = this.document.getElementById(id);
if(el) {
if (isWebkit) {
// Webkit reports incorrect bounding rects in Columns
// Webkit reports incorrect bounding rects in Columns unless a range
// is used to measure
let newRange = new Range();
newRange.selectNode(el);
position = newRange.getBoundingClientRect();
// Webkit reports a position of 0/0 for empty ranges. Temporarily
// inserting a zero-width space ensures that we can locate empty target
// elements
let zeroWidthSpace = document.createTextNode('\ufeff');
newRange.insertNode(zeroWidthSpace);
position = newRange.getBoundingClientRect();
zeroWidthSpace.remove();
} else {
position = el.getBoundingClientRect();
}