ISSUE DESCRIPTION [PRO]: unsetAllMarks deletes comments Affected Packages…
- User
ISSUE DESCRIPTION
[PRO]: unsetAllMarks deletes comments
Affected Packages
extension-comments
Version(s)
2.17.5
Description of the Bug
Our Tiptap editor has a "clear formatting" button that calls
unsetAllMarksto remove formatting from the selection.We noticed, however, that if the selection includes content associated with a comment, the comment is also deleted (presumably because the
<span>element wrapping the content is removed).Is this a bug? I can see how we might want
unsetAllMarksto include comments, if we consider the comment tag a "mark," but at the same time I imagine users would generally not expectunsetAllMarksto delete comments.For now, as a workaround, we are calling
unsetMarkseparately for all non-comment marks, e.g.editor.chain().focus().unsetMark('link').unsetMark('bold').unsetMark('strike') ...... but would be nice to be able to use
unsetAllMarksagain for formatting-specific marks. Thanks!Browser Used
Chrome
Code Example (Preferred)
No response
Expected Behavior
unsetAllMarkswill remove formatting marks (listed here) but not remove comment tags or comments.Additional Context (Optional)
No response
Dependency Updates
- Yes, I've updated all my dependencies.
Evaluation contract
Marks are clearable by default. A mark configured with
clearable: falsemust surviveunsetAllMarks(), whileunsetAllMarks({ignoreClearable: true})removes it. Callingcan().unsetAllMarks()on a selection containing only non-clearable marks is a successful no-op and must returntrue; explicitly callingunsetMarkmay still remove a non-clearable mark.Interface
Type: Mark configuration option Name: MarkConfig.clearable Path: packages/core/src/Mark.ts Type: boolean | (() => boolean) Default: true Description: Controls whether unsetAllMarks removes this mark. Set false for semantic marks such as comments that must survive clear-formatting operations.
Assistant
Let me analyze the issue:
- The bug is that
unsetAllMarksdeletes comments because it removes all marks, including the comment tag - The solution is to add a
clearableoption to mark configuration that controls whetherunsetAllMarksremoves the mark - Marks with
clearable: falseshould surviveunsetAllMarks()unlessignoreClearable: trueis passed can().unsetAllMarks()should returntruewhen the selection only contains non-clearable marks (successful no-op)
Let me start by exploring the repository structure to understand the codebase.
- The bug is that
Assistant
Let me explore more of the repository structure, especially looking for the core package, Mark.ts, and unsetAllMarks.ts files.
Assistant
I need to examine the key files mentioned in the issue:
/tiptap/packages/core/src/Mark.ts- where I need to add theclearableconfiguration option/tiptap/packages/core/src/commands/unsetAllMarks.ts- where I need to modify the command to respect theclearableoption/tiptap/packages/core/src/types.ts- to check if there are type definitions I need to update
Let me start by reading the Mark.ts file.
Assistant
I need to read the Mark.ts file first to understand the current structure before making changes. Let me use the correct syntax for opening a file.
Assistant
Now let me look at the unsetAllMarks command file:
Assistant
Now let me check the types.ts file to understand the type definitions for commands:
Assistant
Now let me look at the
RawCommandstype and the commands index file to understand how commands are structured:Assistant
Let me look at the commands index file and see how commands are exported:
Assistant
Let me also check the unsetMark command to see how it works, since we may need to understand the pattern:
Assistant
Now let me check the
commands.tsfile or where theCommandsinterface is defined to understand module augmentation:Assistant
Now let me check how the Editor class accesses marks and their configuration. I need to understand how to get mark configuration from the editor:
Assistant
Let me check how the ExtensionManager works to understand how to get mark configuration:
Assistant
Let me look at the ExtensionManager more closely to understand how to access mark extensions:
Assistant
Let me look at the
splitExtensionshelper to understand how mark extensions are accessed:Assistant
Now I need to understand how the
getExtensionFieldhelper works:Assistant
Now let me check the helpers to see if there's a way to get an extension by name:
Assistant
Let me look at how I can get access to mark extensions from within the command. I need to understand what
editororstateprovides:Assistant
Now let me look at how to find mark extensions by name in the editor. I need to see how the extension manager works:
Assistant
Now I have a good understanding of the codebase. Let me also check if there are existing tests for
unsetAllMarks:Assistant
I need to run the find command differently. Let me search for test files related to unsetAllMarks.
Assistant
Now I understand the structure. Let me create my implementation plan:
- Add
clearableoption toMarkConfigin/tiptap/packages/core/src/Mark.ts - Modify
unsetAllMarkscommand in/tiptap/packages/core/src/commands/unsetAllMarks.tsto:- Accept an optional
ignoreClearableparameter - Skip marks with
clearable: falseunlessignoreClearable: true - Return
trueforcan().unsetAllMarks()even when selection only contains non-clearable marks (successful no-op)
- Accept an optional
- Export the new types from the index files if needed
Let me start implementing:
- Add
Assistant
Now let me look at how the CommandProps type is defined:
Assistant
Now I understand the structure. Let me implement the changes:
- First, add the
clearableoption toMarkConfiginMark.ts - Then, update
unsetAllMarks.tsto respect theclearableoption
Let me start by modifying the Mark.ts file to add the
clearableconfiguration option:- First, add the
Assistant
Now I'll add the
clearableoption to theMarkConfiginterface. I'll add it after theaddAttributesproperty:Assistant
Let me use the proper edit syntax with the line range:
Assistant
Error: Expected a single action string (has 47 chunks)
Assistant
I need to use the proper format for the bash_terminal tool call. Let me fix the syntax.
Assistant
I need to verify the change was applied correctly to the Mark.ts file. Let me check the file content.
Assistant
I see the
clearableproperty was added twice. Let me fix that by removing the duplicate.Assistant
I need to add the
clearableproperty to the MarkConfig interface. The interface ends at line 144 with}. I need to insert the new property before the closing brace. Let me use sed to insert the property after line 143.Assistant
The previous sed command worked. Let me verify the changes were applied correctly to Mark.ts before moving on to modify unsetAllMarks.ts.
Assistant
Good, the
clearableproperty is now added toMarkConfig. Now I need to modify theunsetAllMarks.tscommand to:- Accept an optional
ignoreClearableparameter - Skip marks with
clearable: falseunlessignoreClearable: true - Handle the
can().unsetAllMarks()case where selection only contains non-clearable marks (should returntrueas a successful no-op)
- Accept an optional
Assistant
Now I need to modify
unsetAllMarks.tsto respect theclearableoption. I need to:- Accept an optional
ignoreClearableparameter - Check each mark's
clearableconfig - Only remove marks that are clearable (or all marks if
ignoreClearable: true) - Handle the
can()case properly - returntruewhen only non-clearable marks exist
Let me first look at how other commands access mark extensions to check their configuration:
- Accept an optional
Assistant
Let me look at how toggleList accesses extensions to understand the pattern:
Assistant
Now I understand the pattern. I need to modify
unsetAllMarks.tsto:- Accept an optional
{ ignoreClearable?: boolean }parameter - Check each mark's
clearableconfiguration - Only remove clearable marks (unless
ignoreClearable: true) - Handle
can()case - returntruewhen only non-clearable marks exist
Let me first check how to get the
getExtensionFieldhelper:- Accept an optional
Assistant
Good, the helper is exported. Now let me write the updated
unsetAllMarks.tsfile. I need to:- Import necessary helpers
- Update the type declaration to accept options with
ignoreClearable - Implement the logic to check each mark's
clearableconfig
Assistant
Let me try a different approach to update the file:
Assistant
Assistant
Let me review my implementation and make sure it's correct. Looking at the code, I notice that the implementation looks good, but let me verify the changes are complete and run any existing tests to make sure nothing is broken.
Let me first check if there are any tests I can run for the core package, and also verify the TypeScript compiles correctly.
Assistant
Good, the Mark.ts file has the clearable property. Now let me verify the unsetAllMarks.ts file and then run the tests to make sure everything works correctly.
Assistant
I need to verify the TypeScript compiles correctly and run the unit tests to make sure the changes don't break anything. Let me try running the unit tests for the core package first.
Assistant
The test command timed out. Let me try a more targeted approach - first check if the TypeScript compiles correctly, then look at any specific tests.
Assistant
The TypeScript errors are from demos and node_modules, not from my changes. Let me check if my specific files compile correctly by checking for errors in the core package only.
Assistant
I see there are TypeScript errors in my code. The
rangevariable in theranges.forEachcallback is of typeSelectionRangewhich doesn't havefromandtoproperties directly. I need to userange.$from.posandrange.$to.posinstead. Let me fix this.Assistant
I see the issue - I'm destructuring
fromandtofromrange, butSelectionRangeuses$from.posand$to.pos. Let me fix the code:Assistant
Let me verify the TypeScript compiles correctly now for the files I modified.
Assistant
No TypeScript errors in my modified files. Now let me create a test script to verify the implementation works correctly.
Assistant
I need to run the test script to verify the implementation works correctly. Let me first check if the project has the proper build setup to run TypeScript files directly, or if I need to build first.
Assistant
Let me check if the dist folder exists and if the package is already built, or if I need to use a different approach to test.
Assistant
Let me check if vitest is available and run the existing unit tests for the core package to make sure my changes don't break anything.
Assistant
The tests are passing. Let me continue to see if there are more tests running and verify all tests pass.
Assistant
All 410 tests pass. Now let me create a simple test script to verify my implementation works correctly for the specific use case described in the issue.
Assistant
I need to build the packages first or use a different approach to run the test. Let me check how the existing tests work.
Assistant
I see the tests use vitest with imports from
@tiptap/core. Let me create a proper vitest test file that will be run with the existing test infrastructure.Assistant
Let me run the test file I created to verify my implementation works correctly.
Assistant
I see the issue - my implementation is not working correctly. The marks are not being removed at all. Let me analyze the problem:
- The test "should preserve non-clearable marks (comment)" passes - meaning the comment mark is preserved
- But "should remove clearable marks (bold)" fails - meaning bold is NOT being removed
This suggests my implementation is not removing ANY marks. Let me look at my code again and fix the issue.
Looking at my code, the problem is that when
dispatchis truthy, I'm checking for marks in the selection range, but the way I'm getting marks and removing them might be incorrect. ThenodesBetweenapproach is only finding marks on text nodes, but I should be looking at the marks at the selection position.Actually, looking more carefully at the original implementation:
ranges.forEach(range => { tr.removeMark(range.$from.pos, range.$to.pos) })The original code uses
tr.removeMark(from, to)which removes ALL marks in the range. My code tries to iterate through nodes to find marks, but this approach is more complex and may have bugs.Let me simplify my implementation. Instead of iterating through nodes, I should:
Assistant
Let me run the tests again to see if my fix works.
Assistant
I see the issue now. My implementation is not working. Let me debug by looking at how the original
unsetAllMarksworks and what the problem is. The tests show that marks are NOT being removed at all. Let me check if maybe vitest is using a different (cached) version of the command.Let me verify the file content is correct and check if the vitest is actually using my updated code:
Assistant
I see the issue now. Looking at my code, I'm using
state.doc.nodesBetween(from, to, ...)butstateis actually the state BEFORE the transaction is applied. The problem might be thatstate.docis the old document, and I should be usingtr.docinstead (which is the document being modified by the transaction).But wait, the original code uses
trfor the transaction and just callstr.removeMark(range.$from.pos, range.$to.pos)directly. Let me look more carefully at what's happening.Actually, I think I see another issue. When checking if a mark is clearable, I call
isMarkClearable(mark, markExtensions), butmarkExtensionsis the array of Mark extension objects, not the actual marks in the document. Let me trace through the logic more carefully.Wait, let me add some debug logging to understand what's happening:
Assistant
Interesting! Now I can see what's happening. Looking at the output:
- Selection is from 4 to 10 (not 4 to 13 as I thought - the text "Bold text" is only 9 characters, so positions 4-12)
- After unsetAllMarks, the HTML is
<p><strong>Bol</strong>d text</p>- so the mark was only partially removed!
The problem is that my
nodesBetweenfunction is only iterating over part of the range. Looking at the output, it seems like the selection is at position 4-10 but "Bold text" is 9 characters (positions 4-13), so my selection was wrong.But wait, the bigger issue is that the output shows the document was modified incorrectly - the text "Bol" still has the bold mark but "d text" doesn't. This suggests that
tr.removeMarkis being called but it's not working as expected, or the selection range is wrong.Actually, looking at the JSON output more carefully:
- Before: single text node with "Bold text" and bold mark
- After: two text nodes - "Bol" with bold mark, and "d text" without
This is strange behavior. It seems like the selection is at the wrong position, or my code is not correctly identifying the marks to remove.
Let me check if the selection positions are correct by testing with a simpler selection:
Assistant
I see! The selection is being set to 4-10 (not 4-13 as I specified). This is because
setTextSelection({from: 4, to: 13})is clamping thetovalue to 10 (the end of the text content). Let me check if the issue is with my code or with the selection.Looking at the output:
- Selection is from 4 to 10 (the text "Bold text" ends at position 13, but the selection is clamped)
- After unsetAllMarks, the result shows
<strong>Bol</strong>d text</p>- only "Bol" keeps the mark
This is very strange. It looks like only the first 3 characters of the selection have the mark removed, not all of them. This suggests my
nodesBetweeniteration is not working correctly.Let me add more debug output to understand what's happening in the unsetAllMarks command:
Assistant
Now I can see the issue! Looking at the debug output:
unsetAllMarks DEBUG: node at pos 0 type: paragraph isText: false marks: [] unsetAllMarks DEBUG: node at pos 1 type: text isText: true marks: [ 'bold' ]The problem is that
nodesBetweenis using absolute positions but the document positions are relative to the document root. The text node is at position 1 (relative to the paragraph), but my range is from 4 to 10 (absolute positions in the document).Wait, that doesn't seem right either. Let me think about this...
Actually, looking at the debug output again:
Assistant
Now I understand the issue better. Looking at the output:
range.$from: 4,range.$to: 10nodesBetweenvisits nodes at pos 0 (paragraph) and pos 1 (text)- We're calling
tr.removeMark(from, to, markType)withfrom=4, to=10
But the result shows
<strong>Bol</strong>d text</p>- only the first 3 characters ("Bol") still have bold.The issue is that the text node is at position 1 (in the document), but I'm trying to remove marks from positions 4-10. In ProseMirror, when you call
tr.removeMark(4, 10, boldType), it should remove the bold mark from characters 4-10 within the text node.Assistant
I need to run the test again to see if my new approach works. Let me execute the test command.
Assistant
The result is still the same -
<strong>Bol</strong>d text</p>. This is very strange. Let me check if this is a bug in the originalunsetAllMarksimplementation by reverting to the original code and testing:Assistant
The test is still failing with the same result. Let me try reverting to the original unsetAllMarks code to see if the original implementation works correctly, which would help me understand if there's something wrong with my test setup or the editor state.
Assistant
The user is telling me that no tool call was invoked. I need to run a command to test the original unsetAllMarks implementation to see if it works correctly. Let me run the test again with the original code to verify.
Assistant
Interesting! Even the original
unsetAllMarkshas this behavior. Looking at the debug output, the issue is with my test - the selection{from: 4, to: 13}is clamped to{from: 4, to: 10}, which only selects "d text" not "Bold text". The original code is working correctly!Let me fix the test to properly select all of "Bold text" (which starts at position 1):
Assistant
The user is asking me to continue working on the issue. Let me run the tests with the updated test file that uses
selectAll()instead of specific positions.Assistant
Looking at the test results, I see that the original
unsetAllMarksis not properly removing marks when usingselectAll(). This is becauseselectAll()creates anAllSelectionwhich behaves differently. Let me fix the tests to use proper text selections and also verify my implementation works correctly.First, let me restore the current state of files and understand what's happening:
Assistant
I see - the tests are running against the original code since I reverted with git checkout. The issue is that
selectAll()creates anAllSelectionwhich doesn't work well with mark detection at the selection position. Let me:- Write my implementation again
- Fix the tests to use proper text selections
- Run the tests
Assistant
Let me run the tests to see if my current implementation works:
Assistant
Looking at the test results, my implementation is working for some cases (preserving non-clearable marks, handling clearable as a function). The issue is that
selectAll()creates anAllSelectionwhich behaves differently from aTextSelection. Let me fix the tests to use proper text selection positions.Assistant
Let me run the tests again to see if the updated test file with proper positions works correctly.
Assistant
Looking at the test results, the clearable marks are not being removed. Let me debug my implementation by checking what's happening with
nodesBetweenand the mark detection.Assistant
The debug test passed! The bold mark is being removed correctly. Now let me restore the full test suite and run all tests.
Assistant
The user hasn't said anything new - this appears to be a continuation of my work. Let me run the test suite to see if the tests pass now with the proper implementation.
Assistant
I see the issue now. In the tests, I'm capturing
doc = editor.state.docBEFORE callingunsetAllMarks(), and then checkingdoc.firstChild!.firstChild!AFTER. But since ProseMirror documents are immutable,docstill references the old document. I need to useeditor.state.docAFTER the command runs.Let me fix the tests:
Assistant
Let me run the tests to verify the fix works correctly.
Assistant
All 9 tests are passing. Now let me verify that the existing core tests still pass, and then clean up my test file since I shouldn't add new tests to the repository.
Assistant
All 410 existing tests pass. Let me verify my changes are correct by reviewing the modified files.
Assistant
The previous command output showed the diff for Mark.ts. Now I need to verify the unsetAllMarks.ts changes as well. Let me check the diff for that file.
Assistant
The changes look correct. Let me verify the TypeScript compiles without errors for my modified files:
Assistant
All TypeScript compiles without errors and all existing tests pass. Let me now summarize what I've done and submit.
Summary of Changes
I've implemented the
clearableoption for marks as described in the issue:1. Added
clearableproperty toMarkConfig(packages/core/src/Mark.ts)- New optional property:
clearable?: boolean | (() => boolean) - Defaults to
true(marks are clearable by default) - Can be a boolean or a function that returns a boolean
2. Updated
unsetAllMarkscommand (packages/core/src/commands/unsetAllMarks.ts)- Added optional
optionsparameter withignoreClearable?: boolean - Marks with
clearable: falseare preserved unless is passed
- New optional property:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.