Skip to content

[lexical-table] Feature: Skip colgroup element in table DOM with undefined col widths - #8850

Merged
etrepum merged 4 commits into
meta:mainfrom
zacharyg-qualtrics:optionalColgroup
Jul 22, 2026
Merged

etrepum merged 4 commits into
meta:mainfrom
zacharyg-qualtrics:optionalColgroup

Conversation

@zacharyg-qualtrics

Copy link
Copy Markdown
Contributor

Description

Currently, the createDOM method of TableNode always adds a colgroup element as a child of the table. However, if the colWidths property is undefined, then the colgroup element ends up being meaningless. This change adds a check in createDOM to only add the element if colWidths is defined.

Test plan

Changes are covered by already existing tests; tests that verified the DOM previously checked that colgroup was included in the table. For cases where the column widths were not being set, those tests have been updated to remove the required colgroup in the expected output. For cases where the column widths were being set, those continue to pass.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jul 16, 2026
@vercel

vercel Bot commented Jul 16, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
lexical Ready Ready Preview Jul 21, 2026 10:33pm
lexical-playground Ready Ready Preview Jul 21, 2026 10:33pm

Request Review

tableElement.appendChild(colGroup);
setDOMUnmanaged(colGroup);
const colWidths = this.getColWidths();
if (colWidths) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is correct because it prevents the table node from ever having a colgroup child if it was created without one. There should be a corresponding updateDOM to create one when/if it makes sense.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch; I tested locally adding column widths after the node was created and the change wasn't reflected in the editor. I've updated $updateColgroup to handle this.

It looks like the unit tests should have caught this case (specifically the test Update column widths), so I had thought it was working. However, the unit tests missed it because they were testing that the DOM was updated by calling createDOM directly to get the DOM element, rather than retrieving it from what the editor was rendering. I changed that test to retrieve the element using the node key and assert against that rather than relying on createDOM. The tests Toggle row striping ON/OFF, Toggle frozen first column ON/OFF and Change Table-level alignment would have this bug as well. I didn't change them because I didn't want to modify tests that weren't related to the change I was making, but if you want I can update those tests as well to retrieve the element directly.

There are a few miscellaneous changes that were added to LexicalTableNode.ts in my latest commit; these were fixes that the repository's ESLint and Prettier configuration changed separately from my changes.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's fine to clean up neighboring tests if you think they are not covering the behavior well enough

Comment on lines +96 to +101
function stripTableContents(element: HTMLElement): HTMLElement {
const clone = element.cloneNode(true) as HTMLElement;
clone.querySelectorAll('tr').forEach(tr => tr.remove());
return clone;
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems kinda weird, why not test the actual html of the table, or narrow the test to query a specific selector (e.g. colgroup)?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comparing against the HTML string is a lot more straightforward than trying to do a full DOM comparison; the test would have to create all of the elements it expects in the document and structure them together. Comparing HTML strings is more readable because the HTML structure is typed into the test file for developers to see, instead of having to read through a bunch of DOM create and append statements.

Testing against the HTML DOM directly also doesn't fix what this helper function is meant to address. The existing tests are getting the DOM by invoking createDOM directly; this returns the TableNode's DOM, but notably does not include anything that would have been created by child nodes. Retrieving the DOM directly from the editor in this change means that the child nodes have had their createDOM methods invoked, so they're included in the DOM from the editor. Testing against the DOM directly would still require accounting for the child nodes in the test output, either by stripping them out (i.e. using this helper function) or including them in the expected result (which would mean that this test file is affected by changes to LexicalTableRowNode and LexicalTableCellNode).

Testing against a specific selector is a bigger departure from the existing testing patterns in the file (which are all getting the table DOM and comparing the entire object to an HTML string), plus would be more likely to have gaps in the test like the one I'm trying to fix. If the test just queries for the colgroup element and checks for the correct property on that element, it might miss changes that cause other colgroup properties to change or that change the colgroup element's relationship with its parent.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you didn’t quite understand the feedback. Two options:

  1. test the html as-is instead of cloning the table and doing some weird stuff (removing tr children in this case) that wasn’t done previously. It’s fine if expectations have to change when the implementation changes.
  2. Use the colgroup instead of the table as the root for the HTML comparison

Nowhere did I suggest using DOM to do comparisons without html

@zacharyg-qualtrics zacharyg-qualtrics Jul 20, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Testing it "as-is" just generally seems like a bad idea, since it makes these tests directly dependent on the behavior of LexicalTableRowNode and LexicalTableCellNode; changing behavior for one node shouldn't affect the success of unit tests for another node.

Using colgroup as the the root for the comparison in this specific test would work, but that would mean leaving open a new test gap in place of the one I'm trying to fix. For example, if we're doing:

const tableElement = editor.getElementByKey(table.__key);
expect(tableElement).toBeDefined();
const colgroupElement = _.find(tableElement.children, (child) => child.tagName === 'COLGROUP');
expect(colgroupElement).toBeDefined();
expectColgroupHtmlToBeEqual(colgroupElement, ...);

This immediately creates a new gap in the test where, if changing the colWidths property modifies the properties of the table element, that change won't be covered by the test. It also just doesn't work for the other tests in the file which this same issue (calling createDOM directly and missing any affects from updateDOM, since those tests are checking values on the table as a whole and not just the colgroup element).

Aside from it being "weird stuff", are any reasons why removing the children from the table is going to cause actually testing issues? This seems like a fairly straightforward way to test the HTML created by the table node without also testing all of its children.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These nodes aren’t independent and trying to isolate their behavior in unit tests is not a design goal. The unit under test is the table package, not specifically the TableNode class independent of TableRowNode, TableCellNode, or even various other behavior included when the extension or legacy plugins are active in the editor.

) {
const colGroup = dom.querySelector('colgroup');
if (!colGroup) {
let colGroup = dom.querySelector('colgroup');

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this query is specific enough in the case of nested tables, if the parent table doesn't have col widths set but an already rendered nested table does this could find the colgroup that the child owns.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be clear something like this is probably better:

Suggested change
let colGroup = dom.querySelector('colgroup');
let colGroup = dom.querySelector(':scope > colgroup');

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can test and add that change 👍

tableElement.appendChild(colGroup);
setDOMUnmanaged(colGroup);
const colWidths = this.getColWidths();
if (colWidths) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's fine to clean up neighboring tests if you think they are not covering the behavior well enough

…nd use read for read-only, generate redundant table body expectations
Merges current main into the PR branch. The only conflict was in
LexicalTableNode.test.tsx, where the PR's synchronous test refactor
overlapped with main's migration to $createTableSelectionFrom (meta#8855).
Resolved by keeping the synchronous refactor while adopting the new
$createTableSelectionFrom API.
@etrepum
etrepum added this pull request to the merge queue Jul 22, 2026
Merged via the queue into meta:main with commit 7f3ea64 Jul 22, 2026
40 checks passed
@etrepum etrepum mentioned this pull request Jul 29, 2026

This branch was successfully deployed

2 active deployments
Preview – lexical — c56b2458 Deployed Jul 21, 2026 by vercel[bot]
Preview – lexical-playground — c56b2458 Deployed Jul 21, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. extended-tests Run extended e2e tests on a PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants