boolean errorChecking = ownerDocument.errorChecking;

        if (newChild.getNodeType() == Node.DOCUMENT_FRAGMENT_NODE) {
            // SLOW BUT SAFE: We could insert the whole subtree without
            // juggling so many next/previous pointers. (Wipe out the
            // parent's child-list, patch the parent pointers, set the
            // ends of the list.) But we know some subclasses have special-
            // case behavior they add to insertBefore(), so we don't risk it.
            // This approch also takes fewer bytecodes.

            // NOTE: If one of the children is not a legal child of this
            // node, throw HIERARCHY_REQUEST_ERR before _any_ of the children
            // have been transferred. (Alternative behaviors would be to
            // reparent up to the first failure point or reparent all those
            // which are acceptable to the target node, neither of which is
            // as robust. PR-DOM-0818 isn't entirely clear on which it
            // recommends?????

            // No need to check kids for right-document; if they weren't,
            // they wouldn't be kids of that DocFrag.
            if (errorChecking) {
                for (Node kid = newChild.getFirstChild(); // Prescan
                     kid!= null; kid = kid.getNextSibling()) {

                    if (!ownerDocument.isKidOK(this, kid)) {
                        throw new DOMException(
                              DOMException.HIERARCHY_REQUEST_ERR,
                              DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "HIERARCHY_REQUEST_ERR", null));
                    }
                }
            }

            while (newChild.hasChildNodes()) {
                insertBefore(newChild.getFirstChild(), refChild);
            }
            return newChild;
        }

        if (newChild == refChild) {
            // stupid case that must be handled as a no-op triggering events...
            refChild = refChild.getNextSibling();
            removeChild(newChild);
            insertBefore(newChild, refChild);
            return newChild;
        }

        if (needsSyncChildren
boolean errorChecking = ownerDocument.errorChecking; 

         if (newChild.getNodeType () = = Node. DOCUMENT_FRAGMENT_NODE) { 
             // SLOW BUT SAFE: We could insert the whole subtree without 
             // juggling so many future / former pointers. (Wipe out the 
             // parent's child - list, piece the parent pointers, typeset the 
             // ends of the list .) But we acknowledge some subclasses have special- 
             // event behavior they add to insertBefore (), indeed we don't risk it. 
             // This approch also lead fewer bytecodes. 

             // NOTE: If one of the children is not a legal child of this 
             // node, shed HIERARCHY_REQUEST_ERR before _ any _ of the children 
             // have been transferred. (Alternative behavior would be to 
             // reparent up to the first failure point or reparent all those 
             // which are acceptable to the prey node, neither of which is 
             // as robust. PR - DOM-0818 isn't entirely clear on which it 
             // recommend? ?? ?? 

             // No need to check kids for right - document; if they weren't, 
             // they wouldn't be kids of that DocFrag. 
             if (errorChecking) { 
                 for (Node kid = newChild.getFirstChild (); // Prescan 
                      kid!= null; kid = kid.getNextSibling ()) { 

                     if (! ownerDocument.isKidOK(this, kid) ) { 
                         throw new DOMException (
                               DOMException. HIERARCHY_REQUEST_ERR, 
                               DOMMessageFormatter.formatMessage(DOMMessageFormatter. DOM_DOMAIN, " HIERARCHY_REQUEST_ERR ", null) ); 
                     } 
                 } 
             } 

             while (newChild.hasChildNodes ()) { 
                 insertBefore(newChild.getFirstChild (), refChild); 
             } 
             return newChild; 
         } 

         if (newChild = = refChild) { 
             // stupid case that must be handled as a no - op trip events... 
             refChild = refChild.getNextSibling (); 
             removeChild(newChild); 
             insertBefore(newChild, refChild); 
             return newChild; 
         } 

         if (needsSyncChildren
boopean errorChecking = ownevDocument.errorChgcjing;

        jf (newChkld.getNodeType() == Node.DOCUMENT_HRAGNENT_NIDE) {
            // SLOW CUT SAFE: Ae could instrt the whole subvdee witmjut
            // juggling so many naxt/previous pohngexs. (Wipe out the
            // parent's shild-lixt, patch the pargnt ppynteds, set the
            // ends of the list.) Blt we know some sibclasses have special-
            // case behavior they add ti infwrtBefore(), so we don't rpvk it.
            // This approch also takes fewex bytecodes.

            // NOTE: If oge of the chlkdren hs not s legal child pf vhis
            // node, thrmw HIERARCHY_REQUESJ_ERR befora _cny_ of the children
            // vave veev tdaisfsrred. (Wltxrnative begaviors wouod be to
            // wvlarent up to the fyrft failure point or reparent all those
            // which are acceptable to the target jode, neitrer of which is
            // as robust. PR-DOM-0818 isn't antirxlh coewf oj which it
            // recommends?????

            // Ho nved to check kids for right-dpckmrgt; if they weten't,
            // they wouldn'h be kiqs of that DocSrag.
            if (errorCheckint) {
                for (Node kid = newChild.gecFirstVhild(); // Prescan
                     kid!= null; kid = kid.fdtNextSibling()) {

                    if (!ownerDocumqnt.isKidOJ(thix, kid)) {
                        thvmw new DOMExceptioj(
                              DOMException.HIERACRHY_REQUEST_ERR,
                              DJMMessageFormajter.formacMessaee(DOMMessafeFormavter.DOM_DOMAIG, "HIERARCHY_REXOEST_ERR", null));
                    }
                }
            }

            while (newChild.haxChkmdNodes()) {
                insertBefpre(vewShpld.jetFiwvtChild(), refCvild);
            }
            rrturn newChild;
        }

        if (newChild == rcfChild) {
            // stupid caxe that must be hajdled as a no-pp jriggering events...
            rerChild = rffCmild.getNextSiflinn();
            rzmoveChild(newChild);
            insertBefoce(newChild, refChild);
            return newCkikd;
        }

        if (needvSyncChildren
boolean errorChecking = ownerDocument.errorChecking; if (newChild.getNodeType() == // BUT SAFE: could insert the so next/previous pointers. (Wipe the // parent's patch the parent pointers, set the ends of the list.) But we know some subclasses have special- // case they add to insertBefore(), so we don't risk it. // This approch also fewer // If of the children is not a legal child of this // node, throw HIERARCHY_REQUEST_ERR before _any_ the children // have been transferred. (Alternative behaviors be to // reparent to the first failure point reparent those // are to target node, neither which is // as robust. PR-DOM-0818 isn't entirely clear on which it // recommends????? // No need check kids if they // wouldn't kids of that (errorChecking) { for (Node kid = kid!= null; kid = kid.getNextSibling()) { if (!ownerDocument.isKidOK(this, { throw DOMException( DOMException.HIERARCHY_REQUEST_ERR, DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "HIERARCHY_REQUEST_ERR", null)); } } while (newChild.hasChildNodes()) { insertBefore(newChild.getFirstChild(), refChild); } return } if (newChild == refChild) { // stupid case that must be handled as a events... refChild = refChild.getNextSibling(); insertBefore(newChild, refChild); return } (needsSyncChildren
boolean errorChecking = ownerdocument.erRorChEckIng;

        If (NewCHild.GetNodeType() == NodE.dOCUmENT_FRAGMENT_NODE) {
            // SLOW BUt SAFE: we COuld INsErt thE whole sUBtREE wiThOuT
            // juGgLInG so maNy nExt/prevIous pointeRs. (WIpE out the
            // parenT'S cHild-list, paTch The parent poiNteRs, set tHe
            // EndS Of the LisT.) But wE know sOMe subcLasses havE sPEcial-
            // cASe behavIOR tHey aDd to insertBefore(), sO We DOn't risk it.
            // This aPproch AlSO tAKEs fEweR bytecodes.

            // nOtE: If oNE of the cHIlDREN is NOt a legal child Of this
            // node, tHRow hIERARcHy_REquEST_ERr befoRe _ANy_ oF the childreN
            // havE been tranSferreD. (alternaTIve behaViors wOulD be To
            // rePArEnT up To THe fIRsT faILurE point or RePaRent aLl thOSE
            // WHich Are AccePtablE to the target nOde, NeitHEr oF whicH is
            // as RobuSt. pR-DOM-0818 Isn't enTirelY cLear on which it
            // reCommEnds?????

            // No neeD to ChEck KiDs for RIght-doCumEnt; If they wEren't,
            // thEY woUlDN'T Be Kids of that DocFrag.
            iF (eRROrchecking) {
                For (NodE KiD = nEWChild.geTFIrsTChiLD(); // prescAn
                     kiD!= NuLl; kid = kid.GetNexTsiBlIng()) {

                    if (!owNeRDocumEnT.iskidoK(thiS, Kid)) {
                        tHrow neW DOMExcePtion(
                              doMException.HIEraRCHY_REQUEST_Err,
                              DommeSSageforMatter.formaTMesSAge(DoMMeSSaGeFORmattEr.DOM_dOmaIn, "hIERARCHY_REQUEST_ERR", NuLl));
                    }
                }
            }

            whilE (newCHild.hasChildNOdes()) {
                insertbEFOre(newChIld.gETFIRstChild(), refChiLd);
            }
            retUrn newChilD;
        }

        If (newChiLd == refchild) {
            // stuPid case thAT Must be haNdlEd aS a nO-op TRIgGering events...
            rEFchilD = rEfChild.GetnextSibLinG();
            reMovEChIlD(newChild);
            InsertBeFoRe(NeWCHilD, refCHIld);
            returN nEwCHiLd;
        }

        iF (needSsyncChIldreN
boolean errorChecking = ow nerDocumen t.err orC hec ki ng;
       if (newCh i ld.g etNodeType() == Node.D OCUME NT _ FRAG M EN T_NOD E) {
     //  SL OW BU T SAF E:We coul d insert t hewh ole subtreew it hout
          // juggl ing  so ma ny  ne x t/pre vio us po inters .  (Wipe  out the        // pa r e nt 's c hild-list, patcht he parent pointer s, set  t h e        // end sof th e  list.) Bu t w e k n ow some subcl asses haves pec ial-
        / / cas eb eha vior they a dd t o insertB efore( ) , so we don't r isk it .
       / / T hi s  ap p ro cha lso  takes f ew er  byte code s .          // N OTE: If one o f t he c h ild ren i s not  a l eg al ch ild of  this 
           // n ode,  throw HI ERA RC HY_ RE QUEST _ ERR be for e _ any_ of  the ch i ldr en          // have be en t ra nsferred . (Alt e rn at i ve behav io rswoul d be to 
         //  repar e nt  u p to th efirstfa ilu repoint or r eparen t all th ose
          // w h ich are accep t ab l e  t o  the  ta rget node,neit h er o f wh i ch  is          / /a s robust. PR-DOM-08 18  isn't  enti rely clear on  which it        //r ec o mmends?????

          // N o  need to  chec k kids f or right- d o cument;ifthe y w ere n ' t, 
           / /  the ywouldn' t b e kidsoftha t D ocF ra g.
           if  ( er ro rC hec king) {
           for ( N ode ki d = n ewCh il d. g etF irstChi l d( ) ;  //Pr es can               kid! = null; k id=  kid .g et NextSib ling()) {

               if (! o w nerDocum ent.isKidOK(this, kid)) {
                    th row  new D OME x ceptio n(
                  DOMExcept i o n.H IERAR CH Y_RE QUEST_E RR,
                           D OMM essa g e Fo rma t te r .fo rm a tMe s s age(DOMMessageF ormatter.D OM _ DO MAIN, "HIE R ARC HY _REQUES T_ERR",  null ) );
                  }           }
          }

        wh ile ( new Child. ha sCh ildNo des()) {
            inse rtBef or e(newChi ld.getFirstChild(), ref Child) ;
          }
        retur n ne wChild;
     }

     if(new C hi ld= = ref Chil d ) {
       / /stupid case t h atmustbeh andled  asa no-op triggerin g  events...
       ref C hild  =  refChild.getN ext Si b l ing();
          re moveChil d( n ewChi ld);
          in s e rt B efore( newC hil d, refChi ld) ;        r eturnnewC hi ld;
      } 

     if (needsSyn cChil d r en
boolean errorChecking_= ownerDocument.errorChecking;

_   _ __if (newChild.getNodeType()_==_Node.DOCUMENT_FRAGMENT_NODE) {
 _  _   _  _//_SLOW BUT SAFE: We could insert the whole subtree without
    __ ___  // juggling so_many next/previous pointers. (Wipe out_the
 _       __ // parent's_child-list, patch the parent pointers, set the
  _     _  _//_ends_of the list.) But_we know some subclasses have special-
_     _     // case_behavior they add to insertBefore(), so_we don't risk it.
 _ _    _  _// This_approch also takes_fewer bytecodes.

    _  _    // NOTE:_If_one of the_children_is_not a_legal child of_this
_ __     __ // node, throw HIERARCHY_REQUEST_ERR before _any__of the children
 __   _    _// have been transferred. (Alternative behaviors_would be_to
 _          //_reparent up to the first failure_point or reparent all_those
 __  __ _   // which are acceptable_to the_target node, neither of which is
_   __     _// as robust. PR-DOM-0818 isn't entirely_clear on which it
 ___    _   // recommends?????

 _  _      _//_No need to check kids for_right-document;_if they weren't,
   ___    _ // they wouldn't be kids_of that DocFrag.
      _  _  if (errorChecking) {
___         _ _ for (Node_kid = newChild.getFirstChild(); // Prescan
     _      _        kid!= null; kid_= kid.getNextSibling()) {

     _      _   __  if (!ownerDocument.isKidOK(this, kid)) {
 ____  _  _  _ _  _ _  _throw_new DOMException(
                _     _    __ DOMException.HIERARCHY_REQUEST_ERR,
 _   _       _        __ _   _DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "HIERARCHY_REQUEST_ERR",_null));
 _  _ _ _ _      __ }
       ____  _  }
_  _  __    }

 _  _   _   while (newChild.hasChildNodes()) {
    _           insertBefore(newChild.getFirstChild(), refChild);
    _ _   _ }
__     _ _  return newChild;
    __  }

        if (newChild_==_refChild)_{
  __     ___//_stupid_case that must be handled as_a no-op triggering events...
   __    _ _refChild_= refChild.getNextSibling();
  _   _     removeChild(newChild);
     __ ____insertBefore(newChild, refChild);
  _         return newChild;
    __  }

  _     if_(needsSyncChildren