Bug 1361301: Add nsContentUtils::GetCommonFlattenedTreeAncestor. r?smaug
MozReview-Commit-ID: EVyjTrjpid2
--- a/dom/base/nsContentUtils.cpp
+++ b/dom/base/nsContentUtils.cpp
@@ -2543,16 +2543,26 @@ nsINode*
nsContentUtils::GetCommonAncestor(nsINode* aNode1, nsINode* aNode2)
{
return GetCommonAncestorInternal(aNode1, aNode2, [](nsINode* aNode) {
return aNode->GetParentNode();
});
}
/* static */
+nsIContent*
+nsContentUtils::GetCommonFlattenedTreeAncestor(nsIContent* aContent1,
+ nsIContent* aContent2)
+{
+ return GetCommonAncestorInternal(aContent1, aContent2, [](nsIContent* aContent) {
+ return aContent->GetFlattenedTreeParent();
+ });
+}
+
+/* static */
bool
nsContentUtils::PositionIsBefore(nsINode* aNode1, nsINode* aNode2)
{
return (aNode2->CompareDocumentPosition(*aNode1) &
(nsIDOMNode::DOCUMENT_POSITION_PRECEDING |
nsIDOMNode::DOCUMENT_POSITION_DISCONNECTED)) ==
nsIDOMNode::DOCUMENT_POSITION_PRECEDING;
}
--- a/dom/base/nsContentUtils.h
+++ b/dom/base/nsContentUtils.h
@@ -367,16 +367,22 @@ public:
/**
* Returns the common ancestor, if any, for two nodes.
*
* Returns null if the nodes are disconnected.
*/
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 true if aNode1 is before aNode2 in the same connected
* tree.
*/
static bool PositionIsBefore(nsINode* aNode1, nsINode* aNode2);
/**
--- a/dom/events/EventStateManager.cpp
+++ b/dom/events/EventStateManager.cpp
@@ -4771,58 +4771,23 @@ GetLabelTarget(nsIContent* aPossibleLabe
mozilla::dom::HTMLLabelElement* label =
mozilla::dom::HTMLLabelElement::FromContent(aPossibleLabel);
if (!label)
return nullptr;
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;
- }
- }
- return nullptr;
+ if (!aNode1 || !aNode2) {
+ return nullptr;
+ }
+ return nsContentUtils::GetCommonFlattenedTreeAncestor(aNode1, aNode2);
}
/* static */
void
EventStateManager::SetFullScreenState(Element* aElement, bool aIsFullScreen)
{
DoStateChange(aElement, NS_EVENT_STATE_FULL_SCREEN, aIsFullScreen);
}