Bug 1348851 - Use new block when better selection isn't found.

Tag #1375
This commit is contained in:
Matt A. Tobin 2020-04-17 05:27:56 -04:00 committed by Roy Tam
commit 9220996e92
3 changed files with 35 additions and 11 deletions

View file

@ -7179,15 +7179,18 @@ HTMLEditRules::PinSelectionToNewBlock(Selection* aSelection)
return NS_OK;
}
if (NS_WARN_IF(!mNewBlock)) {
return NS_ERROR_NULL_POINTER;
}
// get the (collapsed) selection location
nsCOMPtr<nsIDOMNode> selNode, temp;
nsCOMPtr<nsIDOMNode> selNode;
int32_t selOffset;
NS_ENSURE_STATE(mHTMLEditor);
nsresult rv =
mHTMLEditor->GetStartNodeAndOffset(aSelection,
getter_AddRefs(selNode), &selOffset);
NS_ENSURE_SUCCESS(rv, rv);
temp = selNode;
// use ranges and sRangeHelper to compare sel point to new block
nsCOMPtr<nsINode> node = do_QueryInterface(selNode);
@ -7197,24 +7200,23 @@ HTMLEditRules::PinSelectionToNewBlock(Selection* aSelection)
NS_ENSURE_SUCCESS(rv, rv);
rv = range->SetEnd(selNode, selOffset);
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIContent> block = mNewBlock.get();
NS_ENSURE_TRUE(block, NS_ERROR_NO_INTERFACE);
bool nodeBefore, nodeAfter;
rv = nsRange::CompareNodeToRange(block, range, &nodeBefore, &nodeAfter);
rv = nsRange::CompareNodeToRange(mNewBlock, range, &nodeBefore, &nodeAfter);
NS_ENSURE_SUCCESS(rv, rv);
if (nodeBefore && nodeAfter) {
return NS_OK; // selection is inside block
} else if (nodeBefore) {
// selection is after block. put at end of block.
nsCOMPtr<nsIDOMNode> tmp = GetAsDOMNode(mNewBlock);
NS_ENSURE_STATE(mHTMLEditor);
tmp = GetAsDOMNode(mHTMLEditor->GetLastEditableChild(*block));
nsCOMPtr<nsINode> tmp = mHTMLEditor->GetLastEditableChild(*mNewBlock);
if (!tmp) {
tmp = mNewBlock;
}
uint32_t endPoint;
if (mHTMLEditor->IsTextNode(tmp) ||
mHTMLEditor->IsContainer(tmp)) {
rv = EditorBase::GetLengthOfDOMNode(tmp, endPoint);
NS_ENSURE_SUCCESS(rv, rv);
endPoint = tmp->Length();
} else {
tmp = EditorBase::GetNodeLocation(tmp, (int32_t*)&endPoint);
endPoint++; // want to be after this node
@ -7222,9 +7224,11 @@ HTMLEditRules::PinSelectionToNewBlock(Selection* aSelection)
return aSelection->Collapse(tmp, (int32_t)endPoint);
} else {
// selection is before block. put at start of block.
nsCOMPtr<nsIDOMNode> tmp = GetAsDOMNode(mNewBlock);
NS_ENSURE_STATE(mHTMLEditor);
tmp = GetAsDOMNode(mHTMLEditor->GetFirstEditableChild(*block));
nsCOMPtr<nsINode> tmp = mHTMLEditor->GetFirstEditableChild(*mNewBlock);
if (!tmp) {
tmp = mNewBlock;
}
int32_t offset;
if (mHTMLEditor->IsTextNode(tmp) ||
mHTMLEditor->IsContainer(tmp)) {