title: SVG: using getComputedTextLength to wrap text
body: <p>I'm trying to wrap text by building up a text string, and using <code>getComputedTextLength</code> to find out when the text goes beyond the allowed width. However, I can't find a simple way to incrementally build up the text which will work with <code>getComputedTextLength</code>.</p>
  
  <p>The general idea is:</p>
  
  <pre><code>  str = svgDocument.createTextNode(myText[word]); // first word on new line
    word++;
    obj = text.cloneNode(true);                     // new text element for this line
    obj.appendChild(str);
    svgDocument.documentElement.appendChild(obj);   // reqd for getComputedTextLength?
    for( ; word &lt; myText.length; word++) {
       next_width = obj.getComputedTextLength();    // get current line width
       if(next_width &gt;= extent)
          break;
       str += " ";                                  // add next word to the line
       str += myText[word];
       ...
    }
  </code></pre>
  
  <p>Can anyone tell me how to get this to work? Presumably <code>str</code> is copied rather than referenced in <code>obj</code>, but I've also tried putting <code>obj.removeChild(str)</code> and <code>obj.appendChild(str)</code> in the loop, but the <code>appendChild</code> crashes. I've also tried various combinations of moving around the <code>documentElement.appendChild</code>, and removing <code>obj</code> and re-appending it, and so on.</p>