Bug 1443864: Maybe a bit cleaner. r?xidorn draft
authorEmilio Cobos Álvarez <emilio@crisal.io>
Wed, 07 Mar 2018 19:01:25 +0100
changeset 764334 eee7aaf2fb42ffcbc96f53514b87ced21f024307
parent 764325 45e5823f359ba0694cf109323c0e9e93f10664ea
push id101748
push userbmo:emilio@crisal.io
push dateWed, 07 Mar 2018 18:09:15 +0000
reviewersxidorn
bugs1443864
milestone60.0a1
Bug 1443864: Maybe a bit cleaner. r?xidorn I don't feel too strongly about this patch. If you think it's worth, I'll merge it with the patch above. MozReview-Commit-ID: Apl4GltiH27
xpfe/appshell/nsXULWindow.cpp
--- a/xpfe/appshell/nsXULWindow.cpp
+++ b/xpfe/appshell/nsXULWindow.cpp
@@ -1259,16 +1259,34 @@ ReadIntAttribute(const Element& aElement
     return Nothing();
   }
 
   nsresult res = NS_OK;
   int32_t ret = attrString.ToInteger(&res);
   return NS_SUCCEEDED(res) ? Some(ret) : Nothing();
 }
 
+static Maybe<int32_t>
+ReadSize(const Element& aElement,
+         nsAtom* aAttr,
+         nsAtom* aMinAttr,
+         nsAtom* aMaxAttr)
+{
+  Maybe<int32_t> attr = ReadIntAttribute(aElement, aAttr);
+  if (!attr) {
+    return Nothing();
+  }
+
+  int32_t min = ReadIntAttribute(aElement, aMinAttr).valueOr(100);
+  int32_t max = ReadIntAttribute(aElement, aMaxAttr)
+    .valueOr(std::numeric_limits<int32_t>::max());
+
+  return Some(std::min(max, std::max(*attr, min)));
+}
+
 bool
 nsXULWindow::LoadSizeFromXUL(int32_t& aSpecWidth, int32_t& aSpecHeight)
 {
   bool     gotSize = false;
 
   // if we're the hidden window, don't try to validate our size/position. We're
   // special.
   if (mIsHiddenWindow) {
@@ -1277,37 +1295,29 @@ nsXULWindow::LoadSizeFromXUL(int32_t& aS
 
   nsCOMPtr<dom::Element> windowElement = GetWindowDOMElement();
   NS_ENSURE_TRUE(windowElement, false);
 
   // Obtain the sizing information from the <xul:window> element.
   aSpecWidth = 100;
   aSpecHeight = 100;
 
-  if (auto width = ReadIntAttribute(*windowElement, nsGkAtoms::width)) {
-    int32_t min =
-      ReadIntAttribute(*windowElement, nsGkAtoms::minwidth)
-        .valueOr(100);
-    int32_t max =
-      ReadIntAttribute(*windowElement, nsGkAtoms::maxwidth)
-        .valueOr(std::numeric_limits<int32_t>::max());
-
-    aSpecWidth = std::min(max, std::max(*width, min));
+  if (auto width = ReadSize(*windowElement,
+                            nsGkAtoms::width,
+                            nsGkAtoms::minwidth,
+                            nsGkAtoms::maxwidth)) {
+    aSpecWidth = *width;
     gotSize = true;
   }
 
-  if (auto height = ReadIntAttribute(*windowElement, nsGkAtoms::height)) {
-    int32_t min =
-      ReadIntAttribute(*windowElement, nsGkAtoms::minheight)
-        .valueOr(100);
-    int32_t max =
-      ReadIntAttribute(*windowElement, nsGkAtoms::maxheight)
-        .valueOr(std::numeric_limits<int32_t>::max());
-
-    aSpecHeight = std::min(max, std::max(*height, min));
+  if (auto height = ReadSize(*windowElement,
+                             nsGkAtoms::height,
+                             nsGkAtoms::minheight,
+                             nsGkAtoms::maxheight)) {
+    aSpecHeight = *height;
     gotSize = true;
   }
 
   return gotSize;
 }
 
 void
 nsXULWindow::SetSpecifiedSize(int32_t aSpecWidth, int32_t aSpecHeight)