Skip to content

Commit 91981ad

Browse files
committed
Fixed bug
This closes issue #2
1 parent 486c010 commit 91981ad

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

src/highlightjs-line-numbers.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,7 @@
2828
if (typeof element !== 'object') return;
2929

3030
var parent = element.parentNode;
31-
var lines = parent.textContent.match(/\r\n|\r|\n/g);
32-
33-
lines = lines ? lines.length : 0;
34-
35-
// fix for IE
36-
if (isIE()) lines += 1;
31+
var lines = getCountLines(parent.textContent);
3732

3833
if (lines > 1) {
3934
var l = '';
@@ -50,8 +45,17 @@
5045
}
5146
}
5247

53-
function isIE(userAgent) {
54-
userAgent = userAgent || w.navigator.userAgent;
55-
return userAgent.indexOf("MSIE ") > -1 || userAgent.indexOf("Trident/") > -1;
48+
function getCountLines(text) {
49+
if (text.length === 0) return 0;
50+
51+
var regExp = /\r\n|\r|\n/g;
52+
var lines = text.match(regExp);
53+
lines = lines ? lines.length : 0;
54+
55+
if (!text[text.length - 1].match(regExp)) {
56+
lines += 1;
57+
}
58+
59+
return lines;
5660
}
5761
}(window));

0 commit comments

Comments
 (0)