Hash :
f15a012b
Author :
Date :
2012-07-16T19:19:22
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
(function(){
if(!window.Prism
|| !window.addEventListener
|| !window.getComputedStyle
|| !document.querySelectorAll) {
return;
}
function $$(expr, con) {
return Array.prototype.slice.call((con || document).querySelectorAll(expr));
}
var CRLF = crlf = /\r?\n|\r/g,
preTag = /pre/i;
function highlightLines(pre, lines, classes) {
var ranges = lines.replace(/\s+/g, '').split(','),
offset = +pre.getAttribute('data-line-offset') || 0;
var cs = getComputedStyle(pre),
lineHeight = parseFloat(cs.lineHeight),
firstChild = pre.firstChild;
for (var i=0, range; range = ranges[i++];) {
range = range.split('-');
var start = +range[0],
end = +range[1] || start;
var line = document.createElement('div');
line.className = (classes || '') + ' line-highlight';
line.setAttribute('data-start', start);
if(end > start) {
line.setAttribute('data-end', end);
}
line.style.height = (end - start + 1) * lineHeight + 'px';
line.style.top = (start - offset - 1) * lineHeight + 'px';
pre.insertBefore(line, firstChild);
}
}
function applyHash() {
var hash = location.hash.slice(1);
// Remove pre-existing temporary lines
$$('.temporary.line-highlight').forEach(function (line) {
line.parentNode.removeChild(line);
});
var range = (hash.match(/\.([\d,-]+)$/) || [,''])[1];
if(!range || document.getElementById(hash)) {
return;
}
var id = hash.slice(0, hash.lastIndexOf('.')),
pre = document.getElementById(id);
if(!pre) {
return;
}
if(!pre.hasAttribute('data-line')) {
pre.setAttribute('data-line', '');
}
highlightLines(pre, range, 'temporary ');
document.querySelector('.temporary.line-highlight').scrollIntoView();
}
var fakeTimer = 0; // Hack to limit the number of times applyHash() runs
Prism.hooks.add('after-highlight', function(env) {
clearTimeout(fakeTimer);
var pre = preTag.test(env.element.nodeName)? env.element
: preTag.test(env.element.parentNode.nodeName)? env.element.parentNode
: null,
lines = pre.getAttribute('data-line');
if (!pre || !lines) {
return;
}
$$('.line-highlight', pre).forEach(function (line) {
line.parentNode.removeChild(line);
});
highlightLines(pre, lines);
fakeTimer = setTimeout(applyHash, 1);
});
addEventListener('hashchange', applyHash);
})();