2017-10-27 20:33:53 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2005-04-02 03:07:00 +04:00
|
|
|
|
2006-03-30 09:56:38 +04:00
|
|
|
/* base class for nsCounterList and nsQuoteList */
|
|
|
|
|
2005-04-02 03:07:00 +04:00
|
|
|
#include "nsGenConList.h"
|
|
|
|
#include "nsLayoutUtils.h"
|
2013-10-02 01:00:38 +04:00
|
|
|
#include "nsIContent.h"
|
2005-04-02 03:07:00 +04:00
|
|
|
|
|
|
|
void
|
|
|
|
nsGenConList::Clear()
|
|
|
|
{
|
2016-10-27 13:07:52 +03:00
|
|
|
// Delete entire list.
|
2016-08-23 03:31:26 +03:00
|
|
|
mNodes.Clear();
|
2016-10-27 13:07:52 +03:00
|
|
|
while (nsGenConNode* node = mList.popFirst()) {
|
|
|
|
delete node;
|
2005-04-02 03:07:00 +04:00
|
|
|
}
|
|
|
|
mSize = 0;
|
2017-03-28 17:04:48 +03:00
|
|
|
mLastInserted = nullptr;
|
2005-04-02 03:07:00 +04:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2005-04-02 03:07:00 +04:00
|
|
|
nsGenConList::DestroyNodesFor(nsIFrame* aFrame)
|
|
|
|
{
|
2016-08-23 03:31:26 +03:00
|
|
|
// This algorithm relies on the invariant that nodes of a frame are
|
|
|
|
// put contiguously in the linked list. This is guaranteed because
|
|
|
|
// each frame is mapped to only one (nsIContent, pseudoType) pair,
|
|
|
|
// and the nodes in the linked list are put in the tree order based
|
|
|
|
// on that pair and offset inside frame.
|
|
|
|
nsGenConNode* node = mNodes.GetAndRemove(aFrame).valueOr(nullptr);
|
|
|
|
if (!node) {
|
|
|
|
return false;
|
2005-04-02 03:07:00 +04:00
|
|
|
}
|
2016-08-23 03:31:26 +03:00
|
|
|
MOZ_ASSERT(node->mPseudoFrame == aFrame);
|
2016-10-27 13:07:52 +03:00
|
|
|
|
|
|
|
while (node && node->mPseudoFrame == aFrame) {
|
|
|
|
nsGenConNode* nextNode = Next(node);
|
|
|
|
Destroy(node);
|
|
|
|
node = nextNode;
|
2005-04-02 03:07:00 +04:00
|
|
|
}
|
2016-10-27 13:07:52 +03:00
|
|
|
|
2017-03-28 17:04:48 +03:00
|
|
|
// Modification of the list invalidates the cached pointer.
|
|
|
|
mLastInserted = nullptr;
|
|
|
|
|
2016-08-23 03:31:26 +03:00
|
|
|
return true;
|
2005-04-02 03:07:00 +04:00
|
|
|
}
|
|
|
|
|
2008-08-18 05:16:42 +04:00
|
|
|
/**
|
|
|
|
* Compute the type of the pseudo and the content for the pseudo that
|
|
|
|
* we'll use for comparison purposes.
|
|
|
|
* @param aContent the content to use is stored here; it's the element
|
|
|
|
* that generated the ::before or ::after content, or (if not for generated
|
|
|
|
* content), the frame's own element
|
|
|
|
* @return -1 for ::before, +1 for ::after, and 0 otherwise.
|
|
|
|
*/
|
2012-08-22 19:56:38 +04:00
|
|
|
inline int32_t PseudoCompareType(nsIFrame* aFrame, nsIContent** aContent)
|
2005-04-02 03:07:00 +04:00
|
|
|
{
|
2018-03-22 21:20:41 +03:00
|
|
|
nsAtom *pseudo = aFrame->Style()->GetPseudo();
|
2018-08-15 08:46:00 +03:00
|
|
|
if (pseudo == nsCSSPseudoElements::before()) {
|
2008-08-18 05:16:42 +04:00
|
|
|
*aContent = aFrame->GetContent()->GetParent();
|
2005-04-02 03:07:00 +04:00
|
|
|
return -1;
|
2008-08-18 05:16:42 +04:00
|
|
|
}
|
2018-08-15 08:46:00 +03:00
|
|
|
if (pseudo == nsCSSPseudoElements::after()) {
|
2008-08-18 05:16:42 +04:00
|
|
|
*aContent = aFrame->GetContent()->GetParent();
|
2005-04-02 03:07:00 +04:00
|
|
|
return 1;
|
2008-08-18 05:16:42 +04:00
|
|
|
}
|
|
|
|
*aContent = aFrame->GetContent();
|
2005-04-02 03:07:00 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
/* static */ bool
|
2005-04-02 03:07:00 +04:00
|
|
|
nsGenConList::NodeAfter(const nsGenConNode* aNode1, const nsGenConNode* aNode2)
|
|
|
|
{
|
2018-05-14 17:34:45 +03:00
|
|
|
nsIFrame* frame1 = aNode1->mPseudoFrame;
|
|
|
|
nsIFrame* frame2 = aNode2->mPseudoFrame;
|
2005-04-02 03:07:00 +04:00
|
|
|
if (frame1 == frame2) {
|
|
|
|
NS_ASSERTION(aNode2->mContentIndex != aNode1->mContentIndex, "identical");
|
|
|
|
return aNode1->mContentIndex > aNode2->mContentIndex;
|
|
|
|
}
|
2018-05-14 17:34:45 +03:00
|
|
|
nsIContent* content1;
|
|
|
|
nsIContent* content2;
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t pseudoType1 = PseudoCompareType(frame1, &content1);
|
|
|
|
int32_t pseudoType2 = PseudoCompareType(frame2, &content2);
|
2005-04-02 03:07:00 +04:00
|
|
|
if (pseudoType1 == 0 || pseudoType2 == 0) {
|
|
|
|
if (content1 == content2) {
|
|
|
|
NS_ASSERTION(pseudoType1 != pseudoType2, "identical");
|
|
|
|
return pseudoType2 == 0;
|
|
|
|
}
|
|
|
|
// We want to treat an element as coming before its :before (preorder
|
|
|
|
// traversal), so treating both as :before now works.
|
|
|
|
if (pseudoType1 == 0) pseudoType1 = -1;
|
|
|
|
if (pseudoType2 == 0) pseudoType2 = -1;
|
|
|
|
} else {
|
|
|
|
if (content1 == content2) {
|
|
|
|
NS_ASSERTION(pseudoType1 != pseudoType2, "identical");
|
|
|
|
return pseudoType1 == 1;
|
|
|
|
}
|
|
|
|
}
|
2018-05-14 17:34:45 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t cmp = nsLayoutUtils::DoCompareTreePosition(content1, content2,
|
2005-04-02 03:07:00 +04:00
|
|
|
pseudoType1, -pseudoType2);
|
2016-08-23 03:31:26 +03:00
|
|
|
MOZ_ASSERT(cmp != 0, "same content, different frames");
|
2005-04-02 03:07:00 +04:00
|
|
|
return cmp > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsGenConList::Insert(nsGenConNode* aNode)
|
|
|
|
{
|
2016-10-27 13:07:52 +03:00
|
|
|
// Check for append.
|
|
|
|
if (mList.isEmpty() || NodeAfter(aNode, mList.getLast())) {
|
|
|
|
mList.insertBack(aNode);
|
2017-03-28 17:04:48 +03:00
|
|
|
} else if (mLastInserted && mLastInserted != mList.getLast() &&
|
|
|
|
NodeAfter(aNode, mLastInserted) &&
|
|
|
|
NodeAfter(Next(mLastInserted), aNode)) {
|
|
|
|
// Fast path for inserting many consecutive nodes in one place
|
|
|
|
mLastInserted->setNext(aNode);
|
2016-10-27 13:07:52 +03:00
|
|
|
} else {
|
|
|
|
// Binary search.
|
2005-04-02 03:07:00 +04:00
|
|
|
|
2016-10-27 13:07:52 +03:00
|
|
|
// the range of indices at which |aNode| could end up.
|
|
|
|
// (We already know it can't be at index mSize.)
|
|
|
|
uint32_t first = 0, last = mSize - 1;
|
2005-04-02 03:07:00 +04:00
|
|
|
|
2016-10-27 13:07:52 +03:00
|
|
|
// A cursor to avoid walking more than the length of the list.
|
|
|
|
nsGenConNode* curNode = mList.getLast();
|
|
|
|
uint32_t curIndex = mSize - 1;
|
2005-04-02 03:07:00 +04:00
|
|
|
|
2016-10-27 13:07:52 +03:00
|
|
|
while (first != last) {
|
|
|
|
uint32_t test = (first + last) / 2;
|
|
|
|
if (last == curIndex) {
|
|
|
|
for ( ; curIndex != test; --curIndex)
|
|
|
|
curNode = Prev(curNode);
|
|
|
|
} else {
|
|
|
|
for ( ; curIndex != test; ++curIndex)
|
2005-04-02 03:07:00 +04:00
|
|
|
curNode = Next(curNode);
|
|
|
|
}
|
2016-10-27 13:07:52 +03:00
|
|
|
|
|
|
|
if (NodeAfter(aNode, curNode)) {
|
|
|
|
first = test + 1;
|
|
|
|
// if we exit the loop, we need curNode to be right
|
|
|
|
++curIndex;
|
|
|
|
curNode = Next(curNode);
|
|
|
|
} else {
|
|
|
|
last = test;
|
2005-04-02 03:07:00 +04:00
|
|
|
}
|
|
|
|
}
|
2016-10-27 13:07:52 +03:00
|
|
|
curNode->setPrevious(aNode);
|
2005-04-02 03:07:00 +04:00
|
|
|
}
|
|
|
|
++mSize;
|
2007-01-17 12:24:19 +03:00
|
|
|
|
2017-03-28 17:04:48 +03:00
|
|
|
mLastInserted = aNode;
|
|
|
|
|
2016-08-23 03:31:26 +03:00
|
|
|
// Set the mapping only if it is the first node of the frame.
|
|
|
|
// The DEBUG blocks below are for ensuring the invariant required by
|
|
|
|
// nsGenConList::DestroyNodesFor. See comment there.
|
2016-10-27 13:07:52 +03:00
|
|
|
if (IsFirst(aNode) ||
|
2016-08-23 03:31:26 +03:00
|
|
|
Prev(aNode)->mPseudoFrame != aNode->mPseudoFrame) {
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (nsGenConNode* oldFrameFirstNode = mNodes.Get(aNode->mPseudoFrame)) {
|
|
|
|
MOZ_ASSERT(Next(aNode) == oldFrameFirstNode,
|
|
|
|
"oldFrameFirstNode should now be immediately after "
|
|
|
|
"the newly-inserted one.");
|
|
|
|
} else {
|
|
|
|
// If the node is not the only node in the list.
|
2016-10-27 13:07:52 +03:00
|
|
|
if (!IsFirst(aNode) || !IsLast(aNode)) {
|
|
|
|
nsGenConNode* nextNode = Next(aNode);
|
|
|
|
MOZ_ASSERT(!nextNode || nextNode->mPseudoFrame != aNode->mPseudoFrame,
|
2016-08-23 03:31:26 +03:00
|
|
|
"There shouldn't exist any node for this frame.");
|
|
|
|
// If the node is neither the first nor the last node
|
2016-10-27 13:07:52 +03:00
|
|
|
if (!IsFirst(aNode) && !IsLast(aNode)) {
|
2016-08-23 03:31:26 +03:00
|
|
|
MOZ_ASSERT(Prev(aNode)->mPseudoFrame != nextNode->mPseudoFrame,
|
|
|
|
"New node should not break contiguity of nodes of "
|
|
|
|
"the same frame.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
mNodes.Put(aNode->mPseudoFrame, aNode);
|
|
|
|
} else {
|
|
|
|
#ifdef DEBUG
|
|
|
|
nsGenConNode* frameFirstNode = mNodes.Get(aNode->mPseudoFrame);
|
|
|
|
MOZ_ASSERT(frameFirstNode, "There should exist node map for the frame.");
|
|
|
|
for (nsGenConNode* curNode = Prev(aNode);
|
|
|
|
curNode != frameFirstNode; curNode = Prev(curNode)) {
|
|
|
|
MOZ_ASSERT(curNode->mPseudoFrame == aNode->mPseudoFrame,
|
|
|
|
"Every node between frameFirstNode and the new node inserted "
|
|
|
|
"should refer to the same frame.");
|
2016-10-27 13:07:52 +03:00
|
|
|
MOZ_ASSERT(!IsFirst(curNode),
|
|
|
|
"The newly-inserted node should be in a contiguous run after "
|
|
|
|
"frameFirstNode, thus frameFirstNode should be reached before "
|
|
|
|
"the first node of mList.");
|
2016-08-23 03:31:26 +03:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-10-27 13:07:52 +03:00
|
|
|
NS_ASSERTION(IsFirst(aNode) || NodeAfter(aNode, Prev(aNode)),
|
2007-01-17 12:24:19 +03:00
|
|
|
"sorting error");
|
|
|
|
NS_ASSERTION(IsLast(aNode) || NodeAfter(Next(aNode), aNode),
|
|
|
|
"sorting error");
|
2005-04-02 03:07:00 +04:00
|
|
|
}
|