260810_03_ENH: 에디터 버전 맞춤, ssr에 대한 조치
This commit is contained in:
@@ -8,6 +8,7 @@ import { resolveUploadUrl } from './types'
|
||||
import { compressImage } from './utils/imageCompression'
|
||||
import { useDebouncedChange } from './hooks/useDebouncedChange'
|
||||
import { sanitizeHtmlForPreview } from './utils/sanitizeHtml'
|
||||
import { preserveEmptyParagraphs } from './utils/emptyParagraph'
|
||||
import { useTextareaInsert } from './hooks/useTextareaInsert'
|
||||
|
||||
type HtmlOnlyProps = Omit<KriEditorProps, 'mode'> & { mode: 'html' }
|
||||
@@ -121,7 +122,10 @@ export function KriEditorHtmlOnly({
|
||||
}
|
||||
}
|
||||
|
||||
const previewHtml = React.useMemo(() => sanitizeHtmlForPreview(html), [html])
|
||||
const previewHtml = React.useMemo(
|
||||
() => sanitizeHtmlForPreview(preserveEmptyParagraphs(html)),
|
||||
[html],
|
||||
)
|
||||
|
||||
return (
|
||||
<div className={cn('rounded-md border bg-background', className)}>
|
||||
|
||||
@@ -1,12 +1,21 @@
|
||||
"use client";
|
||||
|
||||
import DOMPurify from "dompurify";
|
||||
import DOMPurify, { type DOMPurify as DOMPurifyInstance } from "dompurify";
|
||||
|
||||
export type SanitizeHtmlOptions = {
|
||||
tags?: string[];
|
||||
attributes?: string[];
|
||||
};
|
||||
|
||||
// Next.js SSR(Node) 환경에서는 dompurify가 window에 바인딩된 인스턴스가 아니라
|
||||
// (window: Window) => DOMPurify 형태의 factory만 노출한다. window가 없으면
|
||||
// factory를 호출할 방법이 없으므로, 여기서 안전하게 null로 폴백한다.
|
||||
function resolvePurifier(): DOMPurifyInstance | null {
|
||||
if (typeof window === "undefined") return null;
|
||||
if (typeof DOMPurify.sanitize === "function") return DOMPurify;
|
||||
return DOMPurify(window);
|
||||
}
|
||||
|
||||
export function sanitizeHtml(
|
||||
input: string,
|
||||
options?: SanitizeHtmlOptions,
|
||||
@@ -15,7 +24,16 @@ export function sanitizeHtml(
|
||||
return "";
|
||||
}
|
||||
|
||||
return DOMPurify.sanitize(
|
||||
const purifier = resolvePurifier();
|
||||
|
||||
if (!purifier) {
|
||||
// DOM이 없어 실제 sanitize를 수행할 수 없는 환경(SSR 등)에서는
|
||||
// 검증되지 않은 HTML을 그대로 노출하지 않도록 태그를 모두 제거한 텍스트만 반환한다.
|
||||
// 브라우저에서 하이드레이션된 이후 재계산될 때 실제 sanitize 결과로 대체된다.
|
||||
return input.replace(/<[^>]*>/g, "");
|
||||
}
|
||||
|
||||
return purifier.sanitize(
|
||||
input,
|
||||
{
|
||||
USE_PROFILES: {
|
||||
|
||||
Reference in New Issue
Block a user