Bug 1482016 - part 1: Create HTMLEditor::SelectContentInternal() for internal use of HTMLEditor::SelectElement() r=m_kato

For making it possible HTMLEditor::SelectElement() to distinguish if it's
called by outer class or editor itself, this patch creates
HTMLEditor::SelectContentInternal().

Differential Revision: https://phabricator.services.mozilla.com/D3001

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Masayuki Nakano 2018-08-13 07:21:28 +00:00
Родитель aa59ff8fc6
Коммит cbb0249edb
3 изменённых файлов: 49 добавлений и 12 удалений

Просмотреть файл

@ -1730,28 +1730,52 @@ HTMLEditor::InsertNodeIntoProperAncestorWithTransaction(
NS_IMETHODIMP
HTMLEditor::SelectElement(Element* aElement)
{
if (NS_WARN_IF(!aElement)) {
return NS_ERROR_INVALID_ARG;
}
RefPtr<Selection> selection = GetSelection();
if (NS_WARN_IF(!selection)) {
return NS_ERROR_FAILURE;
}
nsresult rv = SelectContentInternal(*selection, *aElement);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
return NS_OK;
}
nsresult
HTMLEditor::SelectContentInternal(Selection& aSelection,
nsIContent& aContentToSelect)
{
// Must be sure that element is contained in the document body
if (!IsDescendantOfEditorRoot(aElement)) {
return NS_ERROR_NULL_POINTER;
if (!IsDescendantOfEditorRoot(&aContentToSelect)) {
return NS_ERROR_FAILURE;
}
RefPtr<Selection> selection = GetSelection();
NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
nsINode* parent = aElement->GetParentNode();
nsINode* parent = aContentToSelect.GetParentNode();
if (NS_WARN_IF(!parent)) {
return NS_ERROR_FAILURE;
}
int32_t offsetInParent = parent->ComputeIndexOf(aElement);
// Don't notify selection change at collapse.
AutoUpdateViewBatch notifySelectionChangeOnce(this);
// XXX Perhaps, Selection should have SelectNode(nsIContent&).
int32_t offsetInParent = parent->ComputeIndexOf(&aContentToSelect);
// Collapse selection to just before desired element,
nsresult rv = selection->Collapse(parent, offsetInParent);
if (NS_SUCCEEDED(rv)) {
// then extend it to just after
rv = selection->Extend(parent, offsetInParent + 1);
nsresult rv = aSelection.Collapse(parent, offsetInParent);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
return rv;
// then extend it to just after
rv = aSelection.Extend(parent, offsetInParent + 1);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
return NS_OK;
}
NS_IMETHODIMP

Просмотреть файл

@ -771,6 +771,17 @@ protected: // Shouldn't be used by friend classes
virtual nsresult SelectAllInternal() override;
/**
* SelectContentInternal() sets Selection to aContentToSelect to
* aContentToSelect + 1 in parent of aContentToSelect.
*
* @param aSelection The Selection, callers have to guarantee the
* lifetime.
* @param aContentToSelect The content which should be selected.
*/
nsresult SelectContentInternal(Selection& aSelection,
nsIContent& aContentToSelect);
/**
* PasteInternal() pasts text with replacing selected content.
* This tries to dispatch ePaste event first. If its defaultPrevent() is

Просмотреть файл

@ -3075,7 +3075,9 @@ HTMLEditor::SetSelectionAfterTableEdit(Element* aTable,
if (cell) {
if (aSelected) {
// Reselect the cell
SelectElement(cell);
nsresult rv = SelectContentInternal(*selection, *cell);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
"Failed to select the cell");
return;
}