Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/layout/base/nsGenConList.cpp
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
3
/* This Source Code Form is subject to the terms of the Mozilla Public
4
 * License, v. 2.0. If a copy of the MPL was not distributed with this
5
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
/* base class for nsCounterList and nsQuoteList */
8
9
#include "nsGenConList.h"
10
#include "nsLayoutUtils.h"
11
#include "nsIContent.h"
12
13
void
14
nsGenConList::Clear()
15
0
{
16
0
  // Delete entire list.
17
0
  mNodes.Clear();
18
0
  while (nsGenConNode* node = mList.popFirst()) {
19
0
    delete node;
20
0
  }
21
0
  mSize = 0;
22
0
  mLastInserted = nullptr;
23
0
}
24
25
bool
26
nsGenConList::DestroyNodesFor(nsIFrame* aFrame)
27
0
{
28
0
  // This algorithm relies on the invariant that nodes of a frame are
29
0
  // put contiguously in the linked list. This is guaranteed because
30
0
  // each frame is mapped to only one (nsIContent, pseudoType) pair,
31
0
  // and the nodes in the linked list are put in the tree order based
32
0
  // on that pair and offset inside frame.
33
0
  nsGenConNode* node = mNodes.GetAndRemove(aFrame).valueOr(nullptr);
34
0
  if (!node) {
35
0
    return false;
36
0
  }
37
0
  MOZ_ASSERT(node->mPseudoFrame == aFrame);
38
0
39
0
  while (node && node->mPseudoFrame == aFrame) {
40
0
    nsGenConNode* nextNode = Next(node);
41
0
    Destroy(node);
42
0
    node = nextNode;
43
0
  }
44
0
45
0
  // Modification of the list invalidates the cached pointer.
46
0
  mLastInserted = nullptr;
47
0
48
0
  return true;
49
0
}
50
51
/**
52
 * Compute the type of the pseudo and the content for the pseudo that
53
 * we'll use for comparison purposes.
54
 * @param aContent the content to use is stored here; it's the element
55
 * that generated the ::before or ::after content, or (if not for generated
56
 * content), the frame's own element
57
 * @return -1 for ::before, +1 for ::after, and 0 otherwise.
58
 */
59
inline int32_t PseudoCompareType(nsIFrame* aFrame, nsIContent** aContent)
60
0
{
61
0
  nsAtom *pseudo = aFrame->Style()->GetPseudo();
62
0
  if (pseudo == nsCSSPseudoElements::before()) {
63
0
    *aContent = aFrame->GetContent()->GetParent();
64
0
    return -1;
65
0
  }
66
0
  if (pseudo == nsCSSPseudoElements::after()) {
67
0
    *aContent = aFrame->GetContent()->GetParent();
68
0
    return 1;
69
0
  }
70
0
  *aContent = aFrame->GetContent();
71
0
  return 0;
72
0
}
73
74
/* static */ bool
75
nsGenConList::NodeAfter(const nsGenConNode* aNode1, const nsGenConNode* aNode2)
76
0
{
77
0
  nsIFrame* frame1 = aNode1->mPseudoFrame;
78
0
  nsIFrame* frame2 = aNode2->mPseudoFrame;
79
0
  if (frame1 == frame2) {
80
0
    NS_ASSERTION(aNode2->mContentIndex != aNode1->mContentIndex, "identical");
81
0
    return aNode1->mContentIndex > aNode2->mContentIndex;
82
0
  }
83
0
  nsIContent* content1;
84
0
  nsIContent* content2;
85
0
  int32_t pseudoType1 = PseudoCompareType(frame1, &content1);
86
0
  int32_t pseudoType2 = PseudoCompareType(frame2, &content2);
87
0
  if (pseudoType1 == 0 || pseudoType2 == 0) {
88
0
    if (content1 == content2) {
89
0
      NS_ASSERTION(pseudoType1 != pseudoType2, "identical");
90
0
      return pseudoType2 == 0;
91
0
    }
92
0
    // We want to treat an element as coming before its :before (preorder
93
0
    // traversal), so treating both as :before now works.
94
0
    if (pseudoType1 == 0) pseudoType1 = -1;
95
0
    if (pseudoType2 == 0) pseudoType2 = -1;
96
0
  } else {
97
0
    if (content1 == content2) {
98
0
      NS_ASSERTION(pseudoType1 != pseudoType2, "identical");
99
0
      return pseudoType1 == 1;
100
0
    }
101
0
  }
102
0
103
0
  int32_t cmp = nsLayoutUtils::DoCompareTreePosition(content1, content2,
104
0
                                                     pseudoType1, -pseudoType2);
105
0
  MOZ_ASSERT(cmp != 0, "same content, different frames");
106
0
  return cmp > 0;
107
0
}
108
109
void
110
nsGenConList::Insert(nsGenConNode* aNode)
111
0
{
112
0
  // Check for append.
113
0
  if (mList.isEmpty() || NodeAfter(aNode, mList.getLast())) {
114
0
    mList.insertBack(aNode);
115
0
  } else if (mLastInserted && mLastInserted != mList.getLast() &&
116
0
             NodeAfter(aNode, mLastInserted) &&
117
0
             NodeAfter(Next(mLastInserted), aNode)) {
118
0
    // Fast path for inserting many consecutive nodes in one place
119
0
    mLastInserted->setNext(aNode);
120
0
  } else {
121
0
    // Binary search.
122
0
123
0
    // the range of indices at which |aNode| could end up.
124
0
    // (We already know it can't be at index mSize.)
125
0
    uint32_t first = 0, last = mSize - 1;
126
0
127
0
    // A cursor to avoid walking more than the length of the list.
128
0
    nsGenConNode* curNode = mList.getLast();
129
0
    uint32_t curIndex = mSize - 1;
130
0
131
0
    while (first != last) {
132
0
      uint32_t test = (first + last) / 2;
133
0
      if (last == curIndex) {
134
0
        for ( ; curIndex != test; --curIndex)
135
0
          curNode = Prev(curNode);
136
0
      } else {
137
0
        for ( ; curIndex != test; ++curIndex)
138
0
          curNode = Next(curNode);
139
0
      }
140
0
141
0
      if (NodeAfter(aNode, curNode)) {
142
0
        first = test + 1;
143
0
        // if we exit the loop, we need curNode to be right
144
0
        ++curIndex;
145
0
        curNode = Next(curNode);
146
0
      } else {
147
0
        last = test;
148
0
      }
149
0
    }
150
0
    curNode->setPrevious(aNode);
151
0
  }
152
0
  ++mSize;
153
0
154
0
  mLastInserted = aNode;
155
0
156
0
  // Set the mapping only if it is the first node of the frame.
157
0
  // The DEBUG blocks below are for ensuring the invariant required by
158
0
  // nsGenConList::DestroyNodesFor. See comment there.
159
0
  if (IsFirst(aNode) ||
160
0
      Prev(aNode)->mPseudoFrame != aNode->mPseudoFrame) {
161
#ifdef DEBUG
162
    if (nsGenConNode* oldFrameFirstNode = mNodes.Get(aNode->mPseudoFrame)) {
163
      MOZ_ASSERT(Next(aNode) == oldFrameFirstNode,
164
                 "oldFrameFirstNode should now be immediately after "
165
                 "the newly-inserted one.");
166
    } else {
167
      // If the node is not the only node in the list.
168
      if (!IsFirst(aNode) || !IsLast(aNode)) {
169
        nsGenConNode* nextNode = Next(aNode);
170
        MOZ_ASSERT(!nextNode || nextNode->mPseudoFrame != aNode->mPseudoFrame,
171
                   "There shouldn't exist any node for this frame.");
172
        // If the node is neither the first nor the last node
173
        if (!IsFirst(aNode) && !IsLast(aNode)) {
174
          MOZ_ASSERT(Prev(aNode)->mPseudoFrame != nextNode->mPseudoFrame,
175
                     "New node should not break contiguity of nodes of "
176
                     "the same frame.");
177
        }
178
      }
179
    }
180
#endif
181
    mNodes.Put(aNode->mPseudoFrame, aNode);
182
0
  } else {
183
#ifdef DEBUG
184
    nsGenConNode* frameFirstNode = mNodes.Get(aNode->mPseudoFrame);
185
    MOZ_ASSERT(frameFirstNode, "There should exist node map for the frame.");
186
    for (nsGenConNode* curNode = Prev(aNode);
187
         curNode != frameFirstNode; curNode = Prev(curNode)) {
188
      MOZ_ASSERT(curNode->mPseudoFrame == aNode->mPseudoFrame,
189
                 "Every node between frameFirstNode and the new node inserted "
190
                 "should refer to the same frame.");
191
      MOZ_ASSERT(!IsFirst(curNode),
192
                 "The newly-inserted node should be in a contiguous run after "
193
                 "frameFirstNode, thus frameFirstNode should be reached before "
194
                 "the first node of mList.");
195
    }
196
#endif
197
  }
198
0
199
0
  NS_ASSERTION(IsFirst(aNode) || NodeAfter(aNode, Prev(aNode)),
200
0
               "sorting error");
201
0
  NS_ASSERTION(IsLast(aNode) || NodeAfter(Next(aNode), aNode),
202
0
               "sorting error");
203
0
}