Computed min-width/height for auto depends on box.

Resolved value of min-width and min-height for auto min sizing of flex
and grid items may be 'auto'. We based this on the computed style of the
shadow including parent. Instead we should rely on whether the element
will actually be a rendered flex/grid item.

Firefox Nightly passes all added tests.

The mentioned bug fixed is actually about using the shadow including
parent for finding parent computed style instead of the flat tree
parent, but that is no longer relevant as we look at the layout tree.

Bug: 682869
Change-Id: I6e47dc264a8cdbee7fc1983ce452baf7c0df85b3
Reviewed-on: https://chromium-review.googlesource.com/948908
Reviewed-by: Morten Stenshorne <mstensho@chromium.org>
Commit-Queue: Rune Lillesveen <futhark@chromium.org>
Cr-Commit-Position: refs/heads/master@{#540901}
diff --git a/third_party/WebKit/LayoutTests/external/wpt/css/css-flexbox/getcomputedstyle/flexbox_computedstyle_min-auto-size.html b/third_party/WebKit/LayoutTests/external/wpt/css/css-flexbox/getcomputedstyle/flexbox_computedstyle_min-auto-size.html
new file mode 100644
index 0000000..ccfeb35
--- /dev/null
+++ b/third_party/WebKit/LayoutTests/external/wpt/css/css-flexbox/getcomputedstyle/flexbox_computedstyle_min-auto-size.html
@@ -0,0 +1,49 @@
+<!DOCTYPE html>
+<title>CSS Flexible Box Test: computed style for auto minimum size</title>
+<link rel="author" title="Rune Lillesveen" href="mailto:futhark@chromium.org" />
+<link rel="help" href="https://drafts.csswg.org/css-flexbox/#min-size-auto" />
+<script src="/resources/testharness.js"></script>
+<script src="/resources/testharnessreport.js"></script>
+<style>
+  .flex { display: flex }
+  .none { display: none }
+  .min-wh {
+    min-width: auto;
+    min-height: auto;
+  }
+  .contents { display: contents }
+</style>
+<div class="flex">
+  <div class="min-wh"></div>
+</div>
+<div class="none">
+  <div class="flex">
+    <div class="min-wh"></div>
+  </div>
+</div>
+<div class="flex">
+  <div class="contents">
+    <div class="min-wh"></div>
+  </div>
+</div>
+<div class="flex">
+  <div class="min-wh none"></div>
+</div>
+<script>
+  const tests = [
+    { description: "Computed min-width/min-height of specified auto for flex item.", computed: "auto" },
+    { description: "Computed min-width/min-height of specified auto inside display:none which would otherwise have been a flex item.", computed: "0px" },
+    { description: "Computed min-width/min-height of specified auto for flex item inside display:contents.", computed: "auto" },
+    { description: "Computed min-width/min-height of specified auto with display:none which would otherwise have been a flex item.", computed: "0px" }
+  ];
+
+  const testElements = document.querySelectorAll(".min-wh");
+  let testNo = 0;
+  for (let testElement of testElements) {
+    test(() => {
+      assert_equals(getComputedStyle(testElement).minWidth, tests[testNo].computed);
+      assert_equals(getComputedStyle(testElement).minHeight, tests[testNo].computed);
+    }, tests[testNo].description);
+    testNo++;
+  }
+</script>
diff --git a/third_party/WebKit/LayoutTests/external/wpt/css/css-grid/grid-items/grid-item-min-auto-size-001.html b/third_party/WebKit/LayoutTests/external/wpt/css/css-grid/grid-items/grid-item-min-auto-size-001.html
new file mode 100644
index 0000000..f50e9ef3
--- /dev/null
+++ b/third_party/WebKit/LayoutTests/external/wpt/css/css-grid/grid-items/grid-item-min-auto-size-001.html
@@ -0,0 +1,49 @@
+<!DOCTYPE html>
+<title>CSS Grid Test: computed style for auto minimum size</title>
+<link rel="author" title="Rune Lillesveen" href="mailto:futhark@chromium.org" />
+<link rel="help" href="https://drafts.csswg.org/css-grid/#min-size-auto" />
+<script src="/resources/testharness.js"></script>
+<script src="/resources/testharnessreport.js"></script>
+<style>
+  .grid { display: grid }
+  .none { display: none }
+  .min-wh {
+    min-width: auto;
+    min-height: auto;
+  }
+  .contents { display: contents }
+</style>
+<div class="grid">
+  <div class="min-wh"></div>
+</div>
+<div class="none">
+  <div class="grid">
+    <div class="min-wh"></div>
+  </div>
+</div>
+<div class="grid">
+  <div class="contents">
+    <div class="min-wh"></div>
+  </div>
+</div>
+<div class="grid">
+  <div class="min-wh none"></div>
+</div>
+<script>
+  const tests = [
+    { description: "Computed min-width/min-height of specified auto for grid item.", computed: "auto" },
+    { description: "Computed min-width/min-height of specified auto inside display:none which would otherwise have been a grid item.", computed: "0px" },
+    { description: "Computed min-width/min-height of specified auto for grid item inside display:contents.", computed: "auto" },
+    { description: "Computed min-width/min-height of specified auto with display:none which would otherwise have been a grid item.", computed: "0px" }
+  ];
+
+  const testElements = document.querySelectorAll(".min-wh");
+  let testNo = 0;
+  for (let testElement of testElements) {
+    test(() => {
+      assert_equals(getComputedStyle(testElement).minWidth, tests[testNo].computed);
+      assert_equals(getComputedStyle(testElement).minHeight, tests[testNo].computed);
+    }, tests[testNo].description);
+    testNo++;
+  }
+</script>
diff --git a/third_party/WebKit/Source/core/css/properties/ComputedStyleUtils.cpp b/third_party/WebKit/Source/core/css/properties/ComputedStyleUtils.cpp
index 7b5b926..81fb971 100644
--- a/third_party/WebKit/Source/core/css/properties/ComputedStyleUtils.cpp
+++ b/third_party/WebKit/Source/core/css/properties/ComputedStyleUtils.cpp
@@ -497,14 +497,13 @@
 CSSValue* ComputedStyleUtils::MinWidthOrMinHeightAuto(
     Node* styled_node,
     const ComputedStyle& style) {
-  if (styled_node) {
-    Node* parent = styled_node->parentNode();
-    const ComputedStyle* ensured_style =
-        parent ? parent->EnsureComputedStyle() : nullptr;
-    if (ensured_style && ensured_style->IsDisplayFlexibleOrGridBox())
-      return CSSIdentifierValue::Create(CSSValueAuto);
+  LayoutObject* layout_object =
+      styled_node ? styled_node->GetLayoutObject() : nullptr;
+  if (layout_object && layout_object->IsBox() &&
+      (ToLayoutBox(layout_object)->IsFlexItem() ||
+       ToLayoutBox(layout_object)->IsGridItem())) {
+    return CSSIdentifierValue::Create(CSSValueAuto);
   }
-
   return ZoomAdjustedPixelValue(0, style);
 }