From b5dac3d915a56228ebbd41fecabf02385e35f9cf Mon Sep 17 00:00:00 2001 From: Yauheni Pakala Date: Thu, 25 Feb 2016 23:23:25 +0300 Subject: [PATCH] Improvement loading process. Issue #8 --- dist/highlightjs-line-numbers.min.js | 2 +- src/highlightjs-line-numbers.js | 28 +++++++++++++++++----------- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/dist/highlightjs-line-numbers.min.js b/dist/highlightjs-line-numbers.min.js index afaaf7b..6d0a3cb 100644 --- a/dist/highlightjs-line-numbers.min.js +++ b/dist/highlightjs-line-numbers.min.js @@ -1 +1 @@ -!function(e){"use strict";function t(){e.addEventListener("load",function(){try{var e=document.querySelectorAll("code.hljs");for(var t in e)e.hasOwnProperty(t)&&n(e[t])}catch(r){console.error("LineNumbers error: ",r)}})}function n(e){if("object"==typeof e){var t=e.parentNode,n=r(t.textContent);if(n>1){for(var o="",l=0;n>l;l++)o+=l+1+"\n";var c=document.createElement("code");c.className="hljs hljs-line-numbers",c.style["float"]="left",c.textContent=o,t.insertBefore(c,e)}}}function r(e){if(0===e.length)return 0;var t=/\r\n|\r|\n/g,n=e.match(t);return n=n?n.length:0,e[e.length-1].match(t)||(n+=1),n}"undefined"==typeof e.hljs?console.error("highlight.js not detected!"):(e.hljs.initLineNumbersOnLoad=t,e.hljs.lineNumbersBlock=n)}(window); \ No newline at end of file +!function(e){"use strict";function t(){"complete"===document.readyState?n():e.addEventListener("DOMContentLoaded",n)}function n(){try{var e=document.querySelectorAll("code.hljs");for(var t in e)e.hasOwnProperty(t)&&r(e[t])}catch(n){console.error("LineNumbers error: ",n)}}function r(e){if("object"==typeof e){var t=e.parentNode,n=o(t.textContent);if(n>1){for(var r="",c=0;n>c;c++)r+=c+1+"\n";var l=document.createElement("code");l.className="hljs hljs-line-numbers",l.style["float"]="left",l.textContent=r,t.insertBefore(l,e)}}}function o(e){if(0===e.length)return 0;var t=/\r\n|\r|\n/g,n=e.match(t);return n=n?n.length:0,e[e.length-1].match(t)||(n+=1),n}"undefined"==typeof e.hljs?console.error("highlight.js not detected!"):(e.hljs.initLineNumbersOnLoad=t,e.hljs.lineNumbersBlock=r)}(window); \ No newline at end of file diff --git a/src/highlightjs-line-numbers.js b/src/highlightjs-line-numbers.js index 2c7fc2f..f68f8f2 100644 --- a/src/highlightjs-line-numbers.js +++ b/src/highlightjs-line-numbers.js @@ -9,19 +9,25 @@ } function initLineNumbersOnLoad () { - w.addEventListener('load', function () { - try { - var blocks = document.querySelectorAll('code.hljs'); - - for (var i in blocks) { - if (blocks.hasOwnProperty(i)) { - lineNumbersBlock(blocks[i]); - } + if (document.readyState === 'complete') { + documentReady(); + } else { + w.addEventListener('DOMContentLoaded', documentReady); + } + } + + function documentReady () { + try { + var blocks = document.querySelectorAll('code.hljs'); + + for (var i in blocks) { + if (blocks.hasOwnProperty(i)) { + lineNumbersBlock(blocks[i]); } - } catch (e) { - console.error('LineNumbers error: ', e); } - }); + } catch (e) { + console.error('LineNumbers error: ', e); + } } function lineNumbersBlock (element) {