-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-escape.js
More file actions
43 lines (34 loc) · 1.2 KB
/
test-escape.js
File metadata and controls
43 lines (34 loc) · 1.2 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
function escapeInvalidHtmlTags(content) {
const validTagPattern =
/<\/?(?:font|br|b|i|u|o|sub|sup|s|table|tr|td|hr|vr|img)\b[^>]*\/?>/gi;
const validTags = [];
let match;
while ((match = validTagPattern.exec(content)) !== null) {
validTags.push({
start: match.index,
end: match.index + match[0].length,
tag: match[0],
});
}
console.log('Valid tags found:', validTags);
let result = '';
let lastEnd = 0;
for (const tag of validTags) {
const before = content.slice(lastEnd, tag.start);
result += before.replace(/</g, '<').replace(/>/g, '>');
result += content.slice(tag.start, tag.end);
lastEnd = tag.end;
}
const remaining = content.slice(lastEnd);
result += remaining.replace(/</g, '<').replace(/>/g, '>');
return result;
}
const input = '<br /><font point-size="34">Test</font>';
console.log('Input:', input);
console.log('Output:', escapeInvalidHtmlTags(input));
const input2 = '<slot name="header"/>';
console.log('\nInput2:', input2);
console.log('Output2:', escapeInvalidHtmlTags(input2));
const input3 = '<slot name="header"<br />/>';
console.log('\nInput3:', input3);
console.log('Output3:', escapeInvalidHtmlTags(input3));