Bug 336408 - Allow the caret to be positioned at the end of trimmed whitespace, as if the whitespace wasn't trimmed [p=roc r=smontagu sr=mrbkap a=blocking1.9+]

This commit is contained in:
reed@reedloden.com 2007-11-07 20:07:00 -08:00
parent 2c03a2b214
commit 788e2903bf
5 changed files with 144 additions and 1 deletions

View File

@ -68,6 +68,7 @@
#include "nsISelectionController.h"
#include "nsDisplayList.h"
#include "nsCaret.h"
#include "nsTextFrame.h"
// The bidi indicator hangs off the caret to one side, to show which
// direction the typing is in. It needs to be at least 2x2 to avoid looking like
@ -608,6 +609,54 @@ nsCaret::DrawAtPositionWithHint(nsIDOMNode* aNode,
return PR_TRUE;
}
/**
* Find the first frame in an in-order traversal of the frame subtree rooted
* at aFrame which is either a text frame logically at the end of a line,
* or which is aStopAtFrame. Return null if no such frame is found. We don't
* descend into the children of non-eLineParticipant frames.
*/
static nsIFrame*
CheckForTrailingTextFrameRecursive(nsIFrame* aFrame, nsIFrame* aStopAtFrame)
{
if (aFrame == aStopAtFrame ||
((aFrame->GetType() == nsGkAtoms::textFrame &&
(static_cast<nsTextFrame*>(aFrame))->IsAtEndOfLine())))
return aFrame;
if (!aFrame->IsFrameOfType(nsIFrame::eLineParticipant))
return nsnull;
for (nsIFrame* f = aFrame->GetFirstChild(nsnull); f; f = f->GetNextSibling())
{
nsIFrame* r = CheckForTrailingTextFrameRecursive(f, aStopAtFrame);
if (r)
return r;
}
return nsnull;
}
static void
AdjustCaretFrameForLineEnd(nsIFrame** aFrame, PRInt32* aOffset)
{
nsBlockFrame* block = nsLayoutUtils::FindNearestBlockAncestor(*aFrame);
if (!block)
return;
nsLineBox* line = block->FindLineFor(nsLayoutUtils::FindChildContainingDescendant(block, *aFrame));
PRInt32 count = line->GetChildCount();
for (nsIFrame* f = line->mFirstChild; count > 0; --count, f = f->GetNextSibling())
{
nsIFrame* r = CheckForTrailingTextFrameRecursive(f, *aFrame);
if (r == *aFrame)
return;
if (r)
{
*aFrame = r;
NS_ASSERTION(r->GetType() == nsGkAtoms::textFrame, "Expected text frame");
*aOffset = (static_cast<nsTextFrame*>(r))->GetContentEnd();
return;
}
}
}
NS_IMETHODIMP
nsCaret::GetCaretFrameForNodeOffset(nsIContent* aContentNode,
PRInt32 aOffset,
@ -634,6 +683,12 @@ nsCaret::GetCaretFrameForNodeOffset(nsIContent* aContentNode,
if (!theFrame)
return NS_ERROR_FAILURE;
// if theFrame is after a text frame that's logically at the end of the line
// (e.g. if theFrame is a <br> frame), then put the caret at the end of
// that text frame instead. This way, the caret will be positioned as if
// trailing whitespace was not trimmed.
AdjustCaretFrameForLineEnd(&theFrame, &theFrameOffset);
// Mamdouh : modification of the caret to work at rtl and ltr with Bidi
//
// Direction Style from this->GetStyleData()

View File

@ -180,6 +180,12 @@ public:
* should return PR_FALSE if this is not a text frame.
*/
virtual PRBool HasTerminalNewline() const;
/**
* Returns true if this text frame is logically adjacent to the end of the
* line.
*/
PRBool IsAtEndOfLine() const;
#ifdef ACCESSIBILITY
NS_IMETHOD GetAccessible(nsIAccessible** aAccessible);

View File

@ -4457,7 +4457,7 @@ nsTextFrame::PeekOffsetCharacter(PRBool aForward, PRInt32* aOffset)
if (!mTextRun)
return PR_FALSE;
TrimmedOffsets trimmed = GetTrimmedOffsets(mContent->GetText(), PR_TRUE);
TrimmedOffsets trimmed = GetTrimmedOffsets(mContent->GetText(), PR_FALSE);
// A negative offset means "end of frame".
PRInt32 startOffset = GetContentOffset() + (*aOffset < 0 ? contentLength : *aOffset);
@ -5889,3 +5889,9 @@ nsTextFrame::HasTerminalNewline() const
{
return ::HasTerminalNewline(this);
}
PRBool
nsTextFrame::IsAtEndOfLine() const
{
return (GetStateBits() & TEXT_END_OF_LINE) != 0;
}

View File

@ -55,6 +55,7 @@ _TEST_FILES = test_bug323656.html \
test_bug392923.html \
test_bug394173.html \
test_bug394239.html \
test_character_movement.html \
test_word_movement.html \
$(NULL)

View File

@ -0,0 +1,75 @@
<!DOCTYPE HTML>
<html>
<head>
<title>Test Character Movement (including nsTextFrame::PeekOffsetCharacter)</title>
<script type="text/javascript" src="/MochiKit/MochiKit.js"></script>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<script type="application/javascript" src="/tests/SimpleTest/EventUtils.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<p id="display"></p>
<div id="content" style="display: block">
<div contentEditable id="editor"></div>
</div>
<pre id="test">
<script class="testbody" type="text/javascript;version=1.7">
SimpleTest.waitForExplicitFinish();
// This seems to be necessary because the selection is not set up properly otherwise
setTimeout(test, 0);
function test() {
var sel = window.getSelection();
var editor = document.getElementById("editor");
function testRight(node, offset) {
synthesizeKey("VK_RIGHT", {});
is(sel.anchorNode, node, "Right movement broken in " + editor.innerHTML);
is(sel.anchorOffset, offset, "Right movement broken in " + editor.innerHTML);
}
function testLeft(node, offset) {
synthesizeKey("VK_LEFT", {});
is(sel.anchorNode, node, "Left movement broken in " + editor.innerHTML);
is(sel.anchorOffset, offset, "Left movement broken in " + editor.innerHTML);
}
editor.innerHTML = "H K";
sel.collapse(editor.firstChild, 0);
testRight(editor.firstChild, 1);
testRight(editor.firstChild, 2);
testRight(editor.firstChild, 3);
testLeft(editor.firstChild, 2);
testLeft(editor.firstChild, 1);
testLeft(editor.firstChild, 0);
editor.innerHTML = "<b>H</b> K";
sel.collapse(editor.firstChild.firstChild, 0);
testRight(editor.firstChild.firstChild, 1);
testRight(editor.firstChild.nextSibling, 1);
testRight(editor.firstChild.nextSibling, 2);
testLeft(editor.firstChild.nextSibling, 1);
testLeft(editor.firstChild.nextSibling, 0);
testLeft(editor.firstChild.firstChild, 0);
editor.innerHTML = "H <br>K";
sel.collapse(editor.firstChild, 0);
testRight(editor.firstChild, 1);
testRight(editor.firstChild, 2);
testRight(editor.firstChild.nextSibling.nextSibling, 0);
testRight(editor.firstChild.nextSibling.nextSibling, 1);
testLeft(editor.firstChild.nextSibling.nextSibling, 0);
testLeft(editor, 1);
testLeft(editor.firstChild, 1);
testLeft(editor.firstChild, 0);
SimpleTest.finish();
}
</script>
</pre>
</body>
</html>