}
 
   public int getNextToken() throws InvalidInputException {
-
+    int htmlPosition = currentPosition;
     try {
-      int htmlPosition = currentPosition;
       while (!phpMode) {
         currentCharacter = source[currentPosition++];
         if (currentCharacter == '<') {
       }
     } //-----------------end switch while try--------------------
     catch (IndexOutOfBoundsException e) {
+      if (tokenizeWhiteSpace) {
+        // && (whiteStart != currentPosition - 1)) {
+        // reposition scanner in case we are interested by spaces as tokens
+        startPosition = htmlPosition;
+      }
       return TokenNameEOF;
     }