IHTMLTxtRange.pasteHTML does not replace old HTML

I am writing a simple WYSIWYG HTML editor using Microsoft mshtml. One of the functions should be the choice of the type of heading (for example, h1, h2, h3) for the selected text. The first assignment does not cause problems 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 is not replaced, although MSDN says pasteHTML:

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

That means if my HTML was

<H1>foo</H1>

after the first assignment, he gets

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

after the second.

What am I doing wrong? Did I miss something?

+3
source share
3 answers

You tried to change the last line to

range.pasteHTML(elem.innerHTML);

?   , outerHTML h1,

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

<H1>foo</H1>
+1

,

   string rangeText = range.text;
    IHTMLDOMNode parentNode = range.parentElement() as IHTMLDOMNode;
    if (parentNode.nodeName.Contains("H") && parentNode.nodeName.Length == 2)
    {
        parentNode.removeNode(true);
    }
    IHTMLElement elem = doc.createElement(tag);
    elem.innerHTML = rangeText; 
    range.pasteHTML(elem.outerHTML); 
+1

:

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

Source: https://habr.com/ru/post/1715997/


All Articles