2

I try to split an HTML section according to the selection point. For example suppose I have :

<table>
 <tr><td> one </td></tr>
 <tr><td> two </td></tr>
 <tr><td> tree </td></tr>
</table>     

Now if the user selects two with mouse and click on a split button, I should have :

<table>
 <tr><td> one </td></tr>
</table>

And

<table>
 <tr><td> two </td></tr>
 <tr><td> tree </td></tr>
</table> 

It can occur on any other element too (P, div, ...), and on any nested element.

I tried the following C# code:

IHTMLDocument2 doc =
webBrowser1.Document.DomDocument as IHTMLDocument2;
IHTMLBodyElement body = doc.body as IHTMLBodyElement;
if (doc.selection != null)
{        
     if (doc.selection.type == "Text")
     {
          range = doc.selection.createRange() as IHTMLTxtRange;                        
          range.moveEnd("textedit");
          // NOW range.htmlText gives me the second part, 
          // however I don't know how to extract it from the section 
          // and remain the first part
     }
 }

Now range.htmlText gives me the second fragment completely (with the parent tags), however I don't know how to extract it from the section and leave the first fragment?! execCommand("cut") may do the job, however if selection spans on several table rows that trick doesn't work.

Or any other solution?

Ahmad
  • 8,811
  • 11
  • 76
  • 141
  • 1
    What you need to know is which *parent* element contains the selection. [Example code here](http://stackoverflow.com/a/2529482/451480). It gets more complex continuing from there, because then you need to determine which parent elements to close and reopen. – Blaise May 24 '15 at 11:20
  • 1
    Why are you doing this on the server? – rism May 24 '15 at 11:21
  • @rism on server? I do it on a webBrowser Control in a c# application – Ahmad May 24 '15 at 11:25
  • Ok so to rephrase. Why cant you do this using jQuery for instance? Do you have control over the html? – rism May 24 '15 at 11:28
  • 1
    @rism No I can't do it with JQuery, its a desktop windows application. – Ahmad May 24 '15 at 11:30
  • You wouldn't use a text range for that, you would use DOM methods. For example, clone the table, append the clone as a sibling to the other table, remove, remove the appropriate rows from both instances. – Tomalak May 24 '15 at 11:57
  • @Tomalak good point, however the range.htmlText gives me what I needed! – Ahmad May 24 '15 at 12:40

0 Answers0