[CSS Grid Layout] Upgrade align-self and align-items parsing to CSS 3
[WebKit-https.git] / Source / WebCore / rendering / mathml / RenderMathMLScripts.cpp
index cbecaba..d8c3bbd 100644 (file)
@@ -30,7 +30,7 @@
 
 #include "RenderMathMLScripts.h"
 
-#include "MathMLNames.h"
+#include "MathMLElement.h"
 
 namespace WebCore {
     
@@ -50,25 +50,24 @@ using namespace MathMLNames;
 // where BaseWrapper can now be empty and SubSupPairWrapper can now have one or two elements.
 //
 
-static bool isPrescript(RenderObject* renderObject)
+static bool isPrescript(const RenderObject& renderObject)
 {
-    ASSERT(renderObject);
-    return renderObject->node() && renderObject->node()->hasTagName(MathMLNames::mprescriptsTag);
+    return renderObject.node() && renderObject.node()->hasTagName(MathMLNames::mprescriptsTag);
 }
 
-RenderMathMLScripts::RenderMathMLScripts(Element* element)
-    : RenderMathMLBlock(element)
+RenderMathMLScripts::RenderMathMLScripts(Element& element, Ref<RenderStyle>&& style)
+    : RenderMathMLBlock(element, WTF::move(style))
     , m_baseWrapper(0)
 {
     // Determine what kind of sub/sup expression we have by element name
-    if (element->hasLocalName(MathMLNames::msubTag))
+    if (element.hasTagName(MathMLNames::msubTag))
         m_kind = Sub;
-    else if (element->hasLocalName(MathMLNames::msupTag))
+    else if (element.hasTagName(MathMLNames::msupTag))
         m_kind = Super;
-    else if (element->hasLocalName(MathMLNames::msubsupTag))
+    else if (element.hasTagName(MathMLNames::msubsupTag))
         m_kind = SubSup;
     else {
-        ASSERT(element->hasLocalName(MathMLNames::mmultiscriptsTag));
+        ASSERT(element.hasTagName(MathMLNames::mmultiscriptsTag));
         m_kind = Multiscripts;
     }
 }
@@ -76,68 +75,68 @@ RenderMathMLScripts::RenderMathMLScripts(Element* element)
 RenderBoxModelObject* RenderMathMLScripts::base() const
 {
     if (!m_baseWrapper)
-        return 0;
+        return nullptr;
     RenderObject* base = m_baseWrapper->firstChild();
-    if (!base || !base->isBoxModelObject())
-        return 0;
-    return toRenderBoxModelObject(base);
+    if (!is<RenderBoxModelObject>(base))
+        return nullptr;
+    return downcast<RenderBoxModelObject>(base);
 }
 
 void RenderMathMLScripts::fixAnonymousStyleForSubSupPair(RenderObject* subSupPair, bool isPostScript)
 {
-    ASSERT(subSupPair && subSupPair->style()->refCount() == 1);
-    RenderStyle* scriptsStyle = subSupPair->style();
+    ASSERT(subSupPair && subSupPair->style().refCount() == 1);
+    RenderStyle& scriptsStyle = subSupPair->style();
 
     // subSup pairs are drawn in column from bottom (subscript) to top (superscript).
-    scriptsStyle->setFlexDirection(FlowColumnReverse);
+    scriptsStyle.setFlexDirection(FlowColumnReverse);
 
     // The MathML specification does not specify horizontal alignment of
     // scripts. We align the bottom (respectively top) edge of the subscript
     // (respectively superscript) with the bottom (respectively top) edge of
     // the flex container. Note that for valid <msub> and <msup> elements, the
     // subSupPair should actually have only one script.
-    scriptsStyle->setJustifyContent(m_kind == Sub ? JustifyFlexStart : m_kind == Super ? JustifyFlexEnd : JustifySpaceBetween);
+    scriptsStyle.setJustifyContent(m_kind == Sub ? JustifyFlexStart : m_kind == Super ? JustifyFlexEnd : JustifySpaceBetween);
 
     // The MathML specification does not specify vertical alignment of scripts.
     // Let's right align prescripts and left align postscripts.
     // See http://lists.w3.org/Archives/Public/www-math/2012Aug/0006.html
-    scriptsStyle->setAlignItems(isPostScript ? AlignFlexStart : AlignFlexEnd);
+    scriptsStyle.setAlignItems(isPostScript ? ItemPositionFlexStart : ItemPositionFlexEnd);
 
     // We set the order property so that the prescripts are drawn before the base.
-    scriptsStyle->setOrder(isPostScript ? 0 : -1);
+    scriptsStyle.setOrder(isPostScript ? 0 : -1);
 
     // We set this wrapper's font-size for its line-height.
-    LayoutUnit scriptSize = static_cast<int>(0.75 * style()->fontSize());
-    scriptsStyle->setFontSize(scriptSize);
+    LayoutUnit scriptSize = static_cast<int>(0.75 * style().fontSize());
+    scriptsStyle.setFontSize(scriptSize);
 }
 
 void RenderMathMLScripts::fixAnonymousStyles()
 {
     // We set the base wrapper's style so that baseHeight in layout() will be an unstretched height.
-    ASSERT(m_baseWrapper && m_baseWrapper->style()->hasOneRef());
-    m_baseWrapper->style()->setAlignSelf(AlignFlexStart);
+    ASSERT(m_baseWrapper && m_baseWrapper->style().hasOneRef());
+    m_baseWrapper->style().setAlignSelf(ItemPositionFlexStart);
 
     // This sets the style for postscript pairs.
     RenderObject* subSupPair = m_baseWrapper;
-    for (subSupPair = subSupPair->nextSibling(); subSupPair && !isPrescript(subSupPair); subSupPair = subSupPair->nextSibling())
+    for (subSupPair = subSupPair->nextSibling(); subSupPair && !isPrescript(*subSupPair); subSupPair = subSupPair->nextSibling())
         fixAnonymousStyleForSubSupPair(subSupPair, true);
 
     if (subSupPair && m_kind == Multiscripts) {
         // This sets the style for prescript pairs.
-        for (subSupPair = subSupPair->nextSibling(); subSupPair && !isPrescript(subSupPair); subSupPair = subSupPair->nextSibling())
+        for (subSupPair = subSupPair->nextSibling(); subSupPair && !isPrescript(*subSupPair); subSupPair = subSupPair->nextSibling())
             fixAnonymousStyleForSubSupPair(subSupPair, false);
     }
 
     // This resets style for extra subSup pairs.
     for (; subSupPair; subSupPair = subSupPair->nextSibling()) {
-        if (!isPrescript(subSupPair)) {
-            ASSERT(subSupPair && subSupPair->style()->refCount() == 1);
-            RenderStyle* scriptsStyle = subSupPair->style();
-            scriptsStyle->setFlexDirection(FlowRow);
-            scriptsStyle->setJustifyContent(JustifyFlexStart);
-            scriptsStyle->setAlignItems(AlignCenter);
-            scriptsStyle->setOrder(0);
-            scriptsStyle->setFontSize(style()->fontSize());
+        if (!isPrescript(*subSupPair)) {
+            ASSERT(subSupPair && subSupPair->style().refCount() == 1);
+            RenderStyle& scriptsStyle = subSupPair->style();
+            scriptsStyle.setFlexDirection(FlowRow);
+            scriptsStyle.setJustifyContent(JustifyFlexStart);
+            scriptsStyle.setAlignItems(ItemPositionCenter);
+            scriptsStyle.setOrder(0);
+            scriptsStyle.setFontSize(style().fontSize());
         }
     }
 }
@@ -153,9 +152,12 @@ void RenderMathMLScripts::addChildInternal(bool doNotRestructure, RenderObject*
         // beforeChild may be a grandchild, so we call the addChild function of the corresponding wrapper instead.
         RenderObject* parent = beforeChild->parent();
         if (parent != this) {
-            RenderMathMLScriptsWrapper* wrapper = toRenderMathMLScriptsWrapper(parent);
-            wrapper->addChildInternal(false, child, beforeChild);
-            return;
+            RenderMathMLBlock& parentBlock = downcast<RenderMathMLBlock>(*parent);
+            if (is<RenderMathMLScriptsWrapper>(parentBlock)) {
+                RenderMathMLScriptsWrapper& wrapper = downcast<RenderMathMLScriptsWrapper>(parentBlock);
+                wrapper.addChildInternal(false, child, beforeChild);
+                return;
+            }
         }
     }
 
@@ -165,20 +167,20 @@ void RenderMathMLScripts::addChildInternal(bool doNotRestructure, RenderObject*
         return;
     }
     
-    if (isPrescript(child)) {
+    if (isPrescript(*child)) {
         // The new child becomes an <mprescripts/> separator.
         RenderMathMLBlock::addChild(child, beforeChild);
         return;
     }
 
-    if (!beforeChild || isPrescript(beforeChild)) {
+    if (!beforeChild || isPrescript(*beforeChild)) {
         // We are at the end of a sequence of subSup pairs.
-        RenderMathMLBlock* previousSibling = toRenderMathMLBlock(beforeChild ? beforeChild->previousSibling() : lastChild());
-        if (previousSibling && previousSibling->isRenderMathMLScriptsWrapper()) {
-            RenderMathMLScriptsWrapper* wrapper = toRenderMathMLScriptsWrapper(previousSibling);
-            if ((wrapper->m_kind == RenderMathMLScriptsWrapper::Base && wrapper->isEmpty()) || (wrapper->m_kind == RenderMathMLScriptsWrapper::SubSupPair && !wrapper->firstChild()->nextSibling())) {
+        RenderMathMLBlock* previousSibling = downcast<RenderMathMLBlock>(beforeChild ? beforeChild->previousSibling() : lastChild());
+        if (is<RenderMathMLScriptsWrapper>(previousSibling)) {
+            RenderMathMLScriptsWrapper& wrapper = downcast<RenderMathMLScriptsWrapper>(*previousSibling);
+            if ((wrapper.m_kind == RenderMathMLScriptsWrapper::Base && wrapper.isEmpty()) || (wrapper.m_kind == RenderMathMLScriptsWrapper::SubSupPair && !wrapper.firstChild()->nextSibling())) {
                 // The previous sibling is either an empty base or a SubSup pair with a single child so we can insert the new child into that wrapper.
-                wrapper->addChildInternal(true, child);
+                wrapper.addChildInternal(true, child);
                 return;
             }
         }
@@ -190,13 +192,13 @@ void RenderMathMLScripts::addChildInternal(bool doNotRestructure, RenderObject*
     }
 
     // beforeChild is a subSup pair. This is like inserting the new child at the beginning of the subSup wrapper.
-    RenderMathMLScriptsWrapper* wrapper = toRenderMathMLScriptsWrapper(beforeChild);
-    ASSERT(wrapper->m_kind == RenderMathMLScriptsWrapper::SubSupPair);
+    RenderMathMLScriptsWrapper& wrapper = downcast<RenderMathMLScriptsWrapper>(*beforeChild);
+    ASSERT(wrapper.m_kind == RenderMathMLScriptsWrapper::SubSupPair);
     ASSERT(!(m_baseWrapper->isEmpty() && m_baseWrapper->nextSibling() == beforeChild));
-    wrapper->addChildInternal(false, child, wrapper->firstChild());
+    wrapper.addChildInternal(false, child, wrapper.firstChild());
 }
 
-void RenderMathMLScripts::removeChildInternal(bool doNotRestructure, RenderObject* child)
+void RenderMathMLScripts::removeChildInternal(bool doNotRestructure, RenderObject& child)
 {
     if (doNotRestructure) {
         RenderMathMLBlock::removeChild(child);
@@ -205,18 +207,18 @@ void RenderMathMLScripts::removeChildInternal(bool doNotRestructure, RenderObjec
 
     ASSERT(isPrescript(child));
 
-    RenderObject* previousSibling = child->previousSibling();
-    RenderObject* nextSibling = child->nextSibling();
+    RenderObject* previousSibling = child.previousSibling();
+    RenderObject* nextSibling = child.nextSibling();
     ASSERT(previousSibling);
 
-    if (nextSibling && !isPrescript(previousSibling) && !isPrescript(nextSibling)) {
-        RenderMathMLScriptsWrapper* previousWrapper = toRenderMathMLScriptsWrapper(previousSibling);
-        RenderMathMLScriptsWrapper* nextWrapper = toRenderMathMLScriptsWrapper(nextSibling);
-        ASSERT(nextWrapper->m_kind == RenderMathMLScriptsWrapper::SubSupPair && !nextWrapper->isEmpty());
-        if ((previousWrapper->m_kind == RenderMathMLScriptsWrapper::Base && previousWrapper->isEmpty()) || (previousWrapper->m_kind == RenderMathMLScriptsWrapper::SubSupPair && !previousWrapper->firstChild()->nextSibling())) {
-            RenderObject* script = nextWrapper->firstChild();
-            nextWrapper->removeChildInternal(false, script);
-            previousWrapper->addChildInternal(true, script);
+    if (nextSibling && !isPrescript(*previousSibling) && !isPrescript(*nextSibling)) {
+        RenderMathMLScriptsWrapper& previousWrapper = downcast<RenderMathMLScriptsWrapper>(*previousSibling);
+        RenderMathMLScriptsWrapper& nextWrapper = downcast<RenderMathMLScriptsWrapper>(*nextSibling);
+        ASSERT(nextWrapper.m_kind == RenderMathMLScriptsWrapper::SubSupPair && !nextWrapper.isEmpty());
+        if ((previousWrapper.m_kind == RenderMathMLScriptsWrapper::Base && previousWrapper.isEmpty()) || (previousWrapper.m_kind == RenderMathMLScriptsWrapper::SubSupPair && !previousWrapper.firstChild()->nextSibling())) {
+            RenderObject* script = nextWrapper.firstChild();
+            nextWrapper.removeChildInternal(false, *script);
+            previousWrapper.addChildInternal(true, script);
         }
     }
 
@@ -235,7 +237,7 @@ void RenderMathMLScripts::addChild(RenderObject* child, RenderObject* beforeChil
     fixAnonymousStyles();
 }
 
-void RenderMathMLScripts::removeChild(RenderObject* child)
+void RenderMathMLScripts::removeChild(RenderObject& child)
 {
     if (beingDestroyed() || documentBeingDestroyed()) {
         // The renderer is being destroyed so we remove the child normally.
@@ -244,7 +246,6 @@ void RenderMathMLScripts::removeChild(RenderObject* child)
     }
 
     removeChildInternal(false, child);
-    
     fixAnonymousStyles();
 }
 
@@ -259,9 +260,9 @@ void RenderMathMLScripts::styleDidChange(StyleDifference diff, const RenderStyle
 RenderMathMLOperator* RenderMathMLScripts::unembellishedOperator()
 {
     RenderBoxModelObject* base = this->base();
-    if (!base || !base->isRenderMathMLBlock())
-        return 0;
-    return toRenderMathMLBlock(base)->unembellishedOperator();
+    if (!is<RenderMathMLBlock>(base))
+        return nullptr;
+    return downcast<RenderMathMLBlock>(*base).unembellishedOperator();
 }
 
 void RenderMathMLScripts::layout()
@@ -279,13 +280,11 @@ void RenderMathMLScripts::layout()
     // below the base's top edge, or the subscript's bottom edge above the base's bottom edge.
 
     LayoutUnit baseHeight = base->logicalHeight();
-    LayoutUnit baseBaseline = base->firstLineBoxBaseline();
-    if (baseBaseline == -1)
-        baseBaseline = baseHeight;
-    LayoutUnit axis = style()->fontMetrics().xHeight() / 2;
-    int fontSize = style()->fontSize();
+    LayoutUnit baseBaseline = base->firstLineBaseline().valueOr(baseHeight);
+    LayoutUnit axis = style().fontMetrics().xHeight() / 2;
+    int fontSize = style().fontSize();
 
-    ASSERT(m_baseWrapper->style()->hasOneRef());
+    ASSERT(m_baseWrapper->style().hasOneRef());
     bool needsSecondLayout = false;
 
     LayoutUnit topPadding = 0;
@@ -295,16 +294,16 @@ void RenderMathMLScripts::layout()
     LayoutUnit superscriptShiftValue = 0;
     LayoutUnit subscriptShiftValue = 0;
     if (m_kind == Sub || m_kind == SubSup || m_kind == Multiscripts)
-        parseMathMLLength(scriptElement->fastGetAttribute(MathMLNames::subscriptshiftAttr), subscriptShiftValue, style(), false);
+        parseMathMLLength(scriptElement->fastGetAttribute(MathMLNames::subscriptshiftAttr), subscriptShiftValue, &style(), false);
     if (m_kind == Super || m_kind == SubSup || m_kind == Multiscripts)
-        parseMathMLLength(scriptElement->fastGetAttribute(MathMLNames::superscriptshiftAttr), superscriptShiftValue, style(), false);
+        parseMathMLLength(scriptElement->fastGetAttribute(MathMLNames::superscriptshiftAttr), superscriptShiftValue, &style(), false);
 
     bool isPostScript = true;
-    RenderMathMLBlock* subSupPair = toRenderMathMLBlock(m_baseWrapper->nextSibling());
-    for (; subSupPair; subSupPair = toRenderMathMLBlock(subSupPair->nextSibling())) {
+    RenderMathMLBlock* subSupPair = downcast<RenderMathMLBlock>(m_baseWrapper->nextSibling());
+    for (; subSupPair; subSupPair = downcast<RenderMathMLBlock>(subSupPair->nextSibling())) {
 
         // We skip the base and <mprescripts/> elements.
-        if (isPrescript(subSupPair)) {
+        if (isPrescript(*subSupPair)) {
             if (!isPostScript)
                 break;
             isPostScript = false;
@@ -313,64 +312,57 @@ void RenderMathMLScripts::layout()
 
         if (RenderBox* superscript = m_kind == Sub ? 0 : subSupPair->lastChildBox()) {
             LayoutUnit superscriptHeight = superscript->logicalHeight();
-            LayoutUnit superscriptBaseline = superscript->firstLineBoxBaseline();
-            if (superscriptBaseline == -1)
-                superscriptBaseline = superscriptHeight;
-            LayoutUnit minBaseline = max<LayoutUnit>(fontSize / 3 + 1 + superscriptBaseline, superscriptHeight + axis + superscriptShiftValue);
+            LayoutUnit superscriptBaseline = superscript->firstLineBaseline().valueOr(superscriptHeight);
+            LayoutUnit minBaseline = std::max<LayoutUnit>(fontSize / 3 + 1 + superscriptBaseline, superscriptHeight + axis + superscriptShiftValue);
 
-            topPadding = max<LayoutUnit>(topPadding, minBaseline - baseBaseline);
+            topPadding = std::max<LayoutUnit>(topPadding, minBaseline - baseBaseline);
         }
 
         if (RenderBox* subscript = m_kind == Super ? 0 : subSupPair->firstChildBox()) {
             LayoutUnit subscriptHeight = subscript->logicalHeight();
-            LayoutUnit subscriptBaseline = subscript->firstLineBoxBaseline();
-            if (subscriptBaseline == -1)
-                subscriptBaseline = subscriptHeight;
+            LayoutUnit subscriptBaseline = subscript->firstLineBaseline().valueOr(subscriptHeight);
             LayoutUnit baseExtendUnderBaseline = baseHeight - baseBaseline;
             LayoutUnit subscriptUnderItsBaseline = subscriptHeight - subscriptBaseline;
-            LayoutUnit minExtendUnderBaseline = max<LayoutUnit>(fontSize / 5 + 1 + subscriptUnderItsBaseline, subscriptHeight + subscriptShiftValue - axis);
+            LayoutUnit minExtendUnderBaseline = std::max<LayoutUnit>(fontSize / 5 + 1 + subscriptUnderItsBaseline, subscriptHeight + subscriptShiftValue - axis);
 
-            bottomPadding = max<LayoutUnit>(bottomPadding, minExtendUnderBaseline - baseExtendUnderBaseline);
+            bottomPadding = std::max<LayoutUnit>(bottomPadding, minExtendUnderBaseline - baseExtendUnderBaseline);
         }
     }
 
     Length newPadding(topPadding, Fixed);
-    if (newPadding != m_baseWrapper->style()->paddingTop()) {
-        m_baseWrapper->style()->setPaddingTop(newPadding);
+    if (newPadding != m_baseWrapper->style().paddingTop()) {
+        m_baseWrapper->style().setPaddingTop(newPadding);
         needsSecondLayout = true;
     }
 
     newPadding = Length(bottomPadding, Fixed);
-    if (newPadding != m_baseWrapper->style()->paddingBottom()) {
-        m_baseWrapper->style()->setPaddingBottom(newPadding);
+    if (newPadding != m_baseWrapper->style().paddingBottom()) {
+        m_baseWrapper->style().setPaddingBottom(newPadding);
         needsSecondLayout = true;
     }
 
     if (!needsSecondLayout)
         return;
 
-    setNeedsLayout(true, MarkOnlyThis);
-    m_baseWrapper->setChildNeedsLayout(true, MarkOnlyThis);
+    setNeedsLayout(MarkOnlyThis);
+    m_baseWrapper->setChildNeedsLayout(MarkOnlyThis);
 
     RenderMathMLBlock::layout();
 }
 
-int RenderMathMLScripts::firstLineBoxBaseline() const
+Optional<int> RenderMathMLScripts::firstLineBaseline() const
 {
     if (m_baseWrapper) {
-        LayoutUnit baseline = m_baseWrapper->firstLineBoxBaseline();
-        if (baseline != -1)
+        if (Optional<int> baseline = m_baseWrapper->firstLineBaseline())
             return baseline;
     }
-    return RenderMathMLBlock::firstLineBoxBaseline();
+    return RenderMathMLBlock::firstLineBaseline();
 }
 
 RenderMathMLScriptsWrapper* RenderMathMLScriptsWrapper::createAnonymousWrapper(RenderMathMLScripts* renderObject, WrapperType type)
 {
-    RefPtr<RenderStyle> newStyle = RenderStyle::createAnonymousStyleWithDisplay(renderObject->style(), FLEX);
-    RenderMathMLScriptsWrapper* newBlock = new (renderObject->renderArena()) RenderMathMLScriptsWrapper(0, type);
-    newBlock->setDocumentForAnonymous(renderObject->document());
-    newBlock->setStyle(newStyle.release());
+    RenderMathMLScriptsWrapper* newBlock = new RenderMathMLScriptsWrapper(renderObject->document(), RenderStyle::createAnonymousStyleWithDisplay(&renderObject->style(), FLEX), type);
+    newBlock->initializeStyle();
     return newBlock;
 }
 
@@ -381,7 +373,7 @@ void RenderMathMLScriptsWrapper::addChildInternal(bool doNotRestructure, RenderO
         return;
     }
 
-    RenderMathMLScripts* parentNode = toRenderMathMLScripts(parent());
+    RenderMathMLScripts* parentNode = downcast<RenderMathMLScripts>(parent());
 
     if (m_kind == Base) {
         RenderObject* sibling = nextSibling();
@@ -395,8 +387,8 @@ void RenderMathMLScriptsWrapper::addChildInternal(bool doNotRestructure, RenderO
         // The old base (if any) becomes a script ; the new child becomes either the base or an <mprescripts> separator.
         RenderObject* oldBase = firstChild();
         if (oldBase)
-            RenderMathMLBlock::removeChild(oldBase);
-        if (isPrescript(child))
+            RenderMathMLBlock::removeChild(*oldBase);
+        if (isPrescript(*child))
             parentNode->addChildInternal(true, child, sibling);
         else
             RenderMathMLBlock::addChild(child);
@@ -405,7 +397,7 @@ void RenderMathMLScriptsWrapper::addChildInternal(bool doNotRestructure, RenderO
         return;
     }
 
-    if (isPrescript(child)) {
+    if (isPrescript(*child)) {
         // We insert an <mprescripts> element.
         if (!beforeChild)
             parentNode->addChildInternal(true, child, nextSibling());
@@ -414,15 +406,15 @@ void RenderMathMLScriptsWrapper::addChildInternal(bool doNotRestructure, RenderO
         else {
             // We insert the <mprescripts> in the middle of a subSup pair so we must split that pair.
             RenderObject* sibling = nextSibling();
-            parentNode->removeChildInternal(true, this);
+            parentNode->removeChildInternal(true, *this);
             parentNode->addChildInternal(true, child, sibling);
 
             RenderObject* script = firstChild();
-            RenderMathMLBlock::removeChild(script);
+            RenderMathMLBlock::removeChild(*script);
             parentNode->addChildInternal(false, script, child);
 
             script = beforeChild;
-            RenderMathMLBlock::removeChild(script);
+            RenderMathMLBlock::removeChild(*script);
             parentNode->addChildInternal(false, script, sibling);
             destroy();
         }
@@ -431,8 +423,8 @@ void RenderMathMLScriptsWrapper::addChildInternal(bool doNotRestructure, RenderO
 
     // We first move to the last subSup pair in the curent sequence of scripts.
     RenderMathMLScriptsWrapper* subSupPair = this;
-    while (subSupPair->nextSibling() && !isPrescript(subSupPair->nextSibling()))
-        subSupPair = toRenderMathMLScriptsWrapper(subSupPair->nextSibling());
+    while (subSupPair->nextSibling() && !isPrescript(*subSupPair->nextSibling()))
+        subSupPair = downcast<RenderMathMLScriptsWrapper>(subSupPair->nextSibling());
     if (subSupPair->firstChild()->nextSibling()) {
         // The last pair has two children so we need to create a new pair to leave room for the new child.
         RenderMathMLScriptsWrapper* newPair = createAnonymousWrapper(parentNode, RenderMathMLScriptsWrapper::SubSupPair);
@@ -442,44 +434,44 @@ void RenderMathMLScriptsWrapper::addChildInternal(bool doNotRestructure, RenderO
 
     // We shift the successors in the current sequence of scripts.
     for (RenderObject* previousSibling = subSupPair->previousSibling(); subSupPair != this; previousSibling = previousSibling->previousSibling()) {
-        RenderMathMLScriptsWrapper* previousSubSupPair = toRenderMathMLScriptsWrapper(previousSibling);
-        RenderObject* script = previousSubSupPair->lastChild();
-        previousSubSupPair->removeChildInternal(true, script);
+        RenderMathMLScriptsWrapper& previousSubSupPair = downcast<RenderMathMLScriptsWrapper>(*previousSibling);
+        RenderObject* script = previousSubSupPair.lastChild();
+        previousSubSupPair.removeChildInternal(true, *script);
         subSupPair->addChildInternal(true, script, subSupPair->firstChild());
-        subSupPair = toRenderMathMLScriptsWrapper(previousSibling);
+        subSupPair = downcast<RenderMathMLScriptsWrapper>(previousSibling);
     }
 
     // This subSup pair now contain one element which is either beforeChild or the script that was before. Hence we can insert the new child before of after that element.
-    RenderMathMLBlock::addChild(child, firstChild() == beforeChild ? beforeChild : 0);
+    RenderMathMLBlock::addChild(child, firstChild() == beforeChild ? beforeChild : nullptr);
 }
 
 void RenderMathMLScriptsWrapper::addChild(RenderObject* child, RenderObject* beforeChild)
 {
-    RenderMathMLScripts* parentNode = toRenderMathMLScripts(parent());
+    RenderMathMLScripts* parentNode = downcast<RenderMathMLScripts>(parent());
 
     addChildInternal(false, child, beforeChild);
 
     parentNode->fixAnonymousStyles();
 }
 
-void RenderMathMLScriptsWrapper::removeChildInternal(bool doNotRestructure, RenderObject* child)
+void RenderMathMLScriptsWrapper::removeChildInternal(bool doNotRestructure, RenderObject& child)
 {
     if (doNotRestructure) {
         RenderMathMLBlock::removeChild(child);
         return;
     }
 
-    RenderMathMLScripts* parentNode = toRenderMathMLScripts(parent());
+    RenderMathMLScripts* parentNode = downcast<RenderMathMLScripts>(parent());
 
     if (m_kind == Base) {
         // We remove the child from the base wrapper.
         RenderObject* sibling = nextSibling();
         RenderMathMLBlock::removeChild(child);
-        if (sibling && !isPrescript(sibling)) {
+        if (sibling && !isPrescript(*sibling)) {
             // If there are postscripts, the first one becomes the base.
-            RenderMathMLScriptsWrapper* wrapper = toRenderMathMLScriptsWrapper(sibling);
-            RenderObject* script = wrapper->firstChild();
-            wrapper->removeChildInternal(false, script);
+            RenderMathMLScriptsWrapper& wrapper = downcast<RenderMathMLScriptsWrapper>(*sibling);
+            RenderObject* script = wrapper.firstChild();
+            wrapper.removeChildInternal(false, *script);
             RenderMathMLBlock::addChild(script);
         }
         return;
@@ -488,22 +480,22 @@ void RenderMathMLScriptsWrapper::removeChildInternal(bool doNotRestructure, Rend
     // We remove the child and shift the successors in the current sequence of scripts.
     RenderMathMLBlock::removeChild(child);
     RenderMathMLScriptsWrapper* subSupPair = this;
-    for (RenderObject* nextSibling = subSupPair->nextSibling(); nextSibling && !isPrescript(nextSibling); nextSibling = nextSibling->nextSibling()) {
-        RenderMathMLScriptsWrapper* nextSubSupPair = toRenderMathMLScriptsWrapper(nextSibling);
-        RenderObject* script = nextSubSupPair->firstChild();
-        nextSubSupPair->removeChildInternal(true, script);
+    for (RenderObject* nextSibling = subSupPair->nextSibling(); nextSibling && !isPrescript(*nextSibling); nextSibling = nextSibling->nextSibling()) {
+        RenderMathMLScriptsWrapper& nextSubSupPair = downcast<RenderMathMLScriptsWrapper>(*nextSibling);
+        RenderObject* script = nextSubSupPair.firstChild();
+        nextSubSupPair.removeChildInternal(true, *script);
         subSupPair->addChildInternal(true, script);
-        subSupPair = toRenderMathMLScriptsWrapper(nextSibling);
+        subSupPair = downcast<RenderMathMLScriptsWrapper>(nextSibling);
     }
 
     // We remove the last subSup pair if it became empty.
     if (subSupPair->isEmpty()) {
-        parentNode->removeChildInternal(true, subSupPair);
+        parentNode->removeChildInternal(true, *subSupPair);
         subSupPair->destroy();
     }
 }
 
-void RenderMathMLScriptsWrapper::removeChild(RenderObject* child)
+void RenderMathMLScriptsWrapper::removeChild(RenderObject& child)
 {
     if (beingDestroyed() || documentBeingDestroyed()) {
         // The renderer is being destroyed so we remove the child normally.
@@ -511,10 +503,8 @@ void RenderMathMLScriptsWrapper::removeChild(RenderObject* child)
         return;
     }
 
-    RenderMathMLScripts* parentNode = toRenderMathMLScripts(parent());
-
+    RenderMathMLScripts* parentNode = downcast<RenderMathMLScripts>(parent());
     removeChildInternal(false, child);
-
     parentNode->fixAnonymousStyles();
 }