Fix computed style for relative offsets when they're specified in |ch| units. Bug 391034, r+sr+a=dbaron
authorbzbarsky@mit.edu
Thu, 16 Aug 2007 16:19:25 -0700
changeset 4739 2de8ebb61c2d83dd5b5a21b0b056d516a2355ba7
parent 4738 43426064299d12eaa0ecbb312e90b3d16e7cc93e
child 4740 103489b531f1caa0a6fb2b8aefb29dd350aa269c
push idunknown
push userunknown
push dateunknown
bugs391034
milestone1.9a8pre
Fix computed style for relative offsets when they're specified in |ch| units. Bug 391034, r+sr+a=dbaron
layout/style/nsComputedDOMStyle.cpp
layout/style/test/Makefile.in
layout/style/test/test_bug391034.html
--- a/layout/style/nsComputedDOMStyle.cpp
+++ b/layout/style/nsComputedDOMStyle.cpp
@@ -2693,50 +2693,36 @@ nsComputedDOMStyle::GetRelativeOffset(PR
 {
   nsROCSSPrimitiveValue *val = GetROCSSPrimitiveValue();
   NS_ENSURE_TRUE(val, NS_ERROR_OUT_OF_MEMORY);
 
   const nsStylePosition* positionData = GetStylePosition();
   nsStyleCoord coord;
   PRInt32 sign = 1;
   positionData->mOffset.Get(aSide, coord);
-  if (coord.GetUnit() != eStyleUnit_Coord &&
-      coord.GetUnit() != eStyleUnit_Percent) {
+
+  NS_ASSERTION(coord.GetUnit() == eStyleUnit_Coord ||
+               coord.GetUnit() == eStyleUnit_Percent ||
+               coord.GetUnit() == eStyleUnit_Chars ||
+               coord.GetUnit() == eStyleUnit_Auto,
+               "Unexpected unit");
+  
+  if (coord.GetUnit() == eStyleUnit_Auto) {
     positionData->mOffset.Get(NS_OPPOSITE_SIDE(aSide), coord);
     sign = -1;
   }
-  nsIFrame* container = nsnull;
-  switch(coord.GetUnit()) {
-    case eStyleUnit_Coord:
-      val->SetAppUnits(sign * coord.GetCoordValue());
-      break;
-    case eStyleUnit_Percent:
-      container = GetContainingBlockFor(mFrame);
-      if (container) {
-        nsSize size = container->GetContentRect().Size();
-        if (aSide == NS_SIDE_LEFT || aSide == NS_SIDE_RIGHT) {
-          val->SetAppUnits(sign * coord.GetPercentValue() * size.width);
-        } else {
-          val->SetAppUnits(sign * coord.GetPercentValue() * size.height);
-        }
-      } else {
-        // XXX no containing block.
-        val->SetAppUnits(0);
-      }
-      break;
-    default:
-      NS_ERROR("Unexpected left/right/top/bottom unit");
-      // Fall through
-    case eStyleUnit_Auto:
-      // In this case, both this side and the opposite side are auto.
-      // The computed offset is 0.
-      val->SetAppUnits(0);
-      break;
+  PercentageBaseGetter baseGetter;
+  if (aSide == NS_SIDE_LEFT || aSide == NS_SIDE_RIGHT) {
+    baseGetter = &nsComputedDOMStyle::GetCBContentWidth;
+  } else {
+    baseGetter = &nsComputedDOMStyle::GetCBContentHeight;
   }
 
+  val->SetAppUnits(sign * StyleCoordToNSCoord(coord, baseGetter, 0));
+
   return CallQueryInterface(val, aValue);
 }
 
 nsresult
 nsComputedDOMStyle::GetStaticOffset(PRUint8 aSide, nsIDOMCSSValue** aValue)
 
 {
   nsROCSSPrimitiveValue *val = GetROCSSPrimitiveValue();
--- a/layout/style/test/Makefile.in
+++ b/layout/style/test/Makefile.in
@@ -75,16 +75,17 @@ css_properties.js: host_ListCSSPropertie
 		test_bug372770.html \
 		test_bug373293.html \
 		test_bug377947.html \
 		test_bug379440.html \
 		test_bug379741.html \
 		test_bug383075.html \
 		test_bug387615.html \
 		test_bug389464.html \
+		test_bug391034.html \
 		test_bug391221.html \
 		test_compute_data_with_start_struct.html \
 		test_dont_use_document_colors.html \
 		test_inherit_storage.html \
 		test_inherit_computation.html \
 		test_initial_storage.html \
 		test_initial_computation.html \
 		test_property_database.html \
new file mode 100644
--- /dev/null
+++ b/layout/style/test/test_bug391034.html
@@ -0,0 +1,72 @@
+<!DOCTYPE HTML>
+<html>
+<!--
+https://bugzilla.mozilla.org/show_bug.cgi?id=391034
+-->
+<head>
+  <title>Test for Bug 391034</title>
+  <script type="text/javascript" src="/MochiKit/MochiKit.js"></script>
+  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
+  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
+</head>
+<body>
+<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=391034">Mozilla Bug 391034</a>
+<div id="display" style="width: 90px; height: 80px">
+  <div id="width-ref" style="width: 2ch"></div>
+  <div id="width-ref2" style="width: 5ch"></div>
+  <div id="one" style="position: relative; left: 2ch; bottom: 5ch"></div>  
+  <div id="two" style="position: relative; left: 10%; bottom: 20%"></div>  
+  <div id="three" style="position: relative; left: 10px; bottom: 6px"></div>
+</div>
+<div id="content" style="display: none">
+  <div id="four" style="position: relative; left: 10%; bottom: 20%"></div>  
+</div>
+<pre id="test">
+<script class="testbody" type="text/javascript">
+
+/** Test for Bug 391034 **/
+function getComp(id) {
+ return document.defaultView.getComputedStyle($(id), "");
+}
+
+is(getComp("one").top, "-" + getComp("width-ref2").width,
+   "Incorrect computed top offset if specified in ch")
+is(getComp("one").right, "-" + getComp("width-ref").width,
+   "Incorrect computed right offset if specified in ch")
+is(getComp("one").bottom, getComp("width-ref2").width,
+   "Incorrect computed bottom offset if specified in ch")
+is(getComp("one").left, getComp("width-ref").width,
+   "Incorrect computed left offset if specified in ch")
+
+is(getComp("two").top, "-16px",
+   "Incorrect computed top offset if specified in %")
+is(getComp("two").right, "-9px",
+   "Incorrect computed right offset if specified in %")
+is(getComp("two").bottom, "16px",
+   "Incorrect computed bottom offset if specified in %")
+is(getComp("two").left, "9px",
+   "Incorrect computed left offset if specified in %")
+
+is(getComp("three").top, "-6px",
+   "Incorrect computed top offset if specified in %")
+is(getComp("three").right, "-10px",
+   "Incorrect computed right offset if specified in %")
+is(getComp("three").bottom, "6px",
+   "Incorrect computed bottom offset if specified in %")
+is(getComp("three").left, "10px",
+   "Incorrect computed left offset if specified in %")
+
+is(getComp("four").top, "0px",
+   "Incorrect undisplayed computed top offset if specified in %")
+is(getComp("four").right, "0px",
+   "Incorrect undisplayed computed right offset if specified in %")
+is(getComp("four").bottom, "0px",
+   "Incorrect undisplayed computed bottom offset if specified in %")
+is(getComp("four").left, "0px",
+   "Incorrect undisplayed computed left offset if specified in %")
+
+</script>
+</pre>
+</body>
+</html>
+