Skip to content

Commit aa83994

Browse files
committed
Render code snippet as table
This prevents line numbers from being selected.
1 parent 9521b67 commit aa83994

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

resources/css/pmd-tester.css

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,16 @@ code {
1010
font-size: 13pt;
1111
white-space: pre;
1212
}
13-
code.highlight {
13+
code.highlight, tr.highlight {
1414
background-color: yellow;
1515
}
16+
table.code-snippet > tbody > tr > td {
17+
padding: 0px;
18+
border: none;
19+
}
20+
table.code-snippet > tbody > tr > td.line-number > code:before {
21+
content: attr(data-line-number);
22+
}
1623
a {
1724
text-decoration: none;
1825
}

resources/js/code-snippets.js

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,24 +47,41 @@
4747
deleteCount = lines.length - (2 * contextLines) + 1;
4848
lines.splice(2 * contextLines - 1, deleteCount); // delete lines after
4949

50+
let table = document.createElement('table');
51+
table.classList.add('code-snippet');
52+
let tableBody = document.createElement('tbody');
53+
table.appendChild(tableBody);
5054
// now we have just the lines which will be displayed
5155
lines.forEach(line => {
5256
start++;
53-
let lineElt = document.createElement("code");
57+
let tableRow = document.createElement('tr');
5458
if (start === violationLineNumber) {
55-
lineElt.classList.add("highlight");
59+
tableRow.classList.add("highlight");
5660
}
61+
62+
let lineNumberColumn = document.createElement('td');
63+
lineNumberColumn.classList.add('line-number');
64+
tableRow.appendChild(lineNumberColumn);
65+
let lineNumberElement = document.createElement('code');
66+
lineNumberColumn.appendChild(lineNumberElement);
67+
lineNumberElement.setAttribute('data-line-number', formatLineNumber(start));
68+
69+
let codeColumn = document.createElement('td');
70+
tableRow.appendChild(codeColumn);
71+
let codeElement = document.createElement("code");
72+
codeColumn.appendChild(codeElement);
5773
// createTextNode escapes special chars
58-
lineElt.appendChild(document.createTextNode(formatLineNumber(start) + nbsp + line));
59-
lineElt.appendChild(document.createElement("br"));
74+
codeElement.appendChild(document.createTextNode(line));
6075

61-
container.appendChild(lineElt); // append to the container
76+
tableBody.appendChild(tableRow); // append row to the table
6277
});
78+
container.appendChild(table);
6379
});
64-
oReq.open("GET", requestUrl);
65-
oReq.send();
6680

6781
container.innerHTML = "<samp>fetching...</samp>";
82+
83+
oReq.open("GET", requestUrl);
84+
oReq.send();
6885
}
6986

7087
window.pmd_code_snippets = {

0 commit comments

Comments
 (0)