mirror of
https://github.com/suitenumerique/docs.git
synced 2026-04-25 17:15:01 +02:00
fixup! 🐛(frontend) sanitize pasted toolbar links
This commit is contained in:
@@ -93,8 +93,6 @@ interface BlockNoteEditorProps {
|
|||||||
* BlockNote copies links in Markdown autolink format; pasting into the link
|
* BlockNote copies links in Markdown autolink format; pasting into the link
|
||||||
* toolbar input keeps the brackets, producing broken hrefs.
|
* toolbar input keeps the brackets, producing broken hrefs.
|
||||||
*/
|
*/
|
||||||
const stripAngleBrackets = (text: string): string =>
|
|
||||||
text.replace(/^<(.+)>$/, '$1');
|
|
||||||
|
|
||||||
const handlePasteUrlBrackets = (e: React.ClipboardEvent<HTMLDivElement>) => {
|
const handlePasteUrlBrackets = (e: React.ClipboardEvent<HTMLDivElement>) => {
|
||||||
const target = e.target;
|
const target = e.target;
|
||||||
@@ -105,18 +103,14 @@ const handlePasteUrlBrackets = (e: React.ClipboardEvent<HTMLDivElement>) => {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const text = e.clipboardData?.getData('text/plain') ?? '';
|
const text = e.clipboardData?.getData('text/plain') ?? '';
|
||||||
const cleaned = stripAngleBrackets(text.trim());
|
const cleaned = text.replace(/^\s*<([^<>]+)>\s*$/, '$1');
|
||||||
if (cleaned === text) {
|
if (cleaned === text) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
// Use the native value setter (input/textarea) so React-controlled fields pick up the pasted value change.
|
const start = target.selectionStart ?? target.value.length;
|
||||||
const proto =
|
const end = target.selectionEnd ?? target.value.length;
|
||||||
target instanceof HTMLInputElement
|
target.setRangeText(cleaned, start, end, 'end');
|
||||||
? HTMLInputElement.prototype
|
|
||||||
: HTMLTextAreaElement.prototype;
|
|
||||||
const setter = Object.getOwnPropertyDescriptor(proto, 'value')?.set;
|
|
||||||
setter?.call(target, cleaned);
|
|
||||||
target.dispatchEvent(new Event('input', { bubbles: true }));
|
target.dispatchEvent(new Event('input', { bubbles: true }));
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user