Cześć, mam problem z zaznaczaniem tekstu na stronie razem z kodem html.

Mam funkcję do pobierania kodu html:

  1. function getSelectionHtml () {
  2. var html = "";
  3. if (typeof window.getSelection != "undefined") {
  4. var sel = window.getSelection();
  5. if (sel.rangeCount) {
  6. var container = document.createElement("div");
  7. for (var i = 0, len = sel.rangeCount; i < len; ++i) {
  8. container.appendChild(sel.getRangeAt(i).cloneContents());
  9. }
  10. html = container.innerHTML;
  11. }
  12. } else if (typeof document.selection != "undefined") {
  13. if (document.selection.type == "Text") {
  14. html = document.selection.createRange().htmlText;
  15. }
  16. }
  17. return html;
  18. }


Z tym, że działa ona nie tak jak potrzebuję, mianowicie w przypadku gdy mamy kod html:

  1. <p><strong>Tekst 1</strong></p>
  2. <p>tekst 2</p>


i na stronie zaznacze interesujący mnie tekst, chce otrzymać wynik:

  1. kst 1</strong></p>
  2. <p>teks


A zwraca niestety całość, ze wszystkimi zamkniętymi i otwartymi tagami.
Jakieś naprowadzenie, pomysł jak to poprawić?