1 | /* |
2 | * Copyright (C) 2017 Igalia S.L. |
3 | * |
4 | * Redistribution and use in source and binary forms, with or without |
5 | * modification, are permitted provided that the following conditions are |
6 | * met: |
7 | * |
8 | * * Redistributions of source code must retain the above copyright |
9 | * notice, this list of conditions and the following disclaimer. |
10 | * * Redistributions in binary form must reproduce the above |
11 | * copyright notice, this list of conditions and the following disclaimer |
12 | * in the documentation and/or other materials provided with the |
13 | * distribution. |
14 | * * Neither the name of Google Inc. nor the names of its |
15 | * contributors may be used to endorse or promote products derived from |
16 | * this software without specific prior written permission. |
17 | * |
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
21 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
22 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
23 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
24 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
28 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 | */ |
30 | |
31 | #include "config.h" |
32 | #include "GridPosition.h" |
33 | |
34 | namespace WebCore { |
35 | |
36 | Optional<int> GridPosition::gMaxPositionForTesting; |
37 | static const int kGridMaxPosition = 1000000; |
38 | |
39 | void GridPosition::setExplicitPosition(int position, const String& namedGridLine) |
40 | { |
41 | m_type = ExplicitPosition; |
42 | setIntegerPosition(position); |
43 | m_namedGridLine = namedGridLine; |
44 | } |
45 | |
46 | void GridPosition::setAutoPosition() |
47 | { |
48 | m_type = AutoPosition; |
49 | m_integerPosition = 0; |
50 | } |
51 | |
52 | // 'span' values cannot be negative, yet we reuse the <integer> position which can |
53 | // be. This means that we have to convert the span position to an integer, losing |
54 | // some precision here. It shouldn't be an issue in practice though. |
55 | void GridPosition::setSpanPosition(int position, const String& namedGridLine) |
56 | { |
57 | m_type = SpanPosition; |
58 | setIntegerPosition(position); |
59 | m_namedGridLine = namedGridLine; |
60 | } |
61 | |
62 | void GridPosition::setNamedGridArea(const String& namedGridArea) |
63 | { |
64 | m_type = NamedGridAreaPosition; |
65 | m_namedGridLine = namedGridArea; |
66 | } |
67 | |
68 | int GridPosition::integerPosition() const |
69 | { |
70 | ASSERT(type() == ExplicitPosition); |
71 | return m_integerPosition; |
72 | } |
73 | |
74 | String GridPosition::namedGridLine() const |
75 | { |
76 | ASSERT(type() == ExplicitPosition || type() == SpanPosition || type() == NamedGridAreaPosition); |
77 | return m_namedGridLine; |
78 | } |
79 | |
80 | int GridPosition::spanPosition() const |
81 | { |
82 | ASSERT(type() == SpanPosition); |
83 | return m_integerPosition; |
84 | } |
85 | |
86 | int GridPosition::max() |
87 | { |
88 | return gMaxPositionForTesting.valueOr(kGridMaxPosition); |
89 | } |
90 | |
91 | int GridPosition::min() |
92 | { |
93 | return -max(); |
94 | } |
95 | |
96 | bool GridPosition::operator==(const GridPosition& other) const |
97 | { |
98 | return m_type == other.m_type && m_integerPosition == other.m_integerPosition && m_namedGridLine == other.m_namedGridLine; |
99 | } |
100 | |
101 | void GridPosition::setMaxPositionForTesting(unsigned maxPosition) |
102 | { |
103 | gMaxPositionForTesting = static_cast<int>(maxPosition); |
104 | } |
105 | |
106 | } // namespace WebCore |
107 | |