title: Keeping line breaks when using PHP's DomDocument appendChild
body: <p>I'm trying to use the DOMDocument in PHP to add/parse things in an HTML document. From what I could read, setting the formOutput to true and preserveWhiteSpace to false should keep the tabs and newlines in order, but it doesn't seem like it is for newly created or appended nodes.</p>
  
  <p>Here's the code:</p>
  
  <pre><code>$dom = new \DOMDocument;
  $dom-&gt;formatOutput = true;
  $dom-&gt;preserveWhiteSpace = false;
  $dom-&gt;loadHTMLFile($htmlsource);
  $tables = $dom-&gt;getElementsByTagName('table');
  foreach($tables as $table)
  {
      $table-&gt;setAttribute('class', 'tborder');
      $div = $dom-&gt;createElement('div');
      $div-&gt;setAttribute('class', 'm2x');
      $table-&gt;parentNode-&gt;insertBefore($div, $table);
      $div-&gt;appendChild($table);
  }
  $dom-&gt;saveHTMLFile($html)
  </code></pre>
  
  <p>Here's what the HTML looks like:</p>
  
  <pre><code>&lt;table&gt;
      &lt;tr&gt;
          &lt;td&gt;&lt;/td&gt;
      &lt;/tr&gt;
  &lt;/table&gt;
  </code></pre>
  
  <p>Here's what I want:</p>
  
  <pre><code>&lt;div class="m2x"&gt;
      &lt;table class="tborder"&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;/td&gt;
          &lt;/tr&gt;
      &lt;/table&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>Here's what I get:</p>
  
  <pre><code>&lt;div class="m2x"&gt;&lt;table class="tborder"&gt;&lt;tr&gt;
  &lt;td&gt;&lt;/td&gt;
          &lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;
  </code></pre>
  
  <p>Is there something I'm doing wrong? I've tried googling this as many different ways as I could thing of with no luck. </p>