Bug 1405039 - Avoid using nsINode::GetChildAt() in HTMLEditor::GetSelectedElement(); r=masayuki

This commit is contained in:
Ehsan Akhgari 2017-09-29 13:56:35 -04:00
parent 5c760977ba
commit a3b87e2b92
3 changed files with 22 additions and 13 deletions

View File

@ -926,8 +926,9 @@ Selection::FocusOffset()
nsIContent*
Selection::GetChildAtAnchorOffset()
{
if (!mAnchorFocusRange)
if (!mAnchorFocusRange) {
return nullptr;
}
if (GetDirection() == eDirNext) {
return mAnchorFocusRange->GetChildAtStartOffset();
@ -936,6 +937,20 @@ Selection::GetChildAtAnchorOffset()
return mAnchorFocusRange->GetChildAtEndOffset();
}
nsIContent*
Selection::GetChildAtFocusOffset()
{
if (!mAnchorFocusRange) {
return nullptr;
}
if (GetDirection() == eDirNext){
return mAnchorFocusRange->GetChildAtEndOffset();
}
return mAnchorFocusRange->GetChildAtStartOffset();
}
static nsresult
CompareToRangeStart(nsINode* aCompareNode, int32_t aCompareOffset,
nsRange* aRange, int32_t* aCmp)

View File

@ -178,6 +178,7 @@ public:
uint32_t FocusOffset();
nsIContent* GetChildAtAnchorOffset();
nsIContent* GetChildAtFocusOffset();
/*
* IsCollapsed -- is the whole selection just one point, or unset?

View File

@ -2378,16 +2378,10 @@ HTMLEditor::GetSelectedElement(const nsAString& aTagName,
// found for any selection that is totally within a link,
// included a collapsed selection (just a caret in a link)
nsCOMPtr<nsINode> anchorNode = selection->GetAnchorNode();
int32_t anchorOffset = -1;
if (anchorNode) {
anchorOffset = selection->AnchorOffset();
}
nsIContent* anchorChild = selection->GetChildAtAnchorOffset();
nsCOMPtr<nsINode> focusNode = selection->GetFocusNode();
int32_t focusOffset = -1;
if (focusNode) {
focusOffset = selection->FocusOffset();
}
nsIContent* focusChild = selection->GetChildAtFocusOffset();
// Link node must be the same for both ends of selection
if (anchorNode) {
@ -2418,12 +2412,11 @@ HTMLEditor::GetSelectedElement(const nsAString& aTagName,
parentLinkOfAnchor.forget(aReturn);
return NS_OK;
}
} else if (anchorOffset >= 0) {
} else if (anchorChild && focusChild) {
// Check if link node is the only thing selected
nsINode* anchorChild = anchorNode->GetChildAt(anchorOffset);
if (anchorChild && HTMLEditUtils::IsLink(anchorChild) &&
if (HTMLEditUtils::IsLink(anchorChild) &&
anchorNode == focusNode &&
focusOffset == anchorOffset + 1) {
focusChild == anchorChild->GetNextSibling()) {
selectedElement = do_QueryInterface(anchorChild);
bNodeFound = true;
}