Closed Bug 1695968 Opened 3 years ago Closed 1 year ago

MutationObserver does not report the nodes of a line as added or removed when those nodes are deleted into the previous line

Categories

(Core :: DOM: Editor, defect, P3)

Firefox 86
defect

Tracking

()

RESOLVED FIXED
Tracking Status
firefox115 --- fixed

People

(Reporter: briankimpossible, Unassigned)

References

(Blocks 1 open bug)

Details

Attachments

(1 file)

Attached file mutationobserverbug.html (deleted) —

User Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0 Safari/605.1.15

Steps to reproduce:

Open the attached mutationobserverbug.html file.

mutationobserverbug.html
<!DOCTYPE html>
<html>
<body>
<pre id="editable" contenteditable="true"><div>Line 1</div><div><span>Line 2</span></div></pre>
<script type="module">
const observer = new MutationObserver((records) => {
	console.log(records);
});
observer.observe(document.getElementById("editable"), {
	subtree: true,
	childList: true,
	characterData: true,
});
</script>
</body>
</html>

Put the cursor at the start of the second line and press backspace.
Open the console.
One of the mutation records should have the span which was merged into the previous line in their addedNodes or removedNodes.

Actual results:

When attaching a MutationObserver to a contenteditable element, DOM elements which are merged into a previous line do not show up in any of the mutation records generated for the change. The line which was deleted does not contain any of its original contents, and the DOM nodes are not mentioned in any addedNodes or removedNodes list of any other mutation. In the example, this second line is missing a span but it appears magically in the first line without actually being reported as a mutation.

Expected results:

The span should be reported as having been added to the first line, and removed from the second line.

The Bugbug bot thinks this bug should belong to the 'Core::DOM: Editor' component, and is moving the bug to that component. Please revert this change in case you think the bot is wrong.

Component: General → DOM: Editor

Looking at the mutation records, it's not that the second line merges to the first line, instead the first line merges to the second one. The text node of the first line is removed from the first div, then added to the second div, and then the first div detaches. <span> is never reported because it indeed never moves.
Since the contenteditable spec does not require any specific behavior, I don't think this is a bug. But probably good idea to cross check with Masayuki:

Flags: needinfo?(masayuki)

Looking at the mutation records, it's not that the second line merges to the first line, instead the first line merges to the second one. The text node of the first line is removed from the first div, then added to the second div, and then the first div detaches. <span> is never reported because it indeed never moves.
Since the contenteditable spec does not require any specific behavior, I don't think this is a bug. But probably good idea to cross check with Masayuki:

Yes, you are indeed correct. My confusion was based on the fact that Safari and Chrome both seem to merge the second line into the first line when merging text backwards, while Firefox will merge the first line into the second. I’m not sure either behavior is incorrect, though I find Safari/Chrome’s behavior to be slightly more intuitive given that we’re deleting backwards. Nevertheless, it revealed a critical bug in my text editor implementation so I have Firefox to thank for that.

Thank you for your investigation, Kagami. It sounds like that this is a variation of bug 1449874. I guess that fixing this may be hard because if we forget to change somewhere doing same things, it breaks undo/redo. So, there is a risk of dataloss.

Severity: -- → S3
Status: UNCONFIRMED → NEW
Depends on: 1449874
Ever confirmed: true
Flags: needinfo?(masayuki)
OS: Unspecified → All
Priority: -- → P3
Hardware: Unspecified → All

Is this fixed by bug 1735608

Flags: needinfo?(masayuki)

Yes, the original <span> is now in the joined paragraph.

Status: NEW → RESOLVED
Closed: 1 year ago
Flags: needinfo?(masayuki)
Resolution: --- → FIXED
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Creator:
Created:
Updated:
Size: