tags:

views:

404

answers:

1

I'm writing a simple HTML WYSIWYG editor using Microsoft's mshtml. One of the features should be selecting a header type (e.g. h1, h2, h3) for a selected text. The first assignment is no problem with the following code:

// *doc* is my IHTMLDocument
// *tag* contains the header tag

IHTMLTxtRange range = (IHTMLTxtRange)doc.selection.createRange()
string rangeText = range.text;
IHTMLElement elem = doc.createElement(tag)
elem.innerHTML = rangeText;
range.pasteHTML(elem.outerHTML);

When I try to change the header, the old one doesn't get replaced though MSDN says about pasteHTML:

Pastes HTML text into the given text range, replacing any previous text and HTML elements in the range.

This means if my HTML was

<H1>foo</H1>

after the first assignment, it gets

<H1>
<H2>asdasd</H2></H1>

after the second.

What am I doing wrong? Am I missing something?

A: 

Have you tried changing the last line to

range.pasteHTML(elem.innerHTML);

? I think the outerHTML would include the original h1 tag, and then you would paste

<H1><H2>asdasd</H2></H1>   

OVER

<H1>foo</H1>
rosscj2533