-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
71 lines (58 loc) · 1.94 KB
/
script.js
File metadata and controls
71 lines (58 loc) · 1.94 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Theme switcher functionality
function setupThemeToggle() {
const toggleSwitch = document.querySelector('#checkbox');
const currentTheme = localStorage.getItem('theme');
if (currentTheme) {
document.documentElement.setAttribute('data-theme', currentTheme);
if (currentTheme === 'dark') {
toggleSwitch.checked = true;
}
}
toggleSwitch.addEventListener('change', switchTheme);
function switchTheme(e) {
if (e.target.checked) {
document.documentElement.setAttribute('data-theme', 'dark');
localStorage.setItem('theme', 'dark');
} else {
document.documentElement.setAttribute('data-theme', 'light');
localStorage.setItem('theme', 'light');
}
}
}
// XML syntax highlighting
function highlightXML(text) {
// Replace tags
text = text.replace(/(<\/?)(\w+)(\s|>)/g, '$1<span class="xml-tag">$2</span>$3');
// Replace attributes
text = text.replace(/(\s)(\w+)(\s*=\s*)/g, '$1<span class="xml-attribute">$2</span>$3');
// Replace values
text = text.replace(/(".*?")/g, '<span class="xml-value">$1</span>');
// Replace comments
text = text.replace(/(<!--.*?-->)/g, '<span class="xml-comment">$1</span>');
return text;
}
// Apply syntax highlighting to the XML output
function updateOutput() {
// ... existing code ...
// Apply syntax highlighting before displaying
if (outputElement) {
let formattedXML = formatXML(xmlString);
formattedXML = escapeHTML(formattedXML);
formattedXML = highlightXML(formattedXML);
outputElement.innerHTML = formattedXML;
}
}
// Helper function to escape HTML
function escapeHTML(text) {
return text
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''');
}
// Initialize everything when the DOM is loaded
document.addEventListener('DOMContentLoaded', function() {
// ... existing initialization code ...
setupThemeToggle();
});