Issue #2401 - Part 2: Add GetCommonFlattenedTreeAncestor.

Passing GetFlattenedTreeParent() as the "get parent" function.

This way we can reduce the complexity in our EventStateManager by simply
calling out to the templatized function from Part 1.
This commit is contained in:
Moonchild 2024-01-09 11:40:33 +01:00 • committed by roytam1
commit d375578415
3 changed files with 23 additions and 41 deletions

View file

@ -2442,6 +2442,16 @@ nsContentUtils::GetCommonAncestor(nsINode* aNode1, nsINode* aNode2)
});
}
/* static */
nsIContent*
nsContentUtils::GetCommonFlattenedTreeAncestor(nsIContent* aContent1,
nsIContent* aContent2)
{
return GetCommonAncestorInternal(aContent1, aContent2, [](nsIContent* aContent) {
return aContent->GetFlattenedTreeParent();
});
}
// static
nsINode*
nsContentUtils::GetCommonAncestorUnderInteractiveContent(nsINode* aNode1,

View file

@ -338,6 +338,13 @@ public:
*/
static nsINode* GetCommonAncestor(nsINode* aNode1, nsINode* aNode2);
/**
* Returns the common flattened tree ancestor, if any, for two given content
* nodes.
*/
static nsIContent* GetCommonFlattenedTreeAncestor(nsIContent* aContent1,
nsIContent* aContent2);
/**
* Returns the common ancestor under interactive content, if any.
* If neither one has interactive content as ancestor, common ancestor will be

View file

@ -4849,48 +4849,13 @@ GetLabelTarget(nsIContent* aPossibleLabel)
return label->GetLabeledElement();
}
static nsIContent* FindCommonAncestor(nsIContent *aNode1, nsIContent *aNode2)
static nsIContent*
FindCommonAncestor(nsIContent *aNode1, nsIContent *aNode2)
{
// Find closest common ancestor
if (aNode1 && aNode2) {
// Find the nearest common ancestor by counting the distance to the
// root and then walking up again, in pairs.
int32_t offset = 0;
nsIContent *anc1 = aNode1;
for (;;) {
++offset;
nsIContent* parent = anc1->GetFlattenedTreeParent();
if (!parent)
break;
anc1 = parent;
}
nsIContent *anc2 = aNode2;
for (;;) {
--offset;
nsIContent* parent = anc2->GetFlattenedTreeParent();
if (!parent)
break;
anc2 = parent;
}
if (anc1 == anc2) {
anc1 = aNode1;
anc2 = aNode2;
while (offset > 0) {
anc1 = anc1->GetFlattenedTreeParent();
--offset;
}
while (offset < 0) {
anc2 = anc2->GetFlattenedTreeParent();
++offset;
}
while (anc1 != anc2) {
anc1 = anc1->GetFlattenedTreeParent();
anc2 = anc2->GetFlattenedTreeParent();
}
return anc1;
}
if (!aNode1 || !aNode2) {
return nullptr;
}
return nullptr;
return nsContentUtils::GetCommonFlattenedTreeAncestor(aNode1, aNode2);
}
/* static */