|
26 | 26 | return prefix + number;
|
27 | 27 | }
|
28 | 28 |
|
29 |
| - function fetchSnippet(document, container, url, line, weburl) { |
| 29 | + function fetchSnippet(document, container, url, violationLineNumber, weburl) { |
30 | 30 | var weburl, requestUrl, oReq;
|
31 | 31 |
|
32 | 32 | requestUrl = url.replace(/github.com/, "raw.githubusercontent.com");
|
33 | 33 | requestUrl = requestUrl.replace(/(tree|blob)\//, "");
|
34 | 34 |
|
35 | 35 | oReq = new XMLHttpRequest();
|
36 | 36 | oReq.addEventListener("load", function() {
|
37 |
| - let lines, start, deleteCount; |
| 37 | + let lines, start, deleteCount, lineSeparator; |
38 | 38 |
|
39 | 39 | // we'll append stuff in the loop below
|
40 | 40 | container.innerHTML = '<p><a href="' + weburl + '" target="_blank" rel="noopener noreferrer">' + weburl + '</a></p>';
|
41 | 41 |
|
42 |
| - lines = this.responseText.split(/\r\n|\n/); |
43 |
| - start = line - contextLines; |
| 42 | + if (this.responseText.indexOf('\r\n') >= 0) { |
| 43 | + lineSeparator = '\r\n'; |
| 44 | + } else { |
| 45 | + lineSeparator = '\n'; |
| 46 | + } |
| 47 | + lines = this.responseText.split(lineSeparator); |
| 48 | + start = violationLineNumber - contextLines; |
44 | 49 | if (start > 0) {
|
45 | 50 | lines.splice(0, start); // remove lines before
|
46 | 51 | }
|
47 | 52 | deleteCount = lines.length - (2 * contextLines) + 1;
|
48 | 53 | lines.splice(2 * contextLines - 1, deleteCount); // delete lines after
|
49 | 54 |
|
| 55 | + let table = document.createElement('table'); |
| 56 | + table.classList.add('code-snippet'); |
| 57 | + let tableBody = document.createElement('tbody'); |
| 58 | + table.appendChild(tableBody); |
50 | 59 | // now we have just the lines which will be displayed
|
51 | 60 | lines.forEach(line => {
|
52 | 61 | start++;
|
53 |
| - let lineElt = document.createElement("code"); |
54 |
| - if (start === line) { |
55 |
| - lineElt.classList.add("highlight"); |
| 62 | + let tableRow = document.createElement('tr'); |
| 63 | + if (start === violationLineNumber) { |
| 64 | + tableRow.classList.add("highlight"); |
56 | 65 | }
|
| 66 | + |
| 67 | + let lineNumberColumn = document.createElement('td'); |
| 68 | + lineNumberColumn.classList.add('line-number'); |
| 69 | + tableRow.appendChild(lineNumberColumn); |
| 70 | + let lineNumberElement = document.createElement('code'); |
| 71 | + lineNumberColumn.appendChild(lineNumberElement); |
| 72 | + lineNumberElement.setAttribute('data-line-number', formatLineNumber(start)); |
| 73 | + |
| 74 | + let codeColumn = document.createElement('td'); |
| 75 | + tableRow.appendChild(codeColumn); |
| 76 | + let codeElement = document.createElement("code"); |
| 77 | + codeColumn.appendChild(codeElement); |
57 | 78 | // createTextNode escapes special chars
|
58 |
| - lineElt.appendChild(document.createTextNode(formatLineNumber(start) + nbsp + line)); |
59 |
| - lineElt.appendChild(document.createElement("br")); |
| 79 | + codeElement.appendChild(document.createTextNode(line)); |
60 | 80 |
|
61 |
| - container.appendChild(lineElt); // append to the container |
| 81 | + tableBody.appendChild(tableRow); // append row to the table |
62 | 82 | });
|
| 83 | + container.appendChild(table); |
| 84 | + |
| 85 | + if (navigator.clipboard) { |
| 86 | + let copyButton = document.createElement('button'); |
| 87 | + copyButton.classList.add('btn-clipboard'); |
| 88 | + copyButton.setAttribute('title', 'Copy to clipboard'); |
| 89 | + copyButton.appendChild(document.createTextNode('copy')); |
| 90 | + copyButton.onclick = function() { |
| 91 | + navigator.clipboard.writeText(lines.join(lineSeparator)); |
| 92 | + } |
| 93 | + container.appendChild(copyButton); |
| 94 | + } |
63 | 95 | });
|
64 |
| - oReq.open("GET", requestUrl); |
65 |
| - oReq.send(); |
66 | 96 |
|
67 | 97 | container.innerHTML = "<samp>fetching...</samp>";
|
| 98 | + |
| 99 | + oReq.open("GET", requestUrl); |
| 100 | + oReq.send(); |
68 | 101 | }
|
69 | 102 |
|
70 | 103 | window.pmd_code_snippets = {
|
|
0 commit comments