0 Replies Latest reply on May 13, 2012 5:54 PM by jaloren28

    Why does a find and replace method remove whitespace?

    jaloren28 Level 2

      I have method that searches for a string in a FM document and replaces it with a variable. If for example, there was a string foobar that I wanted to replace with the variable barfoo. Then, I expect this text:

       

      Lorem ipsum dolor sit amet, foobar consectetur adipiscing elit. Vivamus sed purus urna, ac tristique tortor. Nam auctor tellus non enim pulvinar a vestibulum neque tincidunt.

       

      To be changed to this text:

       

      Lorem ipsum dolor sit amet, barfoo consectetur adipiscing elit. Vivamus sed purus urna, ac tristique tortor. Nam auctor tellus non enim pulvinar a vestibulum neque tincidunt.

       

      However, while the text does do the replace it also removes the whitespace between the variable and the text that appears right after the variable so it actually looks looks like this:

       

      Lorem ipsum dolor sit amet, barfooconsectetur adipiscing elit. Vivamus sed purus urna, ac tristique tortor. Nam auctor tellus non enim pulvinar a vestibulum neque tincidunt.

       

      Why is the find and replace method removing the whitespace and how do I prevent that from happening? The method is provided below.

       

      function FindAndReplaceString(pDoc, findString, replaceVariable)

      {

       

          if (typeof pDoc != 'undefined'&&typeof findString != 'undefined'&&typeof replaceVariable != 'undefined'&&pDoc.ObjectValid()&&findString.length>0&&replaceVariable.length>0)

          {

              var vVarFmtStatus=checkVarFmStatus (pDoc, replaceVariable);

              if (vVarFmtStatus=='In Doc')

              {

                  var tr = new TextRange();

                  var findParams = new PropVals();

                  var frame = pDoc.MainFlowInDoc.FirstTextFrameInFlow;

                  var restoreTR = pDoc.TextSelection;

       

                  tr.beg.obj = tr.end.obj = frame.FirstPgf;

                  tr.beg.offset = tr.end.offset = 0;

       

                  findParams = AllocatePropVals(1);

                  findParams[0].propIdent.num = Constants.FS_FindText;

                  findParams[0].propVal.valType = Constants.FT_String;

                  findParams[0].propVal.sval = findString;

       

                  tr = pDoc.Find(tr.beg, findParams);

       

                  var vLoopCounter=0;

                  while(FA_errno === Constants.FE_Success&&vLoopCounter++< 1000)

                  {

                      pDoc.TextSelection = tr;

                      pDoc.Clear(0);

                      var newVar = pDoc.NewAnchoredFormattedVar(replaceVariable, tr.beg);

                      var varLength = newVar.TextRange.end.offset - newVar.TextRange.beg.offset;

                      tr.beg.offset += varLength;

                      tr = pDoc.Find(tr.beg, findParams);

                      }

                  if (vLoopCounter>0)

                  {

                      Log (vLogFileName, 'In the document \''+pDoc.Name+'\', the string \''+findString+'\' was replaced with the variable \''+replaceVariable+'\' '+vLoopCounter+' times.\n')

                      }

       

                  if (vLoopCounter>1000)

                  {

                      recordErrors (vErrorLog, 'ERROR: In the document "'+pDoc.Name+'", the find and replace operation was stopped after executing '+vLoopCounter+' times. The term being searched for is "'+findString+'" the replacement variable is "'+ replaceVariable+'".')

                      }

       

                  pDoc.TextSelection = restoreTR;

                  pDoc.ScrollToText(restoreTR);

                  } else {

                      recordErrors (vErrorLog, 'ERROR: The find and replace operation failed because the variable '+replaceVariable+' does not exist in the following doc: '+pDoc.Name)

                  }

              } else {

                  recordErrors (vErrorLog,'Invalid or unitialized parameter passed to function FindAndReplaceString')

                  } 

      }