ISSUE DESCRIPTION [Bug]: extension-table 3.23 TableView NodeView ignores HTMLAttributes…
- User
ISSUE DESCRIPTION
[Bug]: extension-table 3.23 TableView NodeView ignores HTMLAttributes (regression from 3.22.4)
Affected Packages
@tiptap/extension-table
Version(s)
3.23.1 (also 3.23.2, 3.23.4)
Bug Description
Summary
In
@tiptap/extension-table3.23.x, configuring the Table extension withHTMLAttributesno longer applies those attributes to the rendered<table>element whenresizable: false(the default). The class/attributes silently disappear from the DOM.This is a regression from 3.22.4, where the same configuration worked correctly.
TableViewexisted byte-identical in 3.22.4, but the Table extension had noaddNodeView()method, so ProseMirror always fell back to the schema'srenderHTML. 3.23 addedaddNodeView()that wires the NodeView in, bypassingrenderHTML.Reproduction
import { Editor } from '@tiptap/core'; import StarterKit from '@tiptap/starter-kit'; import { Table, TableRow, TableHeader, TableCell } from '@tiptap/extension-table'; new Editor({ extensions: [ StarterKit, Table.configure({ HTMLAttributes: { class: 'my-custom-table' } }), TableRow, TableHeader, TableCell ], content: '<table><tbody><tr><td>cell</td></tr></tbody></table>' });Inspect the rendered DOM. The
<table>element has noclass="my-custom-table".Browser Used
Chrome
Code Example URL
https://codesandbox.io/p/devbox/laughing-glade-n4qh4r
Expected Behavior
The
<table>element should render withclass="my-custom-table"(the documented Table.configure HTMLAttributes behavior), regardless of theresizablesetting.A minimal fix would have
TableViewapplythis.options.HTMLAttributesto the inner<table>element in its constructor.Additional Context (Optional)
Root cause
3.23 introduced
addNodeView()on the Table extension (extension-tabledist/index.jsaround line 645):addNodeView() { const isResizable = this.options.resizable && this.editor.isEditable; const View = this.options.View; if (isResizable || !View) { return null; } return ({ node, view }) => new View(node, this.options.cellMinWidth, view); } Assistant
I'll start by exploring the repository structure to understand the codebase and locate the relevant files.
Assistant
Good, I found the relevant files. Let me look at the main files mentioned in the issue:
/tiptap/packages/extension-table/src/table/table.ts- The Table extension/tiptap/packages/extension-table/src/table/TableView.ts- The TableView NodeView
Let me examine these files to understand the issue better.
Assistant
The open command doesn't use a "file" parameter. Let me check the correct usage.
Assistant
Now let me look at the Table extension to see how it configures the NodeView:
Assistant
Perfect! I can see the issue clearly now. Let me analyze:
- The
addNodeView()method (lines ~376-387) returns a NodeView whenisResizableis false andViewis the defaultTableView - The
addNodeView()creates a newView(node, this.options.cellMinWidth, view)but doesn't passthis.options.HTMLAttributes - The
TableViewconstructor doesn't accept or applyHTMLAttributes- it only creates a basic<table>element without any custom attributes
The issue description suggests that
TableViewshould applythis.options.HTMLAttributesto the inner<table>element. However, looking at the code,TableViewdoesn't have access tothis.optionsfrom the Table extension.Let me check if there are tests related to this:
- The
Assistant
Let me check if there are any existing tests for HTMLAttributes:
Assistant
Let me also check the existing test files to understand the test structure:
Assistant
Now let me create a reproduction script to confirm the bug:
Assistant
Let me check what the build output looks like:
Assistant
The package hasn't been built yet. Let me build it first:
Assistant
manu-script
Assistant
Good! Now let me build the extension-table package:
Assistant
Great! Now let me also build the dependencies:
Assistant
Now let me run the test to confirm the bug:
Assistant
Let me check if there's already a test environment set up:
Assistant
Let me wait for the tests to finish and run the specific tests for extension-table:
Assistant
Great! The tests pass. Now let me create a simple test script to reproduce the issue:
Assistant
Now let me understand the issue better by looking at how
mergeAttributesworks:Assistant
Now I understand the issue. The problem is:
- When
addNodeView()returns a NodeView (TableView), it bypasses the schema'srenderHTMLwhich appliesHTMLAttributes TableViewcreates the DOM elements manually but doesn't applythis.options.HTMLAttributes- The fix should make
TableViewaccept and apply the HTMLAttributes from the Table extension
Let me look at the solution approach. I need to:
- Pass
HTMLAttributestoTableViewthrough theaddNodeView()method - Update
TableViewto accept and apply these attributes
Let me first check the type definition for
TableView:- When
Assistant
Now let me create the fix. The approach will be:
- Modify
TableViewconstructor to accept an optionalHTMLAttributesparameter - Apply those attributes to the
this.tableelement in the constructor - Pass
HTMLAttributesfrom the Table extension'saddNodeView()method toTableView
Let me implement the fix:
- Modify
Assistant
Now let me apply these changes manually:
Assistant
Now let me edit the TableView.ts file to add the htmlAttributes parameter:
Assistant
Let me use sed instead:
Assistant
Now let me add the code to apply HTMLAttributes. Let me first see the exact lines:
Assistant
Now I need to insert the HTMLAttributes code. Let me use edit command:
Assistant
Let me use the edit command to add the code to apply HTMLAttributes:
Assistant
Let me verify the changes:
Assistant
Perfect! Now I need to update the table.ts file to pass the HTMLAttributes to the TableView constructor. Let me check the addNodeView method:
Assistant
Now I need to update line 534 to pass HTMLAttributes:
Assistant
Now let me verify the change:
Assistant
Excellent! Now I also need to update the TableOptions interface to include the new parameter in the View type. Let me check the type definition:
Assistant
Let me update the type definition for the View option:
Assistant
Let me verify the change:
Assistant
Perfect! Now let's rebuild the extension-table package and test our changes:
Assistant
Great! The build succeeded. Now let me check if the columnResizing plugin also needs to be updated, since it has its own NodeView when resizable is enabled. Let me check:
Assistant
Let me check the columnResizing plugin to see if it needs to be updated too:
Assistant
The columnResizing plugin from prosemirror-tables doesn't seem to accept HTMLAttributes directly, but it does accept a View. Let me check if the View in columnResizing is called with the same parameters. Looking at the issue description, it mentions that when
resizable: true, there's a different mechanism. But the issue says that when resizable is true (and View is provided), the columnResizing plugin registers its own NodeView. Let me check if columnResizing passes htmlAttributes to the View.Actually, looking at the code more carefully, when
isResizableis true, theaddNodeView()method returnsnull, so it doesn't use our TableView at all - instead the columnResizing plugin handles it. Let me check if columnResizing from prosemirror-tables needs to be updated.Let me search for how columnResizing uses the View:
Assistant
Now let me check the source code of prosemirror-tables to see if it passes htmlAttributes to the View:
Assistant
I can see the issue! When columnResizing creates the NodeView, it calls:
return new View(node, defaultCellMinWidth, view);It only passes 3 parameters (node, defaultCellMinWidth, view), not the htmlAttributes. This is in the prosemirror-tables package, which we shouldn't modify.
However, the issue description mentions that the problem is specifically when
resizable: false(the default). When resizable is true, the workaround is different. Let me focus on the main issue first - fixing the non-resizable case.But wait, I should also check if there's a way to make it work for the resizable case too. Let me think about this...
Actually, looking at the columnResizing code more carefully, I see that it registers nodeViews in the plugin's props:
nodeViews[tableName] = (node, view) => { return new View(node, defaultCellMinWidth, view); };This means it creates its own nodeView that doesn't have access to the Table extension's options. This is a more complex issue.
For now, let me focus on fixing the non-resizable case (which is the default and the main issue reported). Let me create a test to verify my fix works:
Assistant
Now let me run the test to see if my fix works:
Assistant
I see the issue! The error is:
TypeError: Cannot convert object to primitive value ❯ HTMLTableElement.setAttributeThis is happening because some HTML attributes like
classare special in some DOM implementations. The issue is thatsetAttributeexpects a string value, but when we pass the value from the object, it might not be a string. Let me check what values are being passed.Actually, looking at the error more carefully, it says "Cannot convert object to primitive value", which suggests that
htmlAttributes[name]is an object instead of a string. This is strange because in the test we're passing strings.Wait, I think I see the issue. In the HTMLAttributes object, the
classattribute should be handled specially. Let me check how tiptap's mergeAttributes handles this:Assistant
I see! The
mergeAttributesfunction handles special cases forclassandstyleattributes. But in my implementation, I'm just callingsetAttributedirectly with the value, which should work for strings.The issue is that
setAttributeis being called with values that might not be strings. Let me look at the actual values being passed. Let me trace through the code to see what's happening.Actually, looking at the error message again:
TypeError: Cannot convert object to primitive value ❯ HTMLTableElement.setAttribute node_modules/.pnpm/happy-dom@20.8.9/node_modules/happy-dom/src/nodes/element/Element.ts:714:37This is coming from happy-dom (the test DOM implementation). The issue might be that happy-dom doesn't handle certain attributes well. Let me check if there's a special case I need to handle.
Actually, I think the issue is that I need to convert the value to a string. Let me update the code:
Assistant
cat > /tmp/fix_setattribute.js << 'EOFJS' // Read the file const fs = require('fs'); let content = fs.readFileSync('packages/extension-table/src/table/TableView.ts', 'utf8');
// Replace the setAttribute call with proper string conversion const old_code =
// Apply HTMLAttributes to the table element if (htmlAttributes) { Object.keys(htmlAttributes).forEach(name => { this.table.setAttribute(name, htmlAttributes[name]) }) };const new_code =
// Apply HTMLAttributes to the table element if (htmlAttributes) { Object.keys(htmlAttributes).forEach(name => { this.table.setAttribute(name, String(htmlAttributes[name])) }) };content = content.replace(old_code, new_code);
// Write the file back fs.writeFileSync('packages/extension-table/src/table/TableView.ts', content);
console.log("TableView.ts updated successfully"); EOFJS
cd /tiptap && node /tmp/fix_setattribute.js
Assistant
Good! Now let me rebuild and test again:
Assistant
The error is still happening at
String(htmlAttributes[name]). This means thathtmlAttributes[name]is an object that can't be converted to a string. This is strange because we're passing simple strings in the test.Let me add some debugging to understand what's being passed:
Assistant
The debug shows that the HTMLAttributes object is correct (class is a string), but when it reaches the TableView constructor, something goes wrong. Let me add more debug output to the TableView constructor to see what's actually being passed:
Assistant
Aha! The issue is clear now! The
htmlAttributesparameter is receiving theEditorViewobject instead of the actual HTMLAttributes! This means I'm passing the wrong parameter in theaddNodeView()method.Looking at the line:
return ({ node, view }) => new View(node, this.options.cellMinWidth, view, this.options.HTMLAttributes)The parameters are:
- node
- this.options.cellMinWidth
- view (the EditorView)
- this.options.HTMLAttributes
But in the TableView constructor, the signature is:
constructor(node: ProseMirrorNode, cellMinWidth: number, htmlAttributes?: Record<string, any>)Wait, I removed the
viewparameter from the constructor! Let me check the original constructor signature again:Assistant
I see the issue! In the table.ts file, the addNodeView method passes 4 parameters:
new View(node, this.options.cellMinWidth, view, this.options.HTMLAttributes)But the TableView constructor only accepts 3 parameters:
constructor(node: ProseMirrorNode, cellMinWidth: number, htmlAttributes?: Record<string, any>)So the
viewparameter is being passed ashtmlAttributes! I need to add theviewparameter back to the constructor. Let me check the original signature in the type definition:Assistant
I see! The type definition says the View should accept 4 parameters:
(node, cellMinWidth, view, htmlAttributes). But the TableView constructor only has 3. I need to add theviewparameter to the TableView constructor:Assistant
Now I need to add the import for EditorView at the top of the file:
Assistant
Let me verify the changes:
Assistant
Good! Now let me remove the debug statements:
Assistant
The user is showing me that my last response didn't include a proper tool call. I need to continue with my testing and verification. Let me rebuild the package and run the tests again.
Assistant
Good! The build succeeded. Now let me run the HTMLAttributes test again to see if it passes.
Assistant
Excellent! The HTMLAttributes tests pass! Now let me run all the existing extension-table tests to make sure I didn't break anything:
Assistant
Perfect! All tests pass including the new HTMLAttributes tests. Let me remove the debug test file I created and verify the final changes:
Assistant
Now let me verify the changes I made by viewing the final code:
Assistant
Good, the import is correct. Now let me check the constructor:
Assistant
Perfect! Now let me check the addNodeView method in table.ts:
Assistant
Great! Let me also check the View type definition:
Assistant
Perfect! Now let me run all the tests one more time to make sure everything passes:
Assistant
Let me verify the final changes by checking what files were modified:
Assistant
Perfect! Let me view the complete diff to see all the changes:
Assistant
I need to check the diff for the table.ts file as well to verify all changes.
Assistant
Let me run a final comprehensive test to ensure the fix works correctly before submitting.
Assistant
Perfect! All tests pass. Let me provide a summary of the changes and submit the solution.
Summary
I've successfully fixed the bug where
HTMLAttributeswere being ignored by theTableViewNodeView in@tiptap/extension-tableversion 3.23.x.Root Cause
The issue was that when
addNodeView()returns a NodeView (TableView), it bypasses the schema'srenderHTMLwhich normally appliesHTMLAttributes. TheTableViewconstructor was not accepting or applying theHTMLAttributesfrom the Table extension's options.Solution
I made the following changes:
-
Updated
TableView.ts:- Added
EditorViewimport - Updated constructor to accept and optional parameters
- Added
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.