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:
parent
0963efe979
commit
8fef3b733d
1 changed files with 10 additions and 2 deletions
|
@ -667,10 +667,18 @@ class Contents {
|
||||||
let el = this.document.getElementById(id);
|
let el = this.document.getElementById(id);
|
||||||
if(el) {
|
if(el) {
|
||||||
if (isWebkit) {
|
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();
|
let newRange = new Range();
|
||||||
newRange.selectNode(el);
|
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 {
|
} else {
|
||||||
position = el.getBoundingClientRect();
|
position = el.getBoundingClientRect();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue