title: Javascript DOM, get node text without losing spacing info
body: <p>I am using javascript and want to traverse the HTML tree, getting all the text as it appears to the user. However, I am losing spacing information.</p>
  
  <p>Let's say I have two docs:</p>
  
  <pre><code>&lt;html&gt;XXX&lt;p&gt;YY    YY&lt;/p&gt;&lt;html&gt;
  
  &lt;html&gt;XXX&lt;p&gt;YY&amp;nbsp;&amp;nbsp;&amp;nbsp;YY&lt;/p&gt;&lt;html&gt;
  </code></pre>
  
  <p>The first one will appear with 1 space between the Ys. The second will have 3 spaces. However,  if I traverse the tree and, for each #text node, use:</p>
  
  <pre><code>text = node.nodeValue;
  </code></pre>
  
  <p>then the text for both nodes will have 3 spaces. I no longer know which one has the "real" nbsp spaces. I can use node.innerHTML for the p elements, which will show the nbsp, but I don't think that I can use innerHTML to get just the XXX text (without some kind of text subtraction). </p>
  
  <p>I could just get innerHTML of the whole document and parse that. However, I also need to get the computed style of each element, which I am going to get using  </p>
  
  <pre><code>window.getComputedStyle(theElement).getPropertyValue("text-align");
  </code></pre>
  
  <p>So, I will be traversing each node. Also, innerHTML shows the source as is, while traversing the nodes "fixes" html errors, adding end tags, etc. That's a good thing and something I'd like to keep.</p>