0 Replies Latest reply on Dec 23, 2014 3:47 AM by Stamm

    Object is invalid on object.parentStory.characters.item(i+1).fontStyle;

    Stamm Level 1

      Hey guys,

       

      I have an array where I save the contents of a textFrame, where "object" is the textFrame.

       

      textContent = object.contents;

       

      Then I have a for loop to loop  through all items. With

       

      if(i > 0)

      object.parentStory.characters.item(i-1).fontStyle.toString().toLowerCase();

       

      object.parentStory.characters.item(i).fontStyle.toString().toLowerCase();

       

      if(i < textContent.length-1)

      object.parentStory.characters.item(i+1).fontStyle.toString().toLowerCase();

       

      I am getting the previous, current and next styles. It's for xml purposes and with this I can add a "<b>" , "<i>", etc. tag for my objects. The problem occuring now is that on some point, object.parentStory.characters.item(i+1).fontStyle; returns "object is invalid".

       

      That being said, item(i+1) is valid. If I alert this, it says "object [character]". I tried to use if(object.parentStory.characters.item(i+1).hasOwnProperty('fontStyle')), but it didn't work. I don't know why it throws this error and I couldn't find any solution to this problem on the web.

       

      Has anybody experienced this?  Here is the complete code(without italic and bold italic):

       

      function spliceSlice(str, index, count, add) {

        return str.slice(0, index) + (add || "") + str.slice(index + count);

      }

       

      function checkFontStyle(object){

          textContent = object.contents;

          for(i = 0; i < textContent.length; i++){

              style = object.parentStory.characters.item(i).fontStyle.toString().toLowerCase();

              if(i > 0)

                  prevStyle = object.parentStory.characters.item(i-1).fontStyle.toString().toLowerCase();

              if(i < textContent.length - 1){

                  nextStyle = object.parentStory.characters.item(i+1).fontStyle.toString().toLowerCase();

              }

              if(style.indexOf('bold') > -1){

                  if(prevStyle.indexOf('bold') > -1){

                    

                  }

                  else if(nextStyle.indexOf('bold') < -1){

                      textContent.spliceSlice(textContent, i, 0, '[/b]');

                  }

                  else{

                      textContent = spliceSlice(textContent, i, 0, '[b]');

                  }

              }

          }

          return textContent;

      }

       

      The desired behaviour would be, that if the current object has no "fontStyle" it should just skip this iteration.