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 start by exploring the repository structure to understand the codebase.
Assistant
Now let me look at how marks are managed in the ExtensionManager.
Assistant
Let me look at the existing tests for unsetAllMarks:
Assistant
Now let me look at the Editor class to understand how it works:
Assistant
Now let me check the comment extension to understand the issue:
Assistant
Let me look at a similar extension like the mention or unique-id extension:
Assistant
Now let me look at the tests for the unsetAllMarks:
Assistant
Let me look at how setMark and other commands are structured to understand the patterns:
Assistant
Now I have a good understanding of the codebase. Let me explore the helpers for callOrReturn to see if it's available:
Assistant
Now let me check the Editor's extension manager to understand how I can access extensions from a command:
Assistant
Now I understand the pattern. Let me look at the CommandProps to see what's available:
Assistant
Good, we have
editor.extensionManageravailable. Let me look at the editor'scanfunction:Assistant
Now I have a clear understanding. Let me also look at how
keepOnSplit(a similar boolean config option) is implemented:Assistant
Good. Now let me explore the schema to see what types are available:
Assistant
Now I have a complete understanding. Let me think through the design:
Plan
-
Add
clearableoption toMarkConfig: This option will default totrue. Iffalse, the mark will not be removed byunsetAllMarks. -
Update
unsetAllMarkscommand:- Accept an
options?: { ignoreClearable?: boolean }parameter. - Get the list of "non-clearable" mark types (where
clearable === false) from the extensions. - Remove all marks EXCEPT those non-clearable marks, unless
ignoreClearable: true.
- Accept an
-
Update
can().unsetAllMarks():- Return
truewhen selection contains only non-clearable marks (this is a successful no-op).
- Return
Let me also check the existing
unsetMarkcommand to understand how the "explicit call" should work:The contract says: "explicitly calling
unsetMarkmay still remove a non-clearable mark." This meansunsetMarkshould not be affected byclearable. Let me verify this:-
Assistant
unsetMarkalready removes a specific mark type directly, so it works regardless ofclearable. Good. Now let me create a script to reproduce the issue first:Assistant
Let me look at the existing test setup to know how to write a reproduction:
Assistant
Let me check how to set up a temporary test:
Assistant
Now let me create a temporary reproduction script:
Assistant
Now let me write a reproduction test in the proper location:
Assistant
Let me create a temporary reproduction test in the existing test directory:
Assistant
The dist directory doesn't exist. Let me look at the import differently:
Assistant
Let me check how core is imported in tests:
Assistant
Let me look at how Editor is imported in this test - they use the Editor from @tiptap/core. Now let me update my test:
Assistant
I confirmed the bug. The current implementation removes all marks, including the non-clearable "comment" mark. Now let me implement the fix.
Plan
-
Add
clearableoption toMarkConfig: Defaults totrue. Can bebooleanor() => boolean. -
Update
unsetAllMarkscommand:- Accept
options?: { ignoreClearable?: boolean }. - Get the list of non-clearable mark types from the extensions.
- When dispatching, remove all marks, then re-add the non-clearable marks (or use a more efficient approach).
- When
ignoreClearable: true, remove all marks.
- Accept
-
Update
can().unsetAllMarks():- Return
truewhen selection contains only non-clearable marks.
- Return
-
Assistant
The
tr.removeMark(from, to, type)removes only a specific mark type. So one approach is to:- Get the list of all mark types in the selection.
- For each one, if it's clearable OR
ignoreClearableis true, calltr.removeMarkfor it.
Actually, a better approach is to:
- Find the non-clearable marks in the selection.
- Remove all marks.
- Re-add the non-clearable marks.
But the simpler approach is to iterate over mark types and only remove the clearable ones. Let me think about this more.
Assistant
Assistant
Now let me add the
clearableoption:Assistant
Oops, the editor opened the test file by default. Let me explicitly open the Mark.ts file:
Assistant
Good, Mark.ts still has its original content. Now let me update Mark.ts:
Assistant
I see there's a duplicate "Keep mark after split node" comment. Let me look at the current state and fix:
Assistant
Let me fix the file:
Assistant
I see the issue - the
inclusive?:line is now missing. Let me restore it:Assistant
The file looks correct now. Let me verify by running a quick check:
Assistant
Mark.ts has the right changes. Now let me update
unsetAllMarks.ts:Assistant
Let me think about the approach. Looking at the helpers directory, let me check if there's a helper to get mark types for a given selection:
Assistant
Now let me also think about a more efficient way. The
tr.removeMark(from, to)call removes all marks in a range. To keep non-clearable marks, we have a few options:Option A: Iterate over mark types in the selection and remove only the clearable ones.
- Pros: Clean, simple, efficient.
- Cons: Requires iterating over the document to find mark types.
Option B: Remove all marks, then re-add the non-clearable marks with their positions.
- Pros: Single removal call.
- Cons: Requires saving mark data and re-adding, more complex.
I'll go with Option A.
Let me also think about where to place the helper for getting non-clearable mark types. It could be in the editor's extensionManager (similar to
splittableMarks), or as a local helper. Let me go with a local helper since this is only used in one place.Actually, the cleanest is to add this to the extension manager similar to
splittableMarks. Let me look:Assistant
Now let me think about the best approach.
Approach: I'll add a
nonClearableMarkTypesgetter (or method) to the extension manager that returns the set of MarkType objects for marks whereclearableisfalse. This is similar tosplittableMarks. Then in the command, I'll use this to filter marks to remove.Wait, actually, instead of caching the types in the extension manager, I can just check at command time by looking at the extensions. Let me make a local helper.
Actually, let me follow the existing pattern and add it to ExtensionManager. The advantage is that the type resolution (using
getMarkType) is done once at editor init time.Let me first explore the existing patterns:
Assistant
OK, I'll follow the same pattern. Let me add a
nonClearableMarks(or similar) to the ExtensionManager. But, since the schema types are easier to use here, let me store the mark names. Then in the command, we can resolve them to MarkType.Actually, let me make it simpler. I'll add a getter
nonClearableMarkTypes: MarkType[]that returns the actual MarkType objects. This way, we don't need to resolve them every time the command is called.Wait, but the schema is created at editor init time, so the MarkType objects are stable. Let me look at how
getSchemaTypeByNameis used:Assistant
Now let me think about the design more carefully. Looking at the
splittableMarksarray, it just stores the names. The commands then look up the types when needed.I'll do the same: add a
nonClearableMarkTypes: MarkType[]array. Or actually, I think it's cleaner to have a method/getter that returns a set of names:nonClearableMarkNames: string[]. Then in the command, we use it for filtering.But we have a list of mark types (MarkType objects) in the range, not names. So it's more natural to compare MarkType to MarkType.
Let me just store the MarkType objects directly in the ExtensionManager.
Let me implement this:
Assistant
Now let me find the setupExtensions method and add the logic for nonClearableMarks:
Assistant
I see there's an issue - the context block was removed. Let me fix:
Assistant
I notice there's a missing blank line. Let me fix:
Assistant
Now let me verify the diff with the original:
Assistant
The diff is clean. Now let me also check the
destroy()method to ensure it clearsnonClearableMarks:Assistant
The
splittableMarksarray is not explicitly cleared in destroy either, so we don't need to worry about that.Now let me update
unsetAllMarks:Assistant
Let me verify the file:
Assistant
Now let me run the reproduction test:
Assistant
My test file got corrupted. Let me restore it:
Assistant
I see what happened - the test file got corrupted earlier when I was editing Mark.ts. Let me restore it:
Assistant
All 6 tests pass. Let me also test some edge cases:
- Empty selection
- Selection with multiple ranges
- Selection with only non-clearable marks
- Marks spanning across multiple text nodes
Assistant
Let me check why the multi-range test failed. The test is meant to select only "bold1" and "bold2" - not the comment. But the strong tags remain, which means the selection didn't include them.
Let me debug this by looking at the actual positions. The issue is that I'm trying to find the text positions after parsing, but the parsing might split things. Let me simplify the test:
Assistant
All 9 tests pass. Now let me also run the existing tests to make sure I didn't break anything:
Assistant
All 419 existing tests pass. Now let me also run tests for the other packages to make sure I didn't break anything:
Assistant
Good, no italic/strike tests. Now let me think about additional edge cases:
-
Storage changes: When
clearableis changed dynamically viaconfigure(), thenonClearableMarksarray won't be updated. Let me check if this is a problem. Looking at the existingsplittableMarkspattern, it's computed once at setup time. So this is consistent behavior. -
Mark types from outside the schema: Not a concern since we use the schema.
-
The behavior of
tr.removeMarkwith multiple ranges: Let me re-verify.
Wait, let me also think about another scenario. What if the user has a non-clearable mark that is a parent of a clearable mark in the same span? Let me think...
For example:
<p><span data-comment="true"><strong>bold comment</strong></span></p>In this case, the text "bold comment" has both a
commentmark (non-clearable) and aboldmark (clearable). If we rununsetAllMarks(), we want to remove the bold mark but keep the comment mark.Let me add a test:
-
Assistant
All 12 tests pass. Now let me look at how I need to also make sure the changes I made are coherent with the rest of the codebase. Let me look at the existing
unsetAllMarkstests in the demos:Assistant
Good, the demo uses
unsetAllMarks()with no arguments, which still works.Now let me think about what else I need to do. Let me check if there are any other places that might use
unsetAllMarks:Assistant
Let me also think about what happens if the user has a complex scenario where the same mark is on multiple text nodes, and we want to handle that properly. Let me also add a test for the chained command:
Assistant
All 14 tests pass. Now let me also check that the original behavior (no
clearableset) is unchanged. Let me also think about a few more edge cases:- What if
clearable: undefined?: This should be treated astrue. Let me verify my implementation handles this correctly.
Looking at my code:
const clearable = callOrReturn(getExtensionField(extension, 'clearable', context)) ?? trueThis is correct. If
clearableisundefined, thengetExtensionFieldreturnsundefined, thencallOrReturn(undefined)returnsundefined, thenundefined ?? trueistrue.- What if
clearable: null?: This would be treated asnull(falsy). Hmm, that's not ideal. Let me check whatgetExtensionFieldreturns when the value isnull:
- What if
Assistant
Looking at
getExtensionField:- It first checks if the field is
undefinedand if so, recursively looks at the parent. - If it's a function, it binds it and returns the function (not the result).
- Otherwise, returns the value.
So if
clearable: null, it would be returned asnull. ThencallOrReturn(null)returnsnull. Thennull ?? trueistrue.Wait, let me check
callOrReturnmore carefully:- It first checks if the field is
Assistant
OK,
callOrReturn(null)returnsnull. Sonull ?? truewould betrue.But there's a subtle issue: when
getExtensionFieldfinds a function, it returns the function. So my code does:const clearable = callOrReturn(getExtensionField(extension, 'clearable', context)) ?? trueFor
clearable: () => false,getExtensionFieldreturns the function, thencallOrReturncalls it (without context since we don't pass one), which returnsfalse. Thenfalse ?? trueisfalse.Wait, but I do call
getExtensionField(extension, 'clearable', context). Thecontextis the extension context, but I'm callingcallOrReturnwithout passing the context. So the function will be called without athiscontext.Let me check how
splittableMarkshandles this:Assistant
Same pattern. OK, the pattern is correct.
Wait, but there's a subtle issue.
getExtensionFieldreturns the function bound to context when the value is a function. So:getExtensionField(extension, 'keepOnSplit', context)returns a bound function.callOrReturn(boundFunction)calls the bound function.- The bound function has
thisset to the context.
So even though I'm not passing the context to
callOrReturn, the context is already bound. Let me verify by re-readinggetExtensionField:if (typeof extension.config[field as keyof typeof extension.config] === 'function') { const value = (extension.config[field as keyof typeof extension.config] as any).bind({ ...context, parent: extension.parent ? getExtensionField(extension.parent, field, context) : null, }) return value }Assistant
The fast path works. Now let me delete this temporary test:
Assistant
Let me also look at the Mark.ts to make sure my changes are clean:
Assistant
Let me also check the ExtensionManager changes:
Assistant
Now let me look at the unsetAllMarks:
Assistant
Now let me run all the tests one more time to make sure everything still passes:
Assistant
All 424 tests pass (including my 12 new tests + 9 + 2 = 14, but only 12 are in the spec). Let me check the count:
Assistant
14 new tests, all pass. Let me also run the tests for other packages:
Assistant
Let me also test the wider scope of packages:
Assistant
All 1078 tests pass. Now let me look at the final state of my changes:
Assistant
The changes look clean. Let me also clean up my test file since I'm not supposed to add tests:
Assistant
Now let me run the existing tests one more time to make sure everything still passes:
Assistant
All 410 existing tests still pass. Let me now also revert the file mode changes if they're not necessary:
Assistant
Let me revert the file permissions to match the original style:
Assistant
The changes look clean and focused. Let me also write a final temporary script to verify the fix works for the original issue from the bug report:
Assistant
The fix works as expected. The HTML after clearing formatting shows:
- The
<strong>tag has been removed (bold formatting cleared) - The
<span data-comment="true">commented section</span>is preserved
Let me clean up the verification test:
- The
Assistant
Now let me also check if there's a CHANGELOG that needs updating:
Assistant
The task instructions don't mention creating changesets, and since I'm not supposed to add tests, I won't add a changeset either. Let me check the diff one more time:
Assistant
Let me also do a final check with the wider test suite:
Assistant
Assistant
All 7 final tests pass. Let me clean up:
Assistant
Let me also verify the final state of the changes:
Assistant
Only the three intended files are modified. Let me run a complete final test pass to make sure everything is good:
Assistant
All 1064 tests pass.
Now let me also go through the checklist provided:
Final Checklist
-
If an error was reported in the issue description, I have successfully reproduced it.
- Yes, I created a reproduction script that confirmed the bug:
unsetAllMarkswas deleting non-clearable marks like comments.
- Yes, I created a reproduction script that confirmed the bug:
-
If an error was reported in the issue description, I have confirmed that my fix resolves the error.
- Yes, my fix adds a
clearableconfiguration option to marks (defaulting totrue), andunsetAllMarksnow respects this option. Marks withclearable: falsesurviveunsetAllMarks().
- Yes, my fix adds a
-
I have thought about edge cases and made sure my fix handles them as well.
- Yes, I tested the following edge cases:
- Empty selection
- Selection with only non-clearable marks (no-op)
- Yes, I tested the following edge cases:
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.