Bug 1213589 part.8 When there are no nodes causing text, ContentEventHandler should set start of the editor root to start of the range r=smaug

This commit is contained in:
Masayuki Nakano 2015-12-02 13:20:01 +09:00
parent 98c788bfea
commit b1d47a5bac
2 changed files with 32 additions and 10 deletions

View File

@ -58,7 +58,10 @@ using namespace widget;
// 1.4. Case: <element>[
// When start of a non-empty element is start of a range, start node is
// the element and start offset is 0.
// 1.5. Case: [</root>
// 1.5. Case: <root>[
// When start of a range is 0 and there are no nodes causing text,
// start node is the root node and start offset is 0.
// 1.6. Case: [</root>
// When start of a range is out of bounds, start node is the root node
// and start offset is number of the children.
// 2. End of range:
@ -1001,12 +1004,31 @@ ContentEventHandler::SetRangeFromFlatTextOffset(nsRange* aRange,
}
if (!startSet) {
// Rule #1.5: [</root>
MOZ_ASSERT(!mRootContent->IsNodeOfType(nsINode::eTEXT));
rv = aRange->SetStart(mRootContent,
static_cast<int32_t>(mRootContent->GetChildCount()));
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
if (!offset) {
// Rule #1.5: <root>[</root>
// When there are no nodes causing text, the start of the DOM range
// should be start of the root node since clicking on such editor (e.g.,
// <div contenteditable><span></span></div>) sets caret to the start of
// the editor (i.e., before <span> in the example).
rv = aRange->SetStart(mRootContent, 0);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
if (!aLength) {
rv = aRange->SetEnd(mRootContent, 0);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
return NS_OK;
}
} else {
// Rule #1.5: [</root>
rv = aRange->SetStart(mRootContent,
static_cast<int32_t>(mRootContent->GetChildCount()));
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
}
if (aNewOffset) {
*aNewOffset = offset;

View File

@ -3117,22 +3117,22 @@ function runSetSelectionEventTest()
synthesizeSelectionSet(0, 0);
is(selection.anchorNode, contenteditable,
"runSetSelectionEventTest #11 (0, 0), \"" + contenteditable.innerHTML + "\": selection anchor node should be the root node");
todo_is(selection.anchorOffset, 0,
is(selection.anchorOffset, 0,
"runSetSelectionEventTest #11 (0, 0), \"" + contenteditable.innerHTML + "\": selection anchor offset should be 0");
is(selection.focusNode, contenteditable,
"runSetSelectionEventTest #11 (0, 0), \"" + contenteditable.innerHTML + "\": selection focus node should be the root node");
todo_is(selection.focusOffset, 0,
is(selection.focusOffset, 0,
"runSetSelectionEventTest #11 (0, 0), \"" + contenteditable.innerHTML + "\": selection focus offset should be 0");
checkSelection(0, "", "runSetSelectionEventTest #11 (0, 0), \"" + contenteditable.innerHTML + "\"");
synthesizeSelectionSet(0, 1);
is(selection.anchorNode, contenteditable,
"runSetSelectionEventTest #11 (0, 1), \"" + contenteditable.innerHTML + "\": selection anchor node should be the root node");
todo_is(selection.anchorOffset, 0,
is(selection.anchorOffset, 0,
"runSetSelectionEventTest #11 (0, 1), \"" + contenteditable.innerHTML + "\": selection anchor offset should be 0");
is(selection.focusNode, contenteditable,
"runSetSelectionEventTest #11 (0, 1), \"" + contenteditable.innerHTML + "\": selection focus node should be the root node");
todo_is(selection.focusOffset, contenteditable.childNodes.length,
is(selection.focusOffset, contenteditable.childNodes.length,
"runSetSelectionEventTest #11 (0, 1), \"" + contenteditable.innerHTML + "\": selection focus offset should be the count of the root's children");
checkSelection(0, "", "runSetSelectionEventTest #11 (0, 1), \"" + contenteditable.innerHTML + "\"");