title: "Function declarations cannot be nested inside a statement" - IE in strict mode error
body: <p>In a <code>"use strict"</code> application, I'm using <code>document.createTreeWalker</code> to traverse a DOM tree. After I get the tree from the browser I use a while loop to push the values into an array. The code is an upgrade to the excellent <a href="http://letteringjs.com/" rel="nofollow">Letteringjs.com</a> plugin, and my version can be seen in action <a href="http://sbnwebsolutions.com/" rel="nofollow">here</a>.</p>
  
  <pre><code>function injector(t, splitter, klass, after) {
      var inject = '', n,
          tree = [],
          styleStack = [],
          splitIndex = 0,
  
          styleAndSplit = function (node, splitter, klass, after) {
              var particles = $(node).text().split(splitter);
  
              // Wrap each particle with parent style tags if present
              for (var j = 0, len = particles.length; j &lt; len; j++) {
                  var str = '&lt;span class="'+klass+((splitIndex++)+1)+'"&gt;'+particles[j]+'&lt;/span&gt;'+after;
  
                  for (var k = styleStack.length - 1, style; style = styleStack[k--];) {
                      str =   '&lt;' + style.tagName.toLowerCase() + (style.className ? ' class="' + style.className + '"' : '') + '&gt;' +
                              str + 
                              '&lt;/' + style.tagName.toLowerCase() + '&gt;'
                  }
                  inject += str;
              }
          },
  
          Walker /* Texas Ranger */ = document.createTreeWalker(
              t,
              NodeFilter.SHOW_ALL,
              { acceptNode: function (node) {
                  // Keep only text nodes and style altering tags
                  if (node.nodeType === 3 || node.nodeType === 1 &amp;&amp; !!node.tagName &amp;&amp;
                     (node.tagName.toLowerCase() === 'i' || 
                          node.tagName.toLowerCase() === 'b' || 
                              node.tagName.toLowerCase() === 'u' ||
                                  node.tagName.toLowerCase() === 'span')) {
  
                      return NodeFilter.FILTER_ACCEPT;
                  } else {
                      return NodeFilter.FILTER_SKIP;
                  }
              }},
              false
          );
  
      while (n = Walker.nextNode()) tree.push(n);
  
      // This loop traverses all of the nodes in the order they appear within the HTML tree
      // It will then stack up nested styling tags accordingly
      for (var i = 0, node; node = tree[i++];) {
          if (node.nodeType === 1) {
              styleStack.push(node);
          } else {
              // Get rid of nodes containing only whitespace (newlines specifically)
              if ($.trim(node.nodeValue).length) {
                  while (styleStack.length &amp;&amp; node.parentNode !== styleStack[styleStack.length - 1]) { styleStack.pop(); }
                  styleAndSplit(node, splitter, klass, after);    
              }
          }
      }       
  
      $(t).empty().append(inject);        
  }
  </code></pre>
  
  <p>This code works in Firefox, Chrome and mobile browsers too. But IE9 &amp; IE10 don't play nice.</p>
  
  <p>Both break execution on the <code>while (n = Walker.nextNode()) tree.push(n);</code> line, prompting:</p>
  
  <blockquote>
    <p>SCRIPT1047: In strict mode, function declarations cannot be nested
    inside a statement or block. They may only appear at the top level or
    directly inside a function body.</p>
  </blockquote>
  
  <p>EDIT: this is the <a href="http://msdn.microsoft.com/en-us/library/ie/br230269%28v=vs.94%29.aspx" rel="nofollow">example from MSDN</a> as to what should throw this error:</p>
  
  <pre><code>var arr = [1, 2, 3, 4, 5];
  var index = null;
  for (index in arr) {
      function myFunc() {};
  }
  </code></pre>
  
  <p>EDIT: But that doesn't make sense, because I am not declaring a function, I am only executing one. Regardless though, I followed fred02138's suggestion to remove the <code>styleAndSplit</code> function declaration (I simply replaced the call with the function code, and erased the declaration) - but <strong>it didn't mend the error</strong>.</p>
  
  <p>Is there a different way to traverse a <code>TreeWalker</code>, or is there a workaround for IE (without losing strict mode)?</p>