Bug 1613378: part 1) Decouple Selection::GetTableSelectionType from Selection class. r=smaug

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

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Mirko Brodesser 2020-02-07 09:24:34 +00:00
parent e826123135
commit f3af81d6e9
2 changed files with 57 additions and 54 deletions

View File

@ -410,6 +410,63 @@ void Selection::SetCaretBidiLevel(const Nullable<int16_t>& aCaretBidiLevel,
}
}
/**
* Test whether the supplied range points to a single table element.
* Result is one of the TableSelection constants. "None" means
* a table element isn't selected.
*/
// TODO: Figure out TableSelection::Column and TableSelection::AllCells
static nsresult GetTableSelectionType(nsRange* aRange,
TableSelection* aTableSelectionType) {
if (!aRange || !aTableSelectionType) {
return NS_ERROR_NULL_POINTER;
}
*aTableSelectionType = TableSelection::None;
nsINode* startNode = aRange->GetStartContainer();
if (!startNode) {
return NS_ERROR_FAILURE;
}
nsINode* endNode = aRange->GetEndContainer();
if (!endNode) {
return NS_ERROR_FAILURE;
}
// Not a single selected node
if (startNode != endNode) {
return NS_OK;
}
nsIContent* child = aRange->GetChildAtStartOffset();
// Not a single selected node
if (!child || child->GetNextSibling() != aRange->GetChildAtEndOffset()) {
return NS_OK;
}
nsIContent* startContent = static_cast<nsIContent*>(startNode);
if (!(startNode->IsElement() && startContent->IsHTMLElement())) {
// Implies a check for being an element; if we ever make this work
// for non-HTML, need to keep checking for elements.
return NS_OK;
}
if (startContent->IsHTMLElement(nsGkAtoms::tr)) {
*aTableSelectionType = TableSelection::Cell;
} else // check to see if we are selecting a table or row (column and all
// cells not done yet)
{
if (child->IsHTMLElement(nsGkAtoms::table))
*aTableSelectionType = TableSelection::Table;
else if (child->IsHTMLElement(nsGkAtoms::tr))
*aTableSelectionType = TableSelection::Row;
}
return NS_OK;
}
nsresult Selection::GetTableCellLocationFromRange(
nsRange* aRange, TableSelection* aSelectionType, int32_t* aRow,
int32_t* aCol) {
@ -493,53 +550,6 @@ nsresult Selection::MaybeAddTableCellRange(nsRange* aRange, bool* aDidAddRange,
return AddRangesForSelectableNodes(aRange, aOutIndex);
}
// TODO: Figure out TableSelection::Column and TableSelection::AllCells
nsresult Selection::GetTableSelectionType(nsRange* aRange,
TableSelection* aTableSelectionType) {
if (!aRange || !aTableSelectionType) return NS_ERROR_NULL_POINTER;
*aTableSelectionType = TableSelection::None;
// Must have access to frame selection to get cell info
if (!mFrameSelection) return NS_OK;
nsINode* startNode = aRange->GetStartContainer();
if (!startNode) return NS_ERROR_FAILURE;
nsINode* endNode = aRange->GetEndContainer();
if (!endNode) return NS_ERROR_FAILURE;
// Not a single selected node
if (startNode != endNode) return NS_OK;
nsIContent* child = aRange->GetChildAtStartOffset();
// Not a single selected node
if (!child || child->GetNextSibling() != aRange->GetChildAtEndOffset()) {
return NS_OK;
}
nsIContent* startContent = static_cast<nsIContent*>(startNode);
if (!(startNode->IsElement() && startContent->IsHTMLElement())) {
// Implies a check for being an element; if we ever make this work
// for non-HTML, need to keep checking for elements.
return NS_OK;
}
if (startContent->IsHTMLElement(nsGkAtoms::tr)) {
*aTableSelectionType = TableSelection::Cell;
} else // check to see if we are selecting a table or row (column and all
// cells not done yet)
{
if (child->IsHTMLElement(nsGkAtoms::table))
*aTableSelectionType = TableSelection::Table;
else if (child->IsHTMLElement(nsGkAtoms::tr))
*aTableSelectionType = TableSelection::Row;
}
return NS_OK;
}
Selection::Selection()
: mCachedOffsetForFrame(nullptr),
mDirection(eDirNext),

View File

@ -712,13 +712,6 @@ class Selection final : public nsSupportsWeakReference,
*/
void SelectFramesInAllRanges(nsPresContext* aPresContext);
/**
* Test whether the supplied range points to a single table element.
* Result is one of the TableSelection constants. "None" means
* a table element isn't selected.
*/
nsresult GetTableSelectionType(nsRange* aRange,
TableSelection* aTableSelectionType);
MOZ_CAN_RUN_SCRIPT_BOUNDARY
nsresult GetTableCellLocationFromRange(nsRange* aRange,
TableSelection* aSelectionType,