0

I am writing a script that needs to copy the actual, raw HTML that represents user-selected text. I do not need valid HTML (with balanced tags, etc), I need the character-by-character string from the source document that is wrapped by the selection.

For instance, if the HTML is the following:

<p>one two three <strong>four
five</strong> six seven</p>

... and the user starts the selection at the first 't' (just after the space following the word 'one') and ends the selection after the first 'v' (inside the word 'five'), I need to capture the following exact string to the clipboard:

two three <strong>four\nfiv

Note that the captured text needs to have the opening string tag but not a matching closing tag, and needs to include the line break that exists in the source document after the word 'four' (even though this does not appear in the rendered HTML).

All of the help I have found so far tries to normalize the HTML that is selected, and this is not going to work for what I need.

Is this possible to capture? If so, how do I go about doing this? Thanks in advance for any suggestions or help!

seawolf
  • 2,147
  • 3
  • 20
  • 37

1 Answers1

0

You can try the following javascript function:-

<script type="text/javascript">
function getHTMLOfSelection () {
  var range;
  if (document.selection && document.selection.createRange) {
    range = document.selection.createRange();
    return range.htmlText;
  }
  else if (window.getSelection) {
    var selection = window.getSelection();
    if (selection.rangeCount > 0) {
      range = selection.getRangeAt(0);
      var clonedSelection = range.cloneContents();
      var div = document.createElement('div');
      div.appendChild(clonedSelection);
      return div.innerHTML;
    }
    else {
      return '';
    }
  }
  else {
    return '';
  }
}
</script>
Indranil Mondal
  • 2,799
  • 3
  • 25
  • 40
  • For more see link:- http://stackoverflow.com/questions/5083682/get-selected-html-in-browser-via-javascript – Indranil Mondal Jul 02 '14 at 18:43
  • From the Mozilla Developer Network page on cloneContents(): "Partially selected nodes include the parent tags necessary to make the document fragment valid." – seawolf Jul 02 '14 at 18:51
  • Rangy does the same thing as far as I can tell... when it is used to highlight text, it modifies the HTML you have selected. – seawolf Jul 02 '14 at 18:53