1 /*
2  * Copyright (C) 2012 Google Inc. All rights reserved.
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 #ifndef THIRD_PARTY_BLINK_RENDERER_CORE_STYLE_GRID_POSITION_H_
32 #define THIRD_PARTY_BLINK_RENDERER_CORE_STYLE_GRID_POSITION_H_
33 
34 #include "third_party/blink/renderer/platform/wtf/allocator/allocator.h"
35 #include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
36 
37 namespace blink {
38 
39 enum GridPositionType {
40   kAutoPosition,
41   kExplicitPosition,      // [ <integer> || <string> ]
42   kSpanPosition,          // span && [ <integer> || <string> ]
43   kNamedGridAreaPosition  // <ident>
44 };
45 
46 class GridPosition {
47   DISALLOW_NEW();
48 
49  public:
GridPosition()50   GridPosition() : type_(kAutoPosition), integer_position_(0) {}
51 
IsPositive()52   bool IsPositive() const { return IntegerPosition() > 0; }
53 
GetType()54   GridPositionType GetType() const { return type_; }
IsAuto()55   bool IsAuto() const { return type_ == kAutoPosition; }
IsSpan()56   bool IsSpan() const { return type_ == kSpanPosition; }
IsNamedGridArea()57   bool IsNamedGridArea() const { return type_ == kNamedGridAreaPosition; }
58 
SetExplicitPosition(int position,const AtomicString & named_grid_line)59   void SetExplicitPosition(int position, const AtomicString& named_grid_line) {
60     type_ = kExplicitPosition;
61     integer_position_ = position;
62     named_grid_line_ = named_grid_line;
63   }
64 
SetAutoPosition()65   void SetAutoPosition() {
66     type_ = kAutoPosition;
67     integer_position_ = 0;
68   }
69 
70   // 'span' values cannot be negative, yet we reuse the <integer> position which
71   // can be. This means that we have to convert the span position to an integer,
72   // losing some precision here. It shouldn't be an issue in practice though.
SetSpanPosition(int position,const AtomicString & named_grid_line)73   void SetSpanPosition(int position, const AtomicString& named_grid_line) {
74     type_ = kSpanPosition;
75     integer_position_ = position;
76     named_grid_line_ = named_grid_line;
77   }
78 
SetNamedGridArea(const AtomicString & named_grid_area)79   void SetNamedGridArea(const AtomicString& named_grid_area) {
80     type_ = kNamedGridAreaPosition;
81     named_grid_line_ = named_grid_area;
82   }
83 
IntegerPosition()84   int IntegerPosition() const {
85     DCHECK_EQ(GetType(), kExplicitPosition);
86     return integer_position_;
87   }
88 
NamedGridLine()89   AtomicString NamedGridLine() const {
90     DCHECK(GetType() == kExplicitPosition || GetType() == kSpanPosition ||
91            GetType() == kNamedGridAreaPosition);
92     return named_grid_line_;
93   }
94 
SpanPosition()95   int SpanPosition() const {
96     DCHECK_EQ(GetType(), kSpanPosition);
97     return integer_position_;
98   }
99 
100   bool operator==(const GridPosition& other) const {
101     return type_ == other.type_ &&
102            integer_position_ == other.integer_position_ &&
103            named_grid_line_ == other.named_grid_line_;
104   }
105 
106   bool operator!=(const GridPosition& other) const { return !(*this == other); }
107 
ShouldBeResolvedAgainstOppositePosition()108   bool ShouldBeResolvedAgainstOppositePosition() const {
109     return IsAuto() || IsSpan();
110   }
111 
112  private:
113   GridPositionType type_;
114   int integer_position_;
115   AtomicString named_grid_line_;
116 };
117 
118 }  // namespace blink
119 
120 #endif  // THIRD_PARTY_BLINK_RENDERER_CORE_STYLE_GRID_POSITION_H_
121