title: Getting HTML out of selection / range from a web page in Mozilla Firefox
body: <p>I currently using the following the get the selected text from a webpage into a custom firefox extension:</p>
  
  <pre><code>getSelectedText: function(){
      var textWindow = document.commandDispatcher.focusedWindow;
      var text = textWindow.getSelection();      
      if (text == null) {text =' ';}
      text = text.toString();
      text = text.replace(/^\s*$/ , "");
      text = text.replace(/\r/g, "\r");
      text = text.replace(/\n/g, "\n");
      text = text.replace(/^\s+|\s+$/g , " ");
      text = text.replace(new RegExp(/\u2019/g), "'");
      text = text.replace(new RegExp(/\u201A/g), ",");
      text = text.replace(new RegExp(/\u201B/g), "'");
      return {str:text};
  }
  </code></pre>
  
  <p>This works just fine for plain text.</p>
  
  <p>My problem is that i want all the elements of the webpage copied as well ( kind of like the webclips feature in safari )</p>
  
  <p><strong>Use case -</strong> If the user selects through a webpage with formatted text and images, I want the underlying HTML to get copied as well, so that I can accurately paste it into another XUL window - even send the contents as a rich HTML email if I so wish.</p>
  
  <p>Any pointers?</p>