-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrender_table.js
More file actions
48 lines (40 loc) · 1.14 KB
/
Copy pathrender_table.js
File metadata and controls
48 lines (40 loc) · 1.14 KB
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
function renderResultTable(data) {
const result = document.getElementById('result');
result.innerHTML = '';
const heading = document.createElement('h2');
heading.textContent = 'Submitted Data';
result.appendChild(heading);
const table = document.createElement('table');
table.setAttribute('border', '1');
table.setAttribute('cellpadding', '6');
table.setAttribute('cellspacing', '0');
const tbody = document.createElement('tbody');
for (const key in data) {
const tr = document.createElement('tr');
const th = document.createElement('th');
th.textContent = key;
const td = document.createElement('td');
if (key === 'Address') {
td.innerHTML = escapeHtml(data[key]).replace(/\n/g, '<br>');
} else {
td.textContent = data[key];
}
tr.appendChild(th);
tr.appendChild(td);
tbody.appendChild(tr);
}
table.appendChild(tbody);
result.appendChild(table);
}
// Prevent HTML injection
function escapeHtml(text) {
return text.replace(/[&<>"']/g, function (m) {
return {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": ''',
}[m];
});
}