public void WrapSelection(string tag, IDictionary attributes) { //Create a string for all the attributes string attributeString = String.Empty; if (attributes != null) { foreach (string key in attributes.Keys) { attributeString += key + "=\"" + attributes[key] + "\" "; } } SynchronizeSelection(); if (_type == HtmlSelectionType.TextSelection) { Interop.IHTMLTxtRange textRange = (Interop.IHTMLTxtRange)MSHTMLSelection; string oldText = textRange.GetHtmlText(); if (oldText == null) { oldText = String.Empty; } string newText = "<" + tag + " " + attributeString + ">" + oldText + "</" + tag + ">"; textRange.PasteHTML(newText); } }
/// <summary> /// Inserts the specified string into the html over the current selection /// </summary> /// <param name="html"></param> public void InsertHtml(string html) { Selection.SynchronizeSelection(); if (Selection.Type == HtmlSelectionType.ElementSelection) { //If it's a control range, we can only insert if we are in a div or td Interop.IHtmlControlRange controlRange = (Interop.IHtmlControlRange)Selection.MSHTMLSelection; int selectedItemCount = controlRange.GetLength(); if (selectedItemCount == 1) { Interop.IHTMLElement element = controlRange.Item(0); if ((String.Compare(element.GetTagName(), "div", true) == 0) || (String.Compare(element.GetTagName(), "td", true) == 0)) { element.InsertAdjacentHTML("beforeEnd", html); } } } else { Interop.IHTMLTxtRange textRange = (Interop.IHTMLTxtRange)Selection.MSHTMLSelection; textRange.PasteHTML(html); } }