Constraint space for current child should be adjusted to offset.

As we now have space_for_current_child_ it should be used to search layout opportunities for floats. Also space_for_current_child_'s offset should be adjusted according to the available space.

List of changes:
- Change FindLayoutOpportunityForFragment to use space_for_current_child_.
- Rename CreateConstraintSpaceForChild -> CreateConstraintSpaceForCurrentChild and make it a private class's method.
- Adjust space_for_current_child_'s offset to the available space.
- Change NGBlockLayoutAlgorithmTest::PositionFloatFragments to include a regular block between floats.

BUG=635619

Review-Url: https://codereview.chromium.org/2468113003
Cr-Commit-Position: refs/heads/master@{#429622}
diff --git a/third_party/WebKit/Source/core/layout/ng/ng_block_layout_algorithm.cc b/third_party/WebKit/Source/core/layout/ng/ng_block_layout_algorithm.cc
index 1e8134b..7bc4484 100644
--- a/third_party/WebKit/Source/core/layout/ng/ng_block_layout_algorithm.cc
+++ b/third_party/WebKit/Source/core/layout/ng/ng_block_layout_algorithm.cc
@@ -81,18 +81,11 @@
 // Calculates the logical offset for opportunity.
 NGLogicalOffset CalculateLogicalOffsetForOpportunity(
     const NGLayoutOpportunity& opportunity,
-    NGBoxStrut border_padding,
     LayoutUnit float_offset,
     NGBoxStrut margins) {
-  // TODO(layout-ng): create children_constraint_space with an offset for the
-  // border and padding.
-  // Offset from parent's border/padding.
-  LayoutUnit inline_offset = border_padding.inline_start;
-  LayoutUnit block_offset = border_padding.block_start;
-
   // Adjust to child's margin.
-  inline_offset += margins.inline_start;
-  block_offset += margins.block_start;
+  LayoutUnit inline_offset = margins.inline_start;
+  LayoutUnit block_offset = margins.block_start;
 
   // Offset from the opportunity's block/inline start.
   inline_offset += opportunity.offset.inline_offset;
@@ -138,17 +131,6 @@
   return false;
 }
 
-// Creates a new constraint space for a child.
-NGConstraintSpace* CreateConstraintSpaceForChild(
-    const NGConstraintSpace& space,
-    const NGBox& child,
-    NGConstraintSpaceBuilder* builder) {
-  builder->SetIsNewFormattingContext(
-      IsNewFormattingContextForInFlowBlockLevelChild(space, *child.Style()));
-  return new NGConstraintSpace(space.WritingMode(), space.Direction(),
-                               builder->ToConstraintSpace());
-}
-
 }  // namespace
 
 NGBlockLayoutAlgorithm::NGBlockLayoutAlgorithm(
@@ -200,8 +182,7 @@
       builder_->SetInlineSize(inline_size).SetBlockSize(block_size);
       current_child_ = first_child_;
       if (current_child_)
-        space_for_current_child_ = CreateConstraintSpaceForChild(
-            *constraint_space_, *current_child_, space_builder_);
+        space_for_current_child_ = CreateConstraintSpaceForCurrentChild();
 
       state_ = kStateChildLayout;
       return false;
@@ -212,8 +193,7 @@
           return false;
         current_child_ = current_child_->NextSibling();
         if (current_child_) {
-          space_for_current_child_ = CreateConstraintSpaceForChild(
-              *constraint_space_, *current_child_, space_builder_);
+          space_for_current_child_ = CreateConstraintSpaceForCurrentChild();
           return false;
         }
       }
@@ -348,8 +328,8 @@
     const NGBoxStrut& margins) {
   // TODO(glebl@chromium.org): Support the top edge alignment rule.
   // Find a layout opportunity that will fit our float.
-  const NGLayoutOpportunity opportunity =
-      FindLayoutOpportunityForFragment(constraint_space_, fragment, margins);
+  const NGLayoutOpportunity opportunity = FindLayoutOpportunityForFragment(
+      space_for_current_child_, fragment, margins);
   DCHECK(!opportunity.IsEmpty()) << "Opportunity is empty but it shouldn't be";
 
   // Calculate the float offset if needed.
@@ -363,8 +343,8 @@
       CreateExclusion(fragment, opportunity, float_offset, margins);
   constraint_space_->AddExclusion(exclusion);
 
-  return CalculateLogicalOffsetForOpportunity(opportunity, border_and_padding_,
-                                              float_offset, margins);
+  return CalculateLogicalOffsetForOpportunity(opportunity, float_offset,
+                                              margins);
 }
 
 void NGBlockLayoutAlgorithm::UpdateMarginStrut(const NGMarginStrut& from) {
@@ -375,6 +355,28 @@
   builder_->SetMarginStrutBlockEnd(from);
 }
 
+NGConstraintSpace*
+NGBlockLayoutAlgorithm::CreateConstraintSpaceForCurrentChild() const {
+  DCHECK(current_child_);
+  space_builder_->SetIsNewFormattingContext(
+      IsNewFormattingContextForInFlowBlockLevelChild(*constraint_space_,
+                                                     *current_child_->Style()));
+  NGConstraintSpace* child_space = new NGConstraintSpace(
+      constraint_space_->WritingMode(), constraint_space_->Direction(),
+      space_builder_->ToConstraintSpace());
+
+  // TODO(layout-ng): Set offset through the space builder.
+  child_space->SetOffset(
+      NGLogicalOffset(border_and_padding_.inline_start, content_size_));
+
+  // TODO(layout-ng): avoid copying here. A child and parent constraint spaces
+  // should share the same backing space.
+  for (const auto& exclusion : constraint_space_->Exclusions()) {
+    child_space->AddExclusion(*exclusion.get());
+  }
+  return child_space;
+}
+
 DEFINE_TRACE(NGBlockLayoutAlgorithm) {
   NGLayoutAlgorithm::trace(visitor);
   visitor->trace(first_child_);