Index: lib/src/prism/plugins/line-highlight/prism-line-highlight.js |
diff --git a/lib/src/prism/plugins/line-highlight/prism-line-highlight.js b/lib/src/prism/plugins/line-highlight/prism-line-highlight.js |
index 577d142e981e1d616c432a9a4d1869c33ab23900..975357ead96f887508ae63109ff6312dc238117f 100644 |
--- a/lib/src/prism/plugins/line-highlight/prism-line-highlight.js |
+++ b/lib/src/prism/plugins/line-highlight/prism-line-highlight.js |
@@ -1,6 +1,6 @@ |
(function(){ |
-if(!window.Prism) { |
+if (typeof self === 'undefined' || !self.Prism || !self.document || !document.querySelector) { |
return; |
} |
@@ -13,13 +13,34 @@ function hasClass(element, className) { |
return (" " + element.className + " ").replace(/[\n\t]/g, " ").indexOf(className) > -1 |
} |
-var CRLF = crlf = /\r?\n|\r/g; |
- |
+// Some browsers round the line-height, others don't. |
+// We need to test for it to position the elements properly. |
+var isLineHeightRounded = (function() { |
+ var res; |
+ return function() { |
+ if(typeof res === 'undefined') { |
+ var d = document.createElement('div'); |
+ d.style.fontSize = '13px'; |
+ d.style.lineHeight = '1.5'; |
+ d.style.padding = 0; |
+ d.style.border = 0; |
+ d.innerHTML = ' <br /> '; |
+ document.body.appendChild(d); |
+ // Browsers that round the line-height should have offsetHeight === 38 |
+ // The others should have 39. |
+ res = d.offsetHeight === 38; |
+ document.body.removeChild(d); |
+ } |
+ return res; |
+ } |
+}()); |
+ |
function highlightLines(pre, lines, classes) { |
var ranges = lines.replace(/\s+/g, '').split(','), |
offset = +pre.getAttribute('data-line-offset') || 0; |
- |
- var lineHeight = parseFloat(getComputedStyle(pre).lineHeight); |
+ |
+ var parseMethod = isLineHeightRounded() ? parseInt : parseFloat; |
+ var lineHeight = parseMethod(getComputedStyle(pre).lineHeight); |
for (var i=0, range; range = ranges[i++];) { |
range = range.split('-'); |
@@ -85,7 +106,7 @@ function applyHash() { |
var fakeTimer = 0; // Hack to limit the number of times applyHash() runs |
-Prism.hooks.add('after-highlight', function(env) { |
+Prism.hooks.add('complete', function(env) { |
var pre = env.element.parentNode; |
var lines = pre && pre.getAttribute('data-line'); |