5 Scripted Math —...—

<..void barsIntoFenced(dom)..>
 static void barsIntoFenced(Node dom) {
    Node rightBarNode = null;
    Node mrowNode = dom.getFirstChild();
    Node mrowChild = mrowNode.getLastChild();
    while( mrowChild != null ){
       if(
            mrowChild.getNodeName().equals("msub")
            ||
            mrowChild.getNodeName().equals("msup")
            ||
            mrowChild.getNodeName().equals("msubsup")
       ){
          Node firstChild = mrowChild.getFirstChild();
          if(
              (firstChild.getChildNodes().getLength() == 1)
              &&
              firstChild.getTextContent().equals( "|" )
          ){
             rightBarNode = mrowChild;
       }  }
       else
       if(
          (rightBarNode != null)
          &&
          mrowChild.getNodeName().equals("mo")
          &&
          (mrowChild.getChildNodes().getLength() == 1)
          &&
          mrowChild.getTextContent().equals( "|" )
       ){
         if(mrowChild.getNextSibling() != rightBarNode){
             <.set mfenced sub,sup.>
         }
         rightBarNode = null;
       }
       mrowChild = mrowChild.getPreviousSibling();
 }  }
-_-_-
               A + |B|^2 + C  
<mrow>  
  <mi>A</mi>  
  <mo class="MathClass-bin">+</mo>  
  <mo class="MathClass-rel">|</mo>      #  
  <mi>B</mi>                            #  
  <msup>                                #  
    <mrow>                              #  
      <mo class="MathClass-rel">|</mo>  #  
    </mrow>                             #  
    <mrow>                              #  
      <mn>2</mn>                        #  
    </mrow>                             #  
  </msup>                               #  
  <mo class="MathClass-bin">+</mo>  
  <mi>C</mi>  
</mrow>

<..set mfenced sub,sup..>
 rightBarNode.removeChild( rightBarNode.getFirstChild() );
 Node sub = rightBarNode.getFirstChild();
 Node mfenced = ((Document) dom).createElement( "mfenced" );
 rightBarNode.insertBefore( mfenced, sub );
 ((Element) mfenced).setAttribute("open","|");
 ((Element) mfenced).setAttribute("close","|");
 ((Element) mfenced).setAttribute("separator","");
 Node node = mrowChild.getNextSibling();
 while( node != rightBarNode ){
    Node next = node.getNextSibling();
    mrowNode.removeChild( node );
    mfenced.appendChild( node );
    node = next;
 }
 mrowNode.removeChild( mrowChild );
 mrowChild = mrowNode.getLastChild();
-_-_-