8000 Fixed Previewers bug #1496 (#1497) · PrismJS/prism@4b56f3c · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Commit 4b56f3c

Browse files
RunDevelopmentmAAdhaTTah
authored andcommitted
Fixed Previewers bug #1496 (#1497)
The problem was a bug unique to Firefox which causes offsetTop of span.token to always be 0 under certain conditions. It only occurred together with of the line-number plugin because setting position: relative for the pre > code elements triggers that FF bug. The offset is now calculated using bounding boxes. Tested in Chrome, Edge, IE, FF, and Opera. Fixes #1496.
1 parent 44fed4d commit 4b56f3c

File tree

2 files changed

+15
-22
lines changed

2 files changed

+15
-22
lines changed

plugins/previewers/prism-previewers.js

+14-21
Original file line numberDiff line numberDiff line change
@@ -470,30 +470,23 @@
470470
/**
471471
* Returns the absolute X, Y offsets for an element
472472
* @param {HTMLElement} element
473-
* @returns {{top: number, right: number, bottom: number, left: number}}
473+
* @returns {{top: number, right: number, bottom: number, left: number, width: number, height: number}}
474474
*/
475475
var getOffset = function (element) {
476-
var left = 0, top = 0, el = element;
477-
478-
if (el.parentNode) {
479-
do {
480-
left += el.offsetLeft;
481-
top += el.offsetTop;
482-
} while ((el = el.offsetParent) && el.nodeType < 9);
483-
484-
el = element;
485-
486-
do {
487-
left -= el.scrollLeft;
488-
top -= el.scrollTop;
489-
} while ((el = el.parentNode) && !/body/i.test(el.nodeName));
490-
}
476+
var elementBounds = element.getBoundingClientRect();
477+
var left = elementBounds.left;
478+
var top = elementBounds.top;
479+
var documentBounds = document.documentElement.getBoundingClientRect();
480+
left -= documentBounds.left;
481+
top -= documentBounds.top;
491482

492483
return {
493484
top: top,
494-
right: innerWidth - left - element.offsetWidth,
495-
bottom: innerHeight - top - element.offsetHeight,
496-
left: left
485+
right: innerWidth - left - elementBounds.width,
486+
bottom: innerHeight - top - elementBounds.height,
487+
left: left,
488+
width: elementBounds.width,
489+
height: elementBounds.height
497490
};
498491
};
499492

@@ -621,7 +614,7 @@
621614
this._elt.style.top = '';
622615
}
623616

624-
this._elt.style.left = offset.left + Math.min(200, this._token.offsetWidth / 2) + 'px';
617+
this._elt.style.left = offset.left + Math.min(200, offset.width / 2) + 'px';
625618
} else {
626619
this.hide();
627620
}
@@ -712,4 +705,4 @@
712705
previewers[previewer].create();
713706
}
714707

715-
}());
708+
}());

0 commit comments

Comments
 (0)
0