 
       if @value.length > maxTokenLength
         while startIndex < endIndex
-          fragments.push "<span>" + @escapeString(html, startIndex, startIndex + maxTokenLength) + "</span>"
+          fragments.push "<span>" + @escapeString(@value, startIndex, startIndex + maxTokenLength) + "</span>"
           startIndex += maxTokenLength
       else
-        fragments.push @escapeString(html, startIndex, endIndex)
+        fragments.push @escapeString(@value, startIndex, endIndex)
 
       fragments.push trailingHtml
-
-    fragments.join('')
+      html = fragments.join('')
+    html
 
   escapeString: (str, startIndex, endIndex) ->
     strLength = str.length


        startIndex = match[0].length

      if hasTrailingWhitespace and match = TrailingWhitespaceRegex.exec(html)
        classes = 'trailing-whitespace'
        classes += ' indent-guide' if hasIndentGuide and not hasLeadingWhitespace
        classes += ' invisible-character' if invisibles.space


  getValueAsHtml: ({invisibles, hasLeadingWhitespace, hasTrailingWhitespace, hasIndentGuide})->
    invisibles ?= {}
    if @isHardTab
      classes = 'hard-tab'
      classes += ' indent-guide' if hasIndentGuide
      classes += ' invisible-character' if invisibles.tab
      html = @value.replace StartCharacterRegex, (match) =>
        match = invisibles.tab ? match
        "<span class='#{classes}'>#{@escapeString(match)}</span>"
    else
      startIndex = 0
      endIndex = @value.length

      leadingHtml = ''
      trailingHtml = ''

      if hasLeadingWhitespace and match = LeadingWhitespaceRegex.exec(@value)
        classes = 'leading-whitespace'
        classes += ' indent-guide' if hasIndentGuide
        classes += ' invisible-character' if invisibles.space
