From caabfdcd8b737e835abf33a1132d62a4009737ee Mon Sep 17 00:00:00 2001 From: Jim Chen Date: Wed, 26 Jun 2013 17:28:20 -0400 Subject: [PATCH] Bug 864040 - Set range to after text node when offset is at the end of text node; r=masayuki --- content/events/src/nsContentEventHandler.cpp | 21 +++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) 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))