BookStackApp/BookStack · error · Error

Expected to find TableElement in DOM

Error message

Expected to find TableElement in DOM

What it means

TableObserver.trackTable runs an editor update that fetches the table's DOM element via editor.getElementByKey(this.tableNodeKey) to start observing it. If the element is not present in the DOM at that moment, it throws 'Expected to find TableElement in DOM'. The observer cannot function without the element it watches.

Source

Thrown at resources/js/wysiwyg/lexical/table/LexicalTableObserver.ts:139

          if (
            nodeName === 'TABLE' ||
            nodeName === 'TBODY' ||
            nodeName === 'THEAD' ||
            nodeName === 'TR'
          ) {
            gridNeedsRedraw = true;
            break;
          }
        }

        if (!gridNeedsRedraw) {
          return;
        }

        const tableElement = this.editor.getElementByKey(this.tableNodeKey);

        if (!tableElement) {
          throw new Error('Expected to find TableElement in DOM');
        }

        this.table = getTable(tableElement);
      });
    });
    this.editor.update(() => {
      const tableElement = this.editor.getElementByKey(this.tableNodeKey);

      if (!tableElement) {
        throw new Error('Expected to find TableElement in DOM');
      }

      this.table = getTable(tableElement);
      observer.observe(tableElement, {
        attributes: true,
        childList: true,
        subtree: true,
      });

View on GitHub (pinned to 18f8469a1c)

Solutions

  1. Defer observer/selection creation until after the table is rendered (e.g. in a useEffect or command listener post-mount).
  2. Check editor.getElementByKey(tableNode.getKey()) for null before constructing a TableObserver.
  3. Ensure the editor root is mounted in the document before initializing table selection state.
  4. If the table may be deleted, guard lifecycle so observers are not created for detached nodes.

Example fix

// before
const observer = new TableObserver(editor, tableNodeKey, ...); // throws if unmounted

// after
if (!editor.getElementByKey(tableNodeKey)) return;
const observer = new TableObserver(editor, tableNodeKey, ...);
Defensive patterns

Strategy: validation

Validate before calling

if (!editor.getElementByKey(tableNodeKey)) {
  // defer observer creation until rendered
  return;
}
const observer = new TableObserver(editor, tableNodeKey, ...);

Try / catch

try {
  new TableObserver(editor, tableNodeKey, ...);
} catch (e) {
  if (e instanceof Error && e.message === 'Expected to find TableElement in DOM') {
    retryWhenMounted(() => new TableObserver(editor, tableNodeKey, ...));
  } else throw e;
}

Prevention

When it happens

Trigger: Constructing a TableObserver (via the table selection plugin) for a table node whose DOM element has not yet been rendered, was removed, or belongs to a headless/unmounted editor.

Common situations: Programmatically creating table selections before the table renders (e.g. in initial editor state setup); tables removed by concurrent updates while an observer starts; test environments where the editor content is not flushed to the DOM.

Related errors


AI-assisted analysis of BookStackApp/BookStack@18f8469a1c (2026-09-02). Data as JSON: /api/errors/7e870e71edc3f703. Report an issue: GitHub.