Repository navigation
[lexical-table] Feature: Skip colgroup element in table DOM with undefined col widths - #8850
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
c395174 to
88d3f64
Compare
| tableElement.appendChild(colGroup); | ||
| setDOMUnmanaged(colGroup); | ||
| const colWidths = this.getColWidths(); | ||
| if (colWidths) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
It's fine to clean up neighboring tests if you think they are not covering the behavior well enough
| function stripTableContents(element: HTMLElement): HTMLElement { | ||
| const clone = element.cloneNode(true) as HTMLElement; | ||
| clone.querySelectorAll('tr').forEach(tr => tr.remove()); | ||
| return clone; | ||
| } | ||
|
|
There was a problem hiding this comment.
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)?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
I think you didn’t quite understand the feedback. Two options:
- 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.
- 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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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'); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
To be clear something like this is probably better:
| let colGroup = dom.querySelector('colgroup'); | |
| let colGroup = dom.querySelector(':scope > colgroup'); |
There was a problem hiding this comment.
I can test and add that change 👍
| tableElement.appendChild(colGroup); | ||
| setDOMUnmanaged(colGroup); | ||
| const colWidths = this.getColWidths(); | ||
| if (colWidths) { |
There was a problem hiding this comment.
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.
Description
Currently, the
createDOMmethod ofTableNodealways adds acolgroupelement as a child of the table. However, if thecolWidthsproperty is undefined, then thecolgroupelement ends up being meaningless. This change adds a check increateDOMto only add the element ifcolWidthsis defined.Test plan
Changes are covered by already existing tests; tests that verified the DOM previously checked that
colgroupwas included in the table. For cases where the column widths were not being set, those tests have been updated to remove the requiredcolgroupin the expected output. For cases where the column widths were being set, those continue to pass.