Bug 1730442 - part 2: Rewrite HTMLEditor::GetSelectedOrParentTableElement() as returing Result<RefPtr<Element>, nsresult> r=m_kato

Depends on D146359

Differential Revision: https://phabricator.services.mozilla.com/D146360
This commit is contained in:
Masayuki Nakano
2022-05-17 00:12:09 +00:00
parent c74c282325
commit d035d0bdc4
3 changed files with 46 additions and 54 deletions

View File

@@ -2352,17 +2352,15 @@ nsresult HTMLEditor::GetHTMLBackgroundColorState(bool* aMixed,
*aMixed = false; *aMixed = false;
aOutColor.Truncate(); aOutColor.Truncate();
ErrorResult error; Result<RefPtr<Element>, nsresult> cellOrRowOrTableElementOrError =
RefPtr<Element> cellOrRowOrTableElement = GetSelectedOrParentTableElement();
GetSelectedOrParentTableElement(error); if (cellOrRowOrTableElementOrError.isErr()) {
if (error.Failed()) { NS_WARNING("HTMLEditor::GetSelectedOrParentTableElement() returned error");
NS_WARNING( return cellOrRowOrTableElementOrError.unwrapErr();
"HTMLEditor::GetSelectedOrParentTableElement() returned nullptr");
return error.StealNSResult();
} }
for (RefPtr<Element> element = std::move(cellOrRowOrTableElement); element; for (RefPtr<Element> element = cellOrRowOrTableElementOrError.unwrap();
element = element->GetParentElement()) { element; element = element->GetParentElement()) {
// We are in a cell or selected table // We are in a cell or selected table
element->GetAttr(kNameSpaceID_None, nsGkAtoms::bgcolor, aOutColor); element->GetAttr(kNameSpaceID_None, nsGkAtoms::bgcolor, aOutColor);
@@ -3297,19 +3295,18 @@ nsresult HTMLEditor::SetHTMLBackgroundColorWithTransaction(
MOZ_ASSERT(IsEditActionDataAvailable()); MOZ_ASSERT(IsEditActionDataAvailable());
// Find a selected or enclosing table element to set background on // Find a selected or enclosing table element to set background on
ErrorResult error;
bool isCellSelected = false; bool isCellSelected = false;
RefPtr<Element> cellOrRowOrTableElement = Result<RefPtr<Element>, nsresult> cellOrRowOrTableElementOrError =
GetSelectedOrParentTableElement(error, &isCellSelected); GetSelectedOrParentTableElement(&isCellSelected);
if (error.Failed()) { if (cellOrRowOrTableElementOrError.isErr()) {
NS_WARNING("HTMLEditor::GetSelectedOrParentTableElement() failed"); NS_WARNING("HTMLEditor::GetSelectedOrParentTableElement() failed");
return error.StealNSResult(); return cellOrRowOrTableElementOrError.unwrapErr();
} }
bool setColor = !aColor.IsEmpty(); bool setColor = !aColor.IsEmpty();
RefPtr<Element> rootElementOfBackgroundColor; RefPtr<Element> rootElementOfBackgroundColor =
if (cellOrRowOrTableElement) { cellOrRowOrTableElementOrError.unwrap();
rootElementOfBackgroundColor = std::move(cellOrRowOrTableElement); if (rootElementOfBackgroundColor) {
// Needs to set or remove background color of each selected cell elements. // Needs to set or remove background color of each selected cell elements.
// Therefore, just the cell contains selection range, we don't need to // Therefore, just the cell contains selection range, we don't need to
// do this. Note that users can select each cell, but with Selection API, // do this. Note that users can select each cell, but with Selection API,

View File

@@ -3243,8 +3243,8 @@ class HTMLEditor final : public EditorBase,
* In #1 and #4, *aIsCellSelected will be set to true (i.e,, when * In #1 and #4, *aIsCellSelected will be set to true (i.e,, when
* a selection range selects a cell element). * a selection range selects a cell element).
*/ */
already_AddRefed<Element> GetSelectedOrParentTableElement( Result<RefPtr<Element>, nsresult> GetSelectedOrParentTableElement(
ErrorResult& aRv, bool* aIsCellSelected = nullptr) const; bool* aIsCellSelected = nullptr) const;
/** /**
* PasteInternal() pasts text with replacing selected content. * PasteInternal() pasts text with replacing selected content.

View File

@@ -3867,30 +3867,28 @@ nsresult HTMLEditor::GetCellContext(Element** aTable, Element** aCell,
// or get the enclosing by a cell // or get the enclosing by a cell
if (!cell) { if (!cell) {
// Find a selected or enclosing table element // Find a selected or enclosing table element
ErrorResult error; Result<RefPtr<Element>, nsresult> cellOrRowOrTableElementOrError =
RefPtr<Element> cellOrRowOrTableElement = GetSelectedOrParentTableElement();
GetSelectedOrParentTableElement(error); if (cellOrRowOrTableElementOrError.isErr()) {
if (error.Failed()) {
NS_WARNING("HTMLEditor::GetSelectedOrParentTableElement() failed"); NS_WARNING("HTMLEditor::GetSelectedOrParentTableElement() failed");
return error.StealNSResult(); return cellOrRowOrTableElementOrError.unwrapErr();
} }
if (!cellOrRowOrTableElement) { if (!cellOrRowOrTableElementOrError.inspect()) {
return NS_SUCCESS_EDITOR_ELEMENT_NOT_FOUND; return NS_SUCCESS_EDITOR_ELEMENT_NOT_FOUND;
} }
if (cellOrRowOrTableElement->IsHTMLElement(nsGkAtoms::table)) { if (HTMLEditUtils::IsTable(cellOrRowOrTableElementOrError.inspect())) {
// We have a selected table, not a cell // We have a selected table, not a cell
if (aTable) { if (aTable) {
cellOrRowOrTableElement.forget(aTable); cellOrRowOrTableElementOrError.unwrap().forget(aTable);
} }
return NS_OK; return NS_OK;
} }
if (!cellOrRowOrTableElement->IsAnyOfHTMLElements(nsGkAtoms::td, if (!HTMLEditUtils::IsTableCell(cellOrRowOrTableElementOrError.inspect())) {
nsGkAtoms::th)) {
return NS_SUCCESS_EDITOR_ELEMENT_NOT_FOUND; return NS_SUCCESS_EDITOR_ELEMENT_NOT_FOUND;
} }
// We found a cell // We found a cell
cell = std::move(cellOrRowOrTableElement); cell = cellOrRowOrTableElementOrError.unwrap();
} }
if (aCell) { if (aCell) {
// we don't want to cell.forget() here, because we use it below. // we don't want to cell.forget() here, because we use it below.
@@ -4113,16 +4111,18 @@ NS_IMETHODIMP HTMLEditor::GetSelectedOrParentTableElement(
} }
bool isCellSelected = false; bool isCellSelected = false;
ErrorResult aRv; Result<RefPtr<Element>, nsresult> cellOrRowOrTableElementOrError =
RefPtr<Element> cellOrRowOrTableElement = GetSelectedOrParentTableElement(&isCellSelected);
GetSelectedOrParentTableElement(aRv, &isCellSelected); if (cellOrRowOrTableElementOrError.isErr()) {
if (aRv.Failed()) {
NS_WARNING("HTMLEditor::GetSelectedOrParentTableElement() failed"); NS_WARNING("HTMLEditor::GetSelectedOrParentTableElement() failed");
return EditorBase::ToGenericNSResult(aRv.StealNSResult()); return EditorBase::ToGenericNSResult(
cellOrRowOrTableElementOrError.unwrapErr());
} }
if (!cellOrRowOrTableElement) { if (!cellOrRowOrTableElementOrError.inspect()) {
return NS_OK; return NS_OK;
} }
RefPtr<Element> cellOrRowOrTableElement =
cellOrRowOrTableElementOrError.unwrap();
if (isCellSelected) { if (isCellSelected) {
aTagName.AssignLiteral("td"); aTagName.AssignLiteral("td");
@@ -4131,22 +4131,21 @@ NS_IMETHODIMP HTMLEditor::GetSelectedOrParentTableElement(
return NS_OK; return NS_OK;
} }
if (cellOrRowOrTableElement->IsAnyOfHTMLElements(nsGkAtoms::td, if (HTMLEditUtils::IsTableCell(cellOrRowOrTableElement)) {
nsGkAtoms::th)) {
aTagName.AssignLiteral("td"); aTagName.AssignLiteral("td");
// Keep *aSelectedCount as 0. // Keep *aSelectedCount as 0.
cellOrRowOrTableElement.forget(aCellOrRowOrTableElement); cellOrRowOrTableElement.forget(aCellOrRowOrTableElement);
return NS_OK; return NS_OK;
} }
if (cellOrRowOrTableElement->IsHTMLElement(nsGkAtoms::table)) { if (HTMLEditUtils::IsTable(cellOrRowOrTableElement)) {
aTagName.AssignLiteral("table"); aTagName.AssignLiteral("table");
*aSelectedCount = 1; *aSelectedCount = 1;
cellOrRowOrTableElement.forget(aCellOrRowOrTableElement); cellOrRowOrTableElement.forget(aCellOrRowOrTableElement);
return NS_OK; return NS_OK;
} }
if (cellOrRowOrTableElement->IsHTMLElement(nsGkAtoms::tr)) { if (HTMLEditUtils::IsTableRow(cellOrRowOrTableElement)) {
aTagName.AssignLiteral("tr"); aTagName.AssignLiteral("tr");
*aSelectedCount = 1; *aSelectedCount = 1;
cellOrRowOrTableElement.forget(aCellOrRowOrTableElement); cellOrRowOrTableElement.forget(aCellOrRowOrTableElement);
@@ -4157,19 +4156,16 @@ NS_IMETHODIMP HTMLEditor::GetSelectedOrParentTableElement(
return NS_ERROR_UNEXPECTED; return NS_ERROR_UNEXPECTED;
} }
already_AddRefed<Element> HTMLEditor::GetSelectedOrParentTableElement( Result<RefPtr<Element>, nsresult> HTMLEditor::GetSelectedOrParentTableElement(
ErrorResult& aRv, bool* aIsCellSelected /* = nullptr */) const { bool* aIsCellSelected /* = nullptr */) const {
MOZ_ASSERT(IsEditActionDataAvailable()); MOZ_ASSERT(IsEditActionDataAvailable());
MOZ_ASSERT(!aRv.Failed());
if (aIsCellSelected) { if (aIsCellSelected) {
*aIsCellSelected = false; *aIsCellSelected = false;
} }
if (NS_WARN_IF(!SelectionRef().RangeCount())) { if (NS_WARN_IF(!SelectionRef().RangeCount())) {
aRv.Throw(NS_ERROR_FAILURE); // XXX Shouldn't throw an exception? return Err(NS_ERROR_FAILURE); // XXX Shouldn't throw an exception?
return nullptr;
} }
// Try to get the first selected cell, first. // Try to get the first selected cell, first.
@@ -4179,13 +4175,12 @@ already_AddRefed<Element> HTMLEditor::GetSelectedOrParentTableElement(
if (aIsCellSelected) { if (aIsCellSelected) {
*aIsCellSelected = true; *aIsCellSelected = true;
} }
return cellElement.forget(); return cellElement;
} }
const RangeBoundary& anchorRef = SelectionRef().AnchorRef(); const RangeBoundary& anchorRef = SelectionRef().AnchorRef();
if (NS_WARN_IF(!anchorRef.IsSet())) { if (NS_WARN_IF(!anchorRef.IsSet())) {
aRv.Throw(NS_ERROR_FAILURE); return Err(NS_ERROR_FAILURE);
return nullptr;
} }
// If anchor selects a <td>, <table> or <tr>, return it. // If anchor selects a <td>, <table> or <tr>, return it.
@@ -4201,17 +4196,17 @@ already_AddRefed<Element> HTMLEditor::GetSelectedOrParentTableElement(
if (aIsCellSelected) { if (aIsCellSelected) {
*aIsCellSelected = true; *aIsCellSelected = true;
} }
return do_AddRef(selectedContent->AsElement()); return RefPtr<Element>(selectedContent->AsElement());
} }
if (selectedContent->IsAnyOfHTMLElements(nsGkAtoms::table, if (selectedContent->IsAnyOfHTMLElements(nsGkAtoms::table,
nsGkAtoms::tr)) { nsGkAtoms::tr)) {
return do_AddRef(selectedContent->AsElement()); return RefPtr<Element>(selectedContent->AsElement());
} }
} }
} }
if (NS_WARN_IF(!anchorRef.Container()->IsContent())) { if (NS_WARN_IF(!anchorRef.Container()->IsContent())) {
return nullptr; return RefPtr<Element>();
} }
// Then, look for a cell element (either <td> or <th>) which contains // Then, look for a cell element (either <td> or <th>) which contains
@@ -4219,11 +4214,11 @@ already_AddRefed<Element> HTMLEditor::GetSelectedOrParentTableElement(
cellElement = GetInclusiveAncestorByTagNameInternal( cellElement = GetInclusiveAncestorByTagNameInternal(
*nsGkAtoms::td, *anchorRef.Container()->AsContent()); *nsGkAtoms::td, *anchorRef.Container()->AsContent());
if (!cellElement) { if (!cellElement) {
return nullptr; // Not in table. return RefPtr<Element>(); // Not in table.
} }
// Don't set *aIsCellSelected to true in this case because it does NOT // Don't set *aIsCellSelected to true in this case because it does NOT
// select a cell, just in a cell. // select a cell, just in a cell.
return cellElement.forget(); return cellElement;
} }
NS_IMETHODIMP HTMLEditor::GetSelectedCellsType(Element* aElement, NS_IMETHODIMP HTMLEditor::GetSelectedCellsType(Element* aElement,