[mathml] Fix MathML layout algorithms for empty children with negative margins

This CL adds two WPT crashtests for the edge cases of MathML elements
having empty children with negative margins, testing separately the
inline and block cases:

mathml/crashtests/children-with-negative-inline-sizes.html
mathml/crashtests/children-with-negative-block-sizes.html

The former almost works without problem, but it's currently failing the
assertion that min size is at most the max size for the <mroot> element.
This is due to a mistake in the min/max calculation and is fixed by
aligning with the MathML Core spec [1]. This also makes existing
mathml/presentation-markup/mrow/no-spacing.html test pass.

The latter is failing the assertion that block intrinsic size includes
at least the border/padding, for the algorithms related to fraction and
scripted elements. This is because the calculated ascent/descent of the
content boxes can be negative for the empty children with negative
margins. This is fixed by clamping the calculated ascent/descent to 0
before adding the element's own border/padding. For that purpose, a bit
of code refactoring is also done here.

[1] https://w3c.github.io/mathml-core/#root-with-index

Bug: 1342307
Change-Id: I00db8d423f163c14cc2ef34dcccde8a3a086e036
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3858293
Commit-Queue: Frédéric Wang <fwang@igalia.com>
Reviewed-by: Ian Kilpatrick <ikilpatrick@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1041390}
diff --git a/mathml/crashtests/children-with-negative-block-sizes.html b/mathml/crashtests/children-with-negative-block-sizes.html
new file mode 100644
index 0000000..c50f9be
--- /dev/null
+++ b/mathml/crashtests/children-with-negative-block-sizes.html
@@ -0,0 +1,142 @@
+<!DOCTYPE html>
+<style>
+  /* These are children with negative block sizes. Absolute values are chosen
+     large enough compared to math layout constants. */
+  span { display: inline-block; }
+  mspace, span {
+    margin-block-start: -10em;
+    margin-block-end: -10em;
+  }
+  /* Override display: none; from UA style sheet for the following children. */
+  maction > :not(:first-child),
+  semantics > annotation,
+  semantics > annotation-xml {
+    display: math;
+  }
+</style>
+<math>
+  <mi><span></span></mi>
+  <mn><span></span></mn>
+  <mo><span></span></mo>
+  <ms><span></span></ms>
+  <mtext><span></span></mtext>
+</math>
+<math>
+  <semantics>
+    <mspace/>
+    <annotation><span></span></annotation>
+    <annotation-xml><span></span></annotation-xml>
+  </semantics>
+</math>
+<math>
+  <mtable>
+    <mtr>
+      <mtd><mspace/></mtd>
+      <mtd><mspace/></mtd>
+      <mtd><mspace/></mtd>
+    </mtr>
+    <mtr>
+      <mtd><mspace/></mtd>
+      <mtd><mspace/></mtd>
+      <mtd><mspace/></mtd>
+    </mtr>
+  </mtable>
+</math>
+<math>
+  <mspace/>
+  <mspace/>
+  <mspace/>
+  <mspace/>
+</math>
+<math>
+  <maction>
+    <mspace/>
+    <mspace/>
+    <mspace/>
+    <mspace/>
+  </maction>
+  <merror>
+    <mspace/>
+    <mspace/>
+    <mspace/>
+    <mspace/>
+  </merror>
+  <mpadded>
+    <mspace/>
+    <mspace/>
+    <mspace/>
+    <mspace/>
+  </mpadded>
+  <mphantom>
+    <mspace/>
+    <mspace/>
+    <mspace/>
+    <mspace/>
+  </mphantom>
+  <mstyle>
+    <mspace/>
+    <mspace/>
+    <mspace/>
+    <mspace/>
+  </mstyle>
+</math>
+<math>
+  <mfrac>
+    <mspace/>
+    <mspace/>
+  </mfrac>
+  <mfrac linethickness="0">
+    <mspace/>
+    <mspace/>
+  </mfrac>
+</math>
+<math>
+  <msqrt>
+    <mspace/>
+    <mspace/>
+    <mspace/>
+    <mspace/>
+  </msqrt>
+  <mroot>
+    <mspace/>
+    <mspace/>
+  </mroot>
+</math>
+<math>
+  <msup>
+    <mspace/>
+    <mspace/>
+  </msup>
+  <msub>
+    <mspace/>
+    <mspace/>
+  </msub>
+  <msubsup>
+    <mspace/>
+    <mspace/>
+    <mspace/>
+  </msubsup>
+  <mmultiscripts>
+    <mspace/>
+    <mspace/>
+    <mspace/>
+    <mprescripts/>
+    <mspace/>
+    <mspace/>
+  </mmultiscripts>
+</math>
+<math>
+  <mover>
+    <mspace/>
+    <mspace/>
+  </mover>
+  <munder>
+    <mspace/>
+    <mspace/>
+  </munder>
+  <munderover>
+    <mspace/>
+    <mspace/>
+    <mspace/>
+  </munderover>
+</math>
diff --git a/mathml/crashtests/children-with-negative-inline-sizes.html b/mathml/crashtests/children-with-negative-inline-sizes.html
new file mode 100644
index 0000000..5c41fc8
--- /dev/null
+++ b/mathml/crashtests/children-with-negative-inline-sizes.html
@@ -0,0 +1,142 @@
+<!DOCTYPE html>
+<style>
+  /* These are children with negative block sizes. Absolute values are chosen
+     large enough compared to math layout constants. */
+  span { display: inline-block; }
+  mspace, span {
+    margin-inline-start: -10em;
+    margin-inline-end: -10em;
+  }
+  /* Override display: none; from UA style sheet for the following children. */
+  maction > :not(:first-child),
+  semantics > annotation,
+  semantics > annotation-xml {
+    display: math;
+  }
+</style>
+<math>
+  <mi><span></span></mi>
+  <mn><span></span></mn>
+  <mo><span></span></mo>
+  <ms><span></span></ms>
+  <mtext><span></span></mtext>
+</math>
+<math>
+  <semantics>
+    <mspace/>
+    <annotation><span></span></annotation>
+    <annotation-xml><span></span></annotation-xml>
+  </semantics>
+</math>
+<math>
+  <mtable>
+    <mtr>
+      <mtd><mspace/></mtd>
+      <mtd><mspace/></mtd>
+      <mtd><mspace/></mtd>
+    </mtr>
+    <mtr>
+      <mtd><mspace/></mtd>
+      <mtd><mspace/></mtd>
+      <mtd><mspace/></mtd>
+    </mtr>
+  </mtable>
+</math>
+<math>
+  <mspace/>
+  <mspace/>
+  <mspace/>
+  <mspace/>
+</math>
+<math>
+  <maction>
+    <mspace/>
+    <mspace/>
+    <mspace/>
+    <mspace/>
+  </maction>
+  <merror>
+    <mspace/>
+    <mspace/>
+    <mspace/>
+    <mspace/>
+  </merror>
+  <mpadded>
+    <mspace/>
+    <mspace/>
+    <mspace/>
+    <mspace/>
+  </mpadded>
+  <mphantom>
+    <mspace/>
+    <mspace/>
+    <mspace/>
+    <mspace/>
+  </mphantom>
+  <mstyle>
+    <mspace/>
+    <mspace/>
+    <mspace/>
+    <mspace/>
+  </mstyle>
+</math>
+<math>
+  <mfrac>
+    <mspace/>
+    <mspace/>
+  </mfrac>
+  <mfrac linethickness="0">
+    <mspace/>
+    <mspace/>
+  </mfrac>
+</math>
+<math>
+  <msqrt>
+    <mspace/>
+    <mspace/>
+    <mspace/>
+    <mspace/>
+  </msqrt>
+  <mroot>
+    <mspace/>
+    <mspace/>
+  </mroot>
+</math>
+<math>
+  <msup>
+    <mspace/>
+    <mspace/>
+  </msup>
+  <msub>
+    <mspace/>
+    <mspace/>
+  </msub>
+  <msubsup>
+    <mspace/>
+    <mspace/>
+    <mspace/>
+  </msubsup>
+  <mmultiscripts>
+    <mspace/>
+    <mspace/>
+    <mspace/>
+    <mprescripts/>
+    <mspace/>
+    <mspace/>
+  </mmultiscripts>
+</math>
+<math>
+  <mover>
+    <mspace/>
+    <mspace/>
+  </mover>
+  <munder>
+    <mspace/>
+    <mspace/>
+  </munder>
+  <munderover>
+    <mspace/>
+    <mspace/>
+    <mspace/>
+  </munderover>
+</math>