summaryrefslogtreecommitdiffstats
path: root/dom/grid/GridLines.cpp
blob: 4610f18eabf1dd5e1c57a6a0a9b0ea99ef005188 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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/. */

#include "GridLines.h"

#include "GridDimension.h"
#include "GridLine.h"
#include "mozilla/dom/GridBinding.h"
#include "mozilla/dom/GridArea.h"
#include "nsGridContainerFrame.h"

namespace mozilla::dom {

NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(GridLines, mParent, mLines)
NS_IMPL_CYCLE_COLLECTING_ADDREF(GridLines)
NS_IMPL_CYCLE_COLLECTING_RELEASE(GridLines)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(GridLines)
  NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
  NS_INTERFACE_MAP_ENTRY(nsISupports)
NS_INTERFACE_MAP_END

GridLines::GridLines(GridDimension* aParent) : mParent(aParent) {
  MOZ_ASSERT(aParent, "Should never be instantiated with a null GridDimension");
}

GridLines::~GridLines() = default;

JSObject* GridLines::WrapObject(JSContext* aCx,
                                JS::Handle<JSObject*> aGivenProto) {
  return GridLines_Binding::Wrap(aCx, this, aGivenProto);
}

uint32_t GridLines::Length() const { return mLines.Length(); }

GridLine* GridLines::Item(uint32_t aIndex) {
  return mLines.SafeElementAt(aIndex);
}

GridLine* GridLines::IndexedGetter(uint32_t aIndex, bool& aFound) {
  aFound = aIndex < mLines.Length();
  if (!aFound) {
    return nullptr;
  }
  return mLines[aIndex];
}

static void AddLineNameIfNotPresent(nsTArray<RefPtr<nsAtom>>& aLineNames,
                                    nsAtom* aName) {
  if (!aLineNames.Contains(aName)) {
    aLineNames.AppendElement(aName);
  }
}

static void AddLineNamesIfNotPresent(nsTArray<RefPtr<nsAtom>>& aLineNames,
                                     const nsTArray<RefPtr<nsAtom>>& aNames) {
  for (const auto& name : aNames) {
    AddLineNameIfNotPresent(aLineNames, name);
  }
}

void GridLines::SetLineInfo(const ComputedGridTrackInfo* aTrackInfo,
                            const ComputedGridLineInfo* aLineInfo,
                            const nsTArray<RefPtr<GridArea>>& aAreas,
                            bool aIsRow) {
  MOZ_ASSERT(aLineInfo);
  mLines.Clear();

  if (!aTrackInfo) {
    return;
  }

  uint32_t lineCount =
      aTrackInfo->mEndFragmentTrack - aTrackInfo->mStartFragmentTrack + 1;

  // If there is at least one track, line count is one more
  // than the number of tracks.
  if (lineCount > 0) {
    nscoord lastTrackEdge = 0;
    nscoord startOfNextTrack;
    uint32_t repeatIndex = 0;
    uint32_t numRepeatTracks = aTrackInfo->mRemovedRepeatTracks.Length();
    uint32_t numAddedLines = 0;

    // For the calculation of negative line numbers, we need to know
    // the total number of leading implicit and explicit tracks.
    // This might be different from the number of tracks sizes in
    // aTrackInfo, because some of those tracks may be auto-fits that
    // have been removed.
    uint32_t leadingTrackCount =
        aTrackInfo->mNumLeadingImplicitTracks + aTrackInfo->mNumExplicitTracks;
    if (numRepeatTracks > 0) {
      for (auto& removedTrack : aTrackInfo->mRemovedRepeatTracks) {
        if (removedTrack) {
          ++leadingTrackCount;
        }
      }
    }

    for (uint32_t i = aTrackInfo->mStartFragmentTrack;
         i < aTrackInfo->mEndFragmentTrack + 1; i++) {
      // Since line indexes are 1-based, calculate a 1-based value
      // for this track to simplify some calculations.
      const uint32_t line1Index = i + 1;

      startOfNextTrack = (i < aTrackInfo->mEndFragmentTrack)
                             ? aTrackInfo->mPositions[i]
                             : lastTrackEdge;

      // Get the line names for the current line. aLineInfo->mNames
      // may contain duplicate names. This is intentional, since grid
      // layout works fine with duplicate names, and we don't want to
      // detect and remove duplicates in layout since it is an O(n^2)
      // problem. We do the work here since this is only run when
      // requested by devtools, and slowness here will not affect
      // normal browsing.
      nsTArray<RefPtr<nsAtom>> empty{};
      const nsTArray<RefPtr<nsAtom>>& possiblyDuplicateLineNames(
          aLineInfo->mNames.SafeElementAt(i, empty));

      nsTArray<RefPtr<nsAtom>> lineNames;
      AddLineNamesIfNotPresent(lineNames, possiblyDuplicateLineNames);

      // Add in names from grid areas where this line is used as a boundary.
      for (auto area : aAreas) {
        // We specifically ignore line names from implicitly named areas,
        // because it can be confusing for designers who might naturally use
        // a named line of "-start" or "-end" and create an implicit named
        // area without meaning to.
        if (area->Type() == GridDeclaration::Implicit) {
          continue;
        }

        bool haveNameToAdd = false;
        nsAutoString nameToAdd;
        area->GetName(nameToAdd);
        if (aIsRow) {
          if (area->RowStart() == line1Index) {
            haveNameToAdd = true;
            nameToAdd.AppendLiteral("-start");
          } else if (area->RowEnd() == line1Index) {
            haveNameToAdd = true;
            nameToAdd.AppendLiteral("-end");
          }
        } else {
          if (area->ColumnStart() == line1Index) {
            haveNameToAdd = true;
            nameToAdd.AppendLiteral("-start");
          } else if (area->ColumnEnd() == line1Index) {
            haveNameToAdd = true;
            nameToAdd.AppendLiteral("-end");
          }
        }

        if (haveNameToAdd) {
          RefPtr<nsAtom> name = NS_Atomize(nameToAdd);
          AddLineNameIfNotPresent(lineNames, name);
        }
      }

      if (i >= (aTrackInfo->mRepeatFirstTrack +
                aTrackInfo->mNumLeadingImplicitTracks) &&
          repeatIndex < numRepeatTracks) {
        numAddedLines += AppendRemovedAutoFits(
            aTrackInfo, aLineInfo, lastTrackEdge, repeatIndex, numRepeatTracks,
            leadingTrackCount, lineNames);
      }

      // If this line is the one that ends a repeat, then add
      // in the mNamesFollowingRepeat names from aLineInfo.
      if (numRepeatTracks > 0 && i == (aTrackInfo->mRepeatFirstTrack +
                                       aTrackInfo->mNumLeadingImplicitTracks +
                                       numRepeatTracks - numAddedLines)) {
        AddLineNamesIfNotPresent(lineNames, aLineInfo->mNamesFollowingRepeat);
      }

      RefPtr<GridLine> line = new GridLine(this);
      mLines.AppendElement(line);
      MOZ_ASSERT(line1Index > 0, "line1Index must be positive.");
      bool isBeforeFirstExplicit =
          (line1Index <= aTrackInfo->mNumLeadingImplicitTracks);
      bool isAfterLastExplicit = line1Index > (leadingTrackCount + 1);
      // Calculate an actionable line number for this line, that could be used
      // in a css grid property to align a grid item or area at that line.
      // For implicit lines that appear before line 1, report a number of 0.
      // We can't report negative indexes, because those have a different
      // meaning in the css grid spec (negative indexes are negative-1-based
      // from the end of the grid decreasing towards the front).
      uint32_t lineNumber = isBeforeFirstExplicit
                                ? 0
                                : (line1Index + numAddedLines -
                                   aTrackInfo->mNumLeadingImplicitTracks);

      // The negativeNumber is counted back from the leadingTrackCount.
      int32_t lineNegativeNumber =
          isAfterLastExplicit
              ? 0
              : (line1Index + numAddedLines - (leadingTrackCount + 2));
      GridDeclaration lineType = (isBeforeFirstExplicit || isAfterLastExplicit)
                                     ? GridDeclaration::Implicit
                                     : GridDeclaration::Explicit;
      line->SetLineValues(
          lineNames, nsPresContext::AppUnitsToDoubleCSSPixels(lastTrackEdge),
          nsPresContext::AppUnitsToDoubleCSSPixels(startOfNextTrack -
                                                   lastTrackEdge),
          lineNumber, lineNegativeNumber, lineType);

      if (i < aTrackInfo->mEndFragmentTrack) {
        lastTrackEdge = aTrackInfo->mPositions[i] + aTrackInfo->mSizes[i];
      }
    }

    // Define a function that gets the mLines index for a given line number.
    // This is necessary since it's possible for a line number to not be
    // represented in mLines. If this is the case, then return  -1.
    const int32_t lineCount = mLines.Length();
    const uint32_t lastLineNumber = mLines[lineCount - 1]->Number();
    auto IndexForLineNumber =
        [lineCount, lastLineNumber](uint32_t aLineNumber) -> int32_t {
      if (lastLineNumber == 0) {
        // None of the lines have addressable numbers, so none of them can have
        // aLineNumber
        return -1;
      }

      int32_t possibleIndex = (int32_t)aLineNumber - 1;
      if (possibleIndex < 0 || possibleIndex > lineCount - 1) {
        // aLineNumber is not represented in mLines.
        return -1;
      }

      return possibleIndex;
    };

    // Post-processing loop for implicit grid areas.
    for (const auto& area : aAreas) {
      if (area->Type() == GridDeclaration::Implicit) {
        // Get the appropriate indexes for the area's start and end lines as
        // they are represented in mLines.
        int32_t startIndex =
            IndexForLineNumber(aIsRow ? area->RowStart() : area->ColumnStart());
        int32_t endIndex =
            IndexForLineNumber(aIsRow ? area->RowEnd() : area->ColumnEnd());

        // If both start and end indexes are -1, then stop here since we cannot
        // reason about the naming for either lines.
        if (startIndex < 0 && endIndex < 0) {
          break;
        }

        // Get the "-start" and "-end" line names of the grid area.
        nsAutoString startLineName;
        area->GetName(startLineName);
        startLineName.AppendLiteral("-start");
        nsAutoString endLineName;
        area->GetName(endLineName);
        endLineName.AppendLiteral("-end");

        // Get the list of existing line names for the start and end of the grid
        // area. In the case where one of the start or end indexes are -1, use a
        // dummy line as a substitute for the start/end line.
        RefPtr<GridLine> dummyLine = new GridLine(this);
        RefPtr<GridLine> areaStartLine =
            startIndex > -1 ? mLines[startIndex] : dummyLine;
        nsTArray<RefPtr<nsAtom>> startLineNames(areaStartLine->Names().Clone());

        RefPtr<GridLine> areaEndLine =
            endIndex > -1 ? mLines[endIndex] : dummyLine;
        nsTArray<RefPtr<nsAtom>> endLineNames(areaEndLine->Names().Clone());

        RefPtr<nsAtom> start = NS_Atomize(startLineName);
        RefPtr<nsAtom> end = NS_Atomize(endLineName);
        if (startLineNames.Contains(end) || endLineNames.Contains(start)) {
          // Add the reversed line names.
          AddLineNameIfNotPresent(startLineNames, end);
          AddLineNameIfNotPresent(endLineNames, start);
        } else {
          // Add the normal line names.
          AddLineNameIfNotPresent(startLineNames, start);
          AddLineNameIfNotPresent(endLineNames, end);
        }

        areaStartLine->SetLineNames(startLineNames);
        areaEndLine->SetLineNames(endLineNames);
      }
    }
  }
}

uint32_t GridLines::AppendRemovedAutoFits(
    const ComputedGridTrackInfo* aTrackInfo,
    const ComputedGridLineInfo* aLineInfo, nscoord aLastTrackEdge,
    uint32_t& aRepeatIndex, uint32_t aNumRepeatTracks,
    uint32_t aNumLeadingTracks, nsTArray<RefPtr<nsAtom>>& aLineNames) {
  bool extractedExplicitLineNames = false;
  nsTArray<RefPtr<nsAtom>> explicitLineNames;
  uint32_t linesAdded = 0;
  while (aRepeatIndex < aNumRepeatTracks &&
         aTrackInfo->mRemovedRepeatTracks[aRepeatIndex]) {
    // If this is not the very first call to this function, and if we
    // haven't already added a line this call, pull all the explicit
    // names to pass along to the next line that will be added after
    // this function completes.
    if (aRepeatIndex > 0 && linesAdded == 0) {
      // Find the names that didn't match the before or after names,
      // and extract them.
      for (const auto& name : aLineNames) {
        if (!aLineInfo->mNamesBefore.Contains(name) &&
            !aLineInfo->mNamesAfter.Contains(name)) {
          explicitLineNames.AppendElement(name);
        }
      }
      for (const auto& extractedName : explicitLineNames) {
        aLineNames.RemoveElement(extractedName);
      }
      extractedExplicitLineNames = true;
    }

    AddLineNamesIfNotPresent(aLineNames, aLineInfo->mNamesBefore);

    RefPtr<GridLine> line = new GridLine(this);
    mLines.AppendElement(line);

    // Time to calculate the line numbers. For the positive numbers
    // we count with a 1-based index from mRepeatFirstTrack. Although
    // this number is the index of the first repeat track AFTER all
    // the leading implicit tracks, that's still what we want since
    // all those leading implicit tracks have line number 0.
    uint32_t lineNumber = aTrackInfo->mRepeatFirstTrack + aRepeatIndex + 1;

    // The negative number does have to account for the leading
    // implicit tracks. We've been passed aNumLeadingTracks which is
    // the total of the leading implicit tracks plus the explicit
    // tracks. So all we have to do is subtract that number plus one
    // from the 0-based index of this track.
    int32_t lineNegativeNumber =
        (aTrackInfo->mNumLeadingImplicitTracks + aTrackInfo->mRepeatFirstTrack +
         aRepeatIndex) -
        (aNumLeadingTracks + 1);
    line->SetLineValues(
        aLineNames, nsPresContext::AppUnitsToDoubleCSSPixels(aLastTrackEdge),
        nsPresContext::AppUnitsToDoubleCSSPixels(0), lineNumber,
        lineNegativeNumber, GridDeclaration::Explicit);

    // No matter what, the next line should have the after names associated
    // with it. If we go through the loop again, the before names will also
    // be added.
    aLineNames = aLineInfo->mNamesAfter.Clone();
    aRepeatIndex++;

    linesAdded++;
  }

  aRepeatIndex++;

  if (extractedExplicitLineNames) {
    // Pass on the explicit names we saved to the next explicit line.
    AddLineNamesIfNotPresent(aLineNames, explicitLineNames);
  }

  // If we haven't finished adding auto-repeated tracks, then we need to put
  // back the before names, in case we cleared them above.
  if (aRepeatIndex < aNumRepeatTracks) {
    AddLineNamesIfNotPresent(aLineNames, aLineInfo->mNamesBefore);
  }

  return linesAdded;
}

}  // namespace mozilla::dom