Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:bug: Guard undefined dereferences in AV virtual scroll trim and virtualData collection https://github.com/siyuan-note/siyuan/issues/18213

Danielb5261f88ce9b
diff --git a/app/src/protyle/render/av/gallery/render.ts b/app/src/protyle/render/av/gallery/render.ts
index 78614318d7..9e9aa6f809 100644
--- a/app/src/protyle/render/av/gallery/render.ts
+++ b/app/src/protyle/render/av/gallery/render.ts
@@ -192,7 +192,13 @@ export const renderGallery = async (options: {
if (!item.querySelector(".av__gallery-item") || options.blockElement.getAttribute(Constants.ATTRIBUTE_V_SCROLL) !== "true") {
return;
}
- const firstItemIndex = parseInt(item.querySelector(".av__gallery-item:not([data-type=ghost])").getAttribute("data-index"));
+ // 守卫只保证至少 1 个 .av__gallery-item,但首行索引用 :not([data-type=ghost]) 过滤。
+ // body 内全是 ghost 占位行(插入动画进行中)时查询返回 null,需跳过避免解引用 null.getAttribute
+ const firstItem = item.querySelector(".av__gallery-item:not([data-type=ghost])") as HTMLElement;
+ if (!firstItem) {
+ return;
+ }
+ const firstItemIndex = parseInt(firstItem.getAttribute("data-index"));
virtualData[item.getAttribute("data-group-id") || "all"] = getBodyVirtualData(item, ".av__gallery-add", firstItemIndex);
});
const resetData = {
diff --git a/app/src/protyle/render/av/kanban/render.ts b/app/src/protyle/render/av/kanban/render.ts
index 47b745487c..4ec9797bf9 100644
--- a/app/src/protyle/render/av/kanban/render.ts
+++ b/app/src/protyle/render/av/kanban/render.ts
@@ -95,7 +95,13 @@ export const renderKanban = async (options: {
if (!item.querySelector(".av__gallery-item") || options.blockElement.getAttribute(Constants.ATTRIBUTE_V_SCROLL) !== "true") {
return;
}
- const firstItemIndex = parseInt(item.querySelector(".av__gallery-item:not([data-type=ghost])").getAttribute("data-index"));
+ // 守卫只保证至少 1 个 .av__gallery-item,但首行索引用 :not([data-type=ghost]) 过滤。
+ // body 内全是 ghost 占位行(插入动画进行中)时查询返回 null,需跳过避免解引用 null.getAttribute
+ const firstItem = item.querySelector(".av__gallery-item:not([data-type=ghost])") as HTMLElement;
+ if (!firstItem) {
+ return;
+ }
+ const firstItemIndex = parseInt(firstItem.getAttribute("data-index"));
virtualData[item.getAttribute("data-group-id")] = getBodyVirtualData(item, ".av__gallery-add", firstItemIndex);
});
const resetData = {
diff --git a/app/src/protyle/render/av/render.ts b/app/src/protyle/render/av/render.ts
index 8e2c6f201e..f9bc8a2f61 100644
--- a/app/src/protyle/render/av/render.ts
+++ b/app/src/protyle/render/av/render.ts
@@ -459,11 +459,14 @@ export const avRender = async (element: Element, protyle: IProtyle, cb?: (data:
const virtualData: { [key: string]: IAVVirtualData } = {};
e.querySelectorAll(".av__body").forEach((item: HTMLElement) => {
pageSizes[item.dataset.groupId || "unGroup"] = item.dataset.pageSize;
- if (!item.querySelector(".av__row") || e.getAttribute(Constants.ATTRIBUTE_V_SCROLL) !== "true") {
+ // 守卫只保证至少 1 个 .av__row,但首行索引取的是 [1](首个数据行,[0] 为表头)。
+ // 虚拟滚动 trim 后某分组可能只剩表头,[1] 不存在时需跳过,避免解引用 undefined.getAttribute
+ const secondRow = item.querySelectorAll(".av__row")[1] as HTMLElement;
+ if (!secondRow || e.getAttribute(Constants.ATTRIBUTE_V_SCROLL) !== "true") {
return;
}
virtualData[item.getAttribute("data-group-id") || "all"] = getBodyVirtualData(
- item, ".av__row--util", parseInt(item.querySelectorAll(".av__row")[1].getAttribute("data-index")));
+ item, ".av__row--util", parseInt(secondRow.getAttribute("data-index")));
});
const headerTransformElement = e.querySelector('.av__row--header[style^="transform"]') as HTMLElement;
const footerTransformElement = e.querySelector('.av__row--footer[style^="transform"]') as HTMLElement;
diff --git a/app/src/protyle/render/av/virtualScroll.ts b/app/src/protyle/render/av/virtualScroll.ts
index 3388c28b8e..a877abed02 100644
--- a/app/src/protyle/render/av/virtualScroll.ts
+++ b/app/src/protyle/render/av/virtualScroll.ts
@@ -39,7 +39,13 @@ const doTrim = (blockElement: HTMLElement, elementRect: DOMRect): void => {
const bottomLimit = elementRect.bottom + buffer;
const blockRect = blockElement.getBoundingClientRect();
- const protyle = dataStore.get(blockElement.getAttribute("data-av-id") + blockElement.getAttribute(Constants.CUSTOM_SY_AV_VIEW)).protyle;
+ // AV 重渲/新增分组/局部更新未走完整 initVirtualScroll 时 dataStore 可能缺失,跳过本次 trim,
+ // 等下次 initVirtualScroll 重新登记后再处理,避免解引用 undefined.protyle
+ const stored = dataStore.get(blockElement.getAttribute("data-av-id") + blockElement.getAttribute(Constants.CUSTOM_SY_AV_VIEW));
+ if (!stored) {
+ return;
+ }
+ const protyle = stored.protyle;
const isScrollingUp = lastScrollTop && lastScrollTop > protyle.contentElement.scrollTop;
lastScrollTop = protyle.contentElement.scrollTop;
@@ -51,6 +57,11 @@ const doTrim = (blockElement: HTMLElement, elementRect: DOMRect): void => {
const bodies = blockElement.querySelectorAll(".av__body:not(.fn__none)") as NodeListOf<HTMLElement>;
bodies.forEach((bodyEl: HTMLElement) => {
const state = bodyStates.get(bodyEl);
+ // body 尚未在 initVirtualScroll 中登记(重渲/新增分组/局部更新未走完整流程),
+ // WeakMap 查不到则跳过本次 trim,避免解引用 undefined.view
+ if (!state) {
+ return;
+ }
const dataRows = type === "table" ? (state.view as IAVTable).rows : (state.view as IAVKanban).cards;
let currentRows;
let bottomElement;
@@ -325,7 +336,10 @@ const doTrim = (blockElement: HTMLElement, elementRect: DOMRect): void => {
// 需据此扩展渲染窗口让新行立即可见,否则虚拟滚动下新行会落在窗口外不渲染。
export const getBodyVirtualData = (bodyEl: HTMLElement, endSelector: string, firstRowIndex: number): IAVVirtualData => {
// 末尾标记前可能存在连续 ghost 行,向前回溯找到真实末行
- let lastRow = bodyEl.querySelector(endSelector).previousElementSibling as HTMLElement;
+ // 末尾标记(.av__row--util / .av__gallery-add)缺失时(重渲竞态)直接回退到 firstRowIndex,
+ // 避免解引用 null.previousElementSibling
+ const endMarker = bodyEl.querySelector(endSelector);
+ let lastRow = endMarker ? endMarker.previousElementSibling as HTMLElement : null;
while (lastRow && !lastRow.getAttribute("data-index")) {
lastRow = lastRow.previousElementSibling as HTMLElement;
}