@mohasinac/appkit 3.5.4 → 3.5.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -18,35 +18,39 @@ function isSafeHref(href) {
|
|
|
18
18
|
// relative), or "#" — reject everything else including javascript:.
|
|
19
19
|
return /^(https?:\/\/|mailto:|\/(?!\/)|#)/i.test(trimmed);
|
|
20
20
|
}
|
|
21
|
+
function sanitizeAttribute(el, attr) {
|
|
22
|
+
const isSafeAnchorHref = el.tagName === "A" && attr.name.toLowerCase() === "href";
|
|
23
|
+
if (!isSafeAnchorHref) {
|
|
24
|
+
el.removeAttribute(attr.name);
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
if (!isSafeHref(attr.value))
|
|
28
|
+
el.setAttribute("href", "#");
|
|
29
|
+
}
|
|
30
|
+
function sanitizeElement(root, el) {
|
|
31
|
+
if (!ALLOWED_TAGS.has(el.tagName)) {
|
|
32
|
+
root.removeChild(el);
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
Array.from(el.attributes).forEach((attr) => sanitizeAttribute(el, attr));
|
|
36
|
+
sanitizeChildren(el);
|
|
37
|
+
}
|
|
38
|
+
function sanitizeChildren(root) {
|
|
39
|
+
Array.from(root.childNodes).forEach((child) => {
|
|
40
|
+
if (child.nodeType === Node.ELEMENT_NODE) {
|
|
41
|
+
sanitizeElement(root, child);
|
|
42
|
+
}
|
|
43
|
+
else if (child.nodeType !== Node.TEXT_NODE) {
|
|
44
|
+
root.removeChild(child);
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
}
|
|
21
48
|
function sanitizeRichTextHtml(html) {
|
|
22
49
|
if (typeof document === "undefined" || !html)
|
|
23
50
|
return "";
|
|
24
51
|
const template = document.createElement("template");
|
|
25
52
|
template.innerHTML = html;
|
|
26
|
-
|
|
27
|
-
Array.from(root.childNodes).forEach((child) => {
|
|
28
|
-
if (child.nodeType === Node.ELEMENT_NODE) {
|
|
29
|
-
const el = child;
|
|
30
|
-
if (!ALLOWED_TAGS.has(el.tagName)) {
|
|
31
|
-
root.removeChild(el);
|
|
32
|
-
return;
|
|
33
|
-
}
|
|
34
|
-
Array.from(el.attributes).forEach((attr) => {
|
|
35
|
-
if (el.tagName === "A" && attr.name.toLowerCase() === "href") {
|
|
36
|
-
if (!isSafeHref(attr.value))
|
|
37
|
-
el.setAttribute("href", "#");
|
|
38
|
-
return;
|
|
39
|
-
}
|
|
40
|
-
el.removeAttribute(attr.name);
|
|
41
|
-
});
|
|
42
|
-
walk(el);
|
|
43
|
-
}
|
|
44
|
-
else if (child.nodeType !== Node.TEXT_NODE) {
|
|
45
|
-
root.removeChild(child);
|
|
46
|
-
}
|
|
47
|
-
});
|
|
48
|
-
};
|
|
49
|
-
walk(template.content);
|
|
53
|
+
sanitizeChildren(template.content);
|
|
50
54
|
return template.innerHTML;
|
|
51
55
|
}
|
|
52
56
|
export function RichTextEditor({ value, onChange, disabled = false, className = "", minHeightClassName = "min-h-[180px]", placeholder = "Write formatted content...", }) {
|