Skip to content

Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.

refactor: improve DOM injection safety in theme switcher and draggable list (#6732)

白熱f3212043d8dc
diff --git a/packages/design/src/components/draggable-list/DraggableList.tsx b/packages/design/src/components/draggable-list/DraggableList.tsx
index 5df4cbe7f9..9d0d69222f 100644
--- a/packages/design/src/components/draggable-list/DraggableList.tsx
+++ b/packages/design/src/components/draggable-list/DraggableList.tsx
@@ -68,12 +68,13 @@ export function DraggableList<T = any>(props: IDraggableListProps<T>) {
const displayListRef = useRef(list);
const [draggingId, setDraggingId] = useState<string | null>(null);
const [dragOverId, setDragOverId] = useState<string | null>(null);
- const [ghostMarkup, setGhostMarkup] = useState<string | null>(null);
+ const [ghostElement, setGhostElement] = useState<HTMLElement | null>(null);
const [ghostPosition, setGhostPosition] = useState<{ x: number; y: number } | null>(null);
const [canUsePortal, setCanUsePortal] = useState(false);
const pointerIdRef = useRef<number | null>(null);
const dragPointerOffsetRef = useRef({ x: 0, y: 0 });
const dragItemSizeRef = useRef({ width: 0, height: 0 });
+ const ghostContainerRef = useRef<HTMLDivElement | null>(null);
const pressedHandleIdRef = useRef<string | null>(null);
const dragSourceIdRef = useRef<string | null>(null);
@@ -90,6 +91,18 @@ export function DraggableList<T = any>(props: IDraggableListProps<T>) {
setCanUsePortal(typeof document !== 'undefined');
}, []);
+ useEffect(() => {
+ const container = ghostContainerRef.current;
+ if (!container) {
+ return;
+ }
+
+ container.replaceChildren();
+ if (ghostElement) {
+ container.appendChild(ghostElement);
+ }
+ }, [ghostElement]);
+
useEffect(() => {
if (!draggingId) {
return;
@@ -145,7 +158,7 @@ export function DraggableList<T = any>(props: IDraggableListProps<T>) {
dragSourceIdRef.current = null;
dragStartIndexRef.current = -1;
setDragOverId(null);
- setGhostMarkup(null);
+ setGhostElement(null);
setGhostPosition(null);
setDraggingId(null);
};
@@ -225,7 +238,7 @@ export function DraggableList<T = any>(props: IDraggableListProps<T>) {
height: rect.height,
};
setGhostPosition({ x: rect.left, y: rect.top });
- setGhostMarkup((e.currentTarget as HTMLDivElement).innerHTML);
+ setGhostElement((e.currentTarget as HTMLDivElement).cloneNode(true) as HTMLElement);
pointerIdRef.current = e.pointerId;
dragSourceIdRef.current = itemId;
@@ -270,8 +283,8 @@ export function DraggableList<T = any>(props: IDraggableListProps<T>) {
opacity: 0.95,
}}
>
- {ghostMarkup
- ? <div dangerouslySetInnerHTML={{ __html: ghostMarkup }} />
+ {ghostElement
+ ? <div ref={ghostContainerRef} />
: itemRender(draggingItem, displayList.findIndex((item) => getItemId(item) === getItemId(draggingItem)))}
</div>
), document.body
diff --git a/packages/design/src/components/draggable-list/__tests__/index.spec.tsx b/packages/design/src/components/draggable-list/__tests__/index.spec.tsx
index 8b9320aa07..0178d73e24 100644
--- a/packages/design/src/components/draggable-list/__tests__/index.spec.tsx
+++ b/packages/design/src/components/draggable-list/__tests__/index.spec.tsx
@@ -262,21 +262,21 @@ describe('DraggableList', () => {
expect(onListChange).toHaveBeenCalledTimes(1);
});
- it('should render ghost with itemRender fallback when innerHTML is empty', async () => {
+ it('should render ghost from cloned item dom', async () => {
const onListChange = vi.fn();
- const itemRender = vi.fn(() => null);
const { container } = render(
<DraggableList<IItem>
list={BASE_LIST.slice(0, 1)}
idKey="id"
onListChange={onListChange}
- itemRender={itemRender}
+ itemRender={(item) => <div className="drag-content">{item.label}</div>}
/>
);
const itemA = container.querySelector('[data-draggable-list-item-id="a"]') as HTMLDivElement;
mockRect(itemA);
+ const cloneNodeSpy = vi.spyOn(itemA, 'cloneNode');
Object.defineProperty(itemA, 'setPointerCapture', {
configurable: true,
value: vi.fn(),
@@ -289,7 +289,8 @@ describe('DraggableList', () => {
});
await waitFor(() => {
- expect(itemRender.mock.calls.length).toBeGreaterThanOrEqual(3);
+ expect(cloneNodeSpy).toHaveBeenCalledWith(true);
+ expect(container.querySelectorAll('.drag-content')).toHaveLength(2);
});
fireEvent.pointerUp(window, {
diff --git a/packages/network/src/facade/f-univer.ts b/packages/network/src/facade/f-univer.ts
index 477b433709..dc0d585b45 100644
--- a/packages/network/src/facade/f-univer.ts
+++ b/packages/network/src/facade/f-univer.ts
@@ -36,7 +36,7 @@ interface IFUniverNetworkMixin {
* @example
* ```typescript
* // Replace the URL with the address of your own WebSocket service
- * const ws = univerAPI.createSocket('ws://47.100.177.253:8449/ws');
+ * const ws = univerAPI.createSocket('wss://47.100.177.253:8449/ws');
*
* ws.open$.subscribe(() => {
* console.log('websocket opened');
diff --git a/packages/ui/src/services/theme-switcher/theme-switcher.service.ts b/packages/ui/src/services/theme-switcher/theme-switcher.service.ts
index bfb4e4923b..9bbdd78f49 100644
--- a/packages/ui/src/services/theme-switcher/theme-switcher.service.ts
+++ b/packages/ui/src/services/theme-switcher/theme-switcher.service.ts
@@ -50,7 +50,7 @@ export class ThemeSwitcherService extends Disposable {
const styleElement = document.createElement('style');
styleElement.setAttribute('id', this._styleSheetId);
- styleElement.innerHTML = `:root {\n${cssVariables}\n}`;
+ styleElement.textContent = `:root {\n${cssVariables}\n}`;
document.head.appendChild(styleElement);
}