diff --git a/content/events/src/nsContentEventHandler.cpp b/content/events/src/nsContentEventHandler.cpp index 8b151cb87f96..3d552fd9cd1a 100644 --- a/content/events/src/nsContentEventHandler.cpp +++ b/content/events/src/nsContentEventHandler.cpp @@ -1006,12 +1006,23 @@ static void AdjustRangeForSelection(nsIContent* aRoot, { nsINode* node = *aNode; int32_t offset = *aOffset; - if (aRoot != node && node->GetParent() && - !node->IsNodeOfType(nsINode::eTEXT)) { - node = node->GetParent(); - offset = node->IndexOf(*aNode) + (offset ? 1 : 0); + if (aRoot != node && node->GetParent()) { + if (node->IsNodeOfType(nsINode::eTEXT)) { + // When the offset is at the end of the text node, set it to after the + // text node, to make sure the caret is drawn on a new line when the last + // character of the text node is '\n' + int32_t length = (int32_t)(static_cast(node)->TextLength()); + MOZ_ASSERT(offset <= length, "Offset is past length of text node"); + if (offset == length) { + node = node->GetParent(); + offset = node->IndexOf(*aNode) + 1; + } + } else { + node = node->GetParent(); + offset = node->IndexOf(*aNode) + (offset ? 1 : 0); + } } - + nsIContent* brContent = node->GetChildAt(offset - 1); while (brContent && brContent->IsHTML()) { if (brContent->Tag() != nsGkAtoms::br || IsContentBR(brContent))