summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/layout/ng/ng_relative_utils.cc
blob: 9a48401064a84dac71857de3f14e19d22d391b28 (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
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "third_party/blink/renderer/core/layout/ng/ng_relative_utils.h"

#include "base/optional.h"
#include "third_party/blink/renderer/core/layout/ng/geometry/ng_physical_offset.h"
#include "third_party/blink/renderer/core/layout/ng/geometry/ng_physical_size.h"
#include "third_party/blink/renderer/core/style/computed_style.h"
#include "third_party/blink/renderer/platform/length_functions.h"

namespace blink {

// Returns the child's relative position wrt the containing fragment.
NGPhysicalOffset ComputeRelativeOffset(const ComputedStyle& child_style,
                                       WritingMode container_writing_mode,
                                       TextDirection container_direction,
                                       NGPhysicalSize container_size) {
  NGPhysicalOffset offset;
  if (child_style.GetPosition() != EPosition::kRelative)
    return offset;

  base::Optional<LayoutUnit> left, right, top, bottom;

  if (!child_style.Left().IsAuto())
    left = ValueForLength(child_style.Left(), container_size.width);
  if (!child_style.Right().IsAuto())
    right = ValueForLength(child_style.Right(), container_size.width);
  if (!child_style.Top().IsAuto())
    top = ValueForLength(child_style.Top(), container_size.height);
  if (!child_style.Bottom().IsAuto())
    bottom = ValueForLength(child_style.Bottom(), container_size.height);

  // Common case optimization
  if (!left && !right && !top && !bottom)
    return offset;

  // Implements confict resolution rules from spec:
  // https://www.w3.org/TR/css-position-3/#rel-pos
  if (!left && !right) {
    left = LayoutUnit();
    right = LayoutUnit();
  }
  if (!left)
    left = -right.value();
  if (!right)
    right = -left.value();
  if (!top && !bottom) {
    top = LayoutUnit();
    bottom = LayoutUnit();
  }
  if (!top)
    top = -bottom.value();
  if (!bottom)
    bottom = -top.value();

  if (IsHorizontalWritingMode(container_writing_mode)) {
    if (IsLtr(container_direction))
      offset.left = left.value();
    else
      offset.left = -right.value();
    offset.top = top.value();
  } else {
    if (IsLtr(container_direction))
      offset.top = top.value();
    else
      offset.top = -bottom.value();
    offset.left = left.value();
  }
  return offset;
}

}  // namespace blink