Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(380)

Side by Side Diff: lib/src/prism/plugins/line-highlight/prism-line-highlight.js

Issue 1418513006: update elements and fix some bugs (Closed) Base URL: git@github.com:dart-lang/polymer_elements.git@master
Patch Set: code review updates Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 (function(){ 1 (function(){
2 2
3 if(!window.Prism) { 3 if (typeof self === 'undefined' || !self.Prism || !self.document || !document.qu erySelector) {
4 return; 4 return;
5 } 5 }
6 6
7 function $$(expr, con) { 7 function $$(expr, con) {
8 return Array.prototype.slice.call((con || document).querySelectorAll(exp r)); 8 return Array.prototype.slice.call((con || document).querySelectorAll(exp r));
9 } 9 }
10 10
11 function hasClass(element, className) { 11 function hasClass(element, className) {
12 className = " " + className + " "; 12 className = " " + className + " ";
13 return (" " + element.className + " ").replace(/[\n\t]/g, " ").indexOf(classNa me) > -1 13 return (" " + element.className + " ").replace(/[\n\t]/g, " ").indexOf(classNa me) > -1
14 } 14 }
15 15
16 var CRLF = crlf = /\r?\n|\r/g; 16 // Some browsers round the line-height, others don't.
17 17 // We need to test for it to position the elements properly.
18 var isLineHeightRounded = (function() {
19 » var res;
20 » return function() {
21 » » if(typeof res === 'undefined') {
22 » » » var d = document.createElement('div');
23 » » » d.style.fontSize = '13px';
24 » » » d.style.lineHeight = '1.5';
25 » » » d.style.padding = 0;
26 » » » d.style.border = 0;
27 » » » d.innerHTML = '&nbsp;<br />&nbsp;';
28 » » » document.body.appendChild(d);
29 » » » // Browsers that round the line-height should have offse tHeight === 38
30 » » » // The others should have 39.
31 » » » res = d.offsetHeight === 38;
32 » » » document.body.removeChild(d);
33 » » }
34 » » return res;
35 » }
36 }());
37
18 function highlightLines(pre, lines, classes) { 38 function highlightLines(pre, lines, classes) {
19 var ranges = lines.replace(/\s+/g, '').split(','), 39 var ranges = lines.replace(/\s+/g, '').split(','),
20 offset = +pre.getAttribute('data-line-offset') || 0; 40 offset = +pre.getAttribute('data-line-offset') || 0;
21 » 41
22 » var lineHeight = parseFloat(getComputedStyle(pre).lineHeight); 42 » var parseMethod = isLineHeightRounded() ? parseInt : parseFloat;
43 » var lineHeight = parseMethod(getComputedStyle(pre).lineHeight);
23 44
24 for (var i=0, range; range = ranges[i++];) { 45 for (var i=0, range; range = ranges[i++];) {
25 range = range.split('-'); 46 range = range.split('-');
26 47
27 var start = +range[0], 48 var start = +range[0],
28 end = +range[1] || start; 49 end = +range[1] || start;
29 50
30 var line = document.createElement('div'); 51 var line = document.createElement('div');
31 52
32 line.textContent = Array(end - start + 2).join(' \n'); 53 line.textContent = Array(end - start + 2).join(' \n');
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
78 pre.setAttribute('data-line', ''); 99 pre.setAttribute('data-line', '');
79 } 100 }
80 101
81 highlightLines(pre, range, 'temporary '); 102 highlightLines(pre, range, 'temporary ');
82 103
83 document.querySelector('.temporary.line-highlight').scrollIntoView(); 104 document.querySelector('.temporary.line-highlight').scrollIntoView();
84 } 105 }
85 106
86 var fakeTimer = 0; // Hack to limit the number of times applyHash() runs 107 var fakeTimer = 0; // Hack to limit the number of times applyHash() runs
87 108
88 Prism.hooks.add('after-highlight', function(env) { 109 Prism.hooks.add('complete', function(env) {
89 var pre = env.element.parentNode; 110 var pre = env.element.parentNode;
90 var lines = pre && pre.getAttribute('data-line'); 111 var lines = pre && pre.getAttribute('data-line');
91 112
92 if (!pre || !lines || !/pre/i.test(pre.nodeName)) { 113 if (!pre || !lines || !/pre/i.test(pre.nodeName)) {
93 return; 114 return;
94 } 115 }
95 116
96 clearTimeout(fakeTimer); 117 clearTimeout(fakeTimer);
97 118
98 $$('.line-highlight', pre).forEach(function (line) { 119 $$('.line-highlight', pre).forEach(function (line) {
99 line.parentNode.removeChild(line); 120 line.parentNode.removeChild(line);
100 }); 121 });
101 122
102 highlightLines(pre, lines); 123 highlightLines(pre, lines);
103 124
104 fakeTimer = setTimeout(applyHash, 1); 125 fakeTimer = setTimeout(applyHash, 1);
105 }); 126 });
106 127
107 addEventListener('hashchange', applyHash); 128 addEventListener('hashchange', applyHash);
108 129
109 })(); 130 })();
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698