/// <summary> /// The currently selected text/controls will be replaced by the given HTML code. /// If nothing is selected, the HTML code is inserted at the cursor position /// </summary> /// <param name="s_Html"></param> /// <returns></returns> public bool PasteIntoSelection(string s_Html) { if (m_pDoc2 == null) { return(false); } IHTMLSelectionObject sel = m_pDoc2.selection as IHTMLSelectionObject; if (sel == null) { return(false); } IHTMLTxtRange range = sel.createRange() as IHTMLTxtRange; if (range == null) { return(false); } //none //text //control if ((!String.IsNullOrEmpty(sel.EventType)) && (sel.EventType == "control")) { IHTMLControlRange ctlrange = range as IHTMLControlRange; if (ctlrange != null) //Control(s) selected { IHTMLElement elem = null; int ctls = ctlrange.length; for (int i = 0; i < ctls; i++) { //Remove all selected controls elem = ctlrange.item(i); if (elem != null) { RemoveNode(elem, true); } } // Now get the textrange after deleting all selected controls range = null; range = sel.createRange() as IHTMLTxtRange; } } if (range != null) { // range will be valid if nothing is selected or if text is selected // or if multiple elements are seleted range.pasteHTML(s_Html); range.collapse(false); range.select(); } return(true); }
/// <summary> /// Create a MarkupRange from a selection object. /// </summary> public MarkupRange CreateMarkupRange(IHTMLSelectionObject selection) { if (selection == null) { return(null); } // see what type of range is selected object range = selection.createRange(); if (range is IHTMLTxtRange) { return(CreateMarkupRange(range as IHTMLTxtRange)); } else if (range is IHTMLControlRange) { // we only support single-selection so a "control-range" can always // be converted into a single-element text range IHTMLControlRange controlRange = range as IHTMLControlRange; if (controlRange.length == 1) { IHTMLElement selectedElement = controlRange.item(0); MarkupRange markupRange = CreateMarkupRange(selectedElement); //return the precisely positioned text range return(markupRange); } else { Debug.Fail("Length of control range not equal to 1 (value was " + controlRange.length.ToString(CultureInfo.InvariantCulture)); return(null); } } else // null or unexpected range type { return(null); } }
public static bool SelectionIsImage(IHTMLSelectionObject selection) { // see if we have selected an image if (selection != null && selection.type == "Control") { object range = selection.createRange(); if (range is IHTMLControlRange) { IHTMLControlRange controlRange = (IHTMLControlRange)range; if (controlRange.length > 0) { IHTMLElement selectedElement = controlRange.item(0); if (selectedElement is IHTMLImgElement) { return(true); } } } } // got this far, not an image! return(false); }
/// <summary> /// Method to insert a basic table /// Will honour the existing table if passed in /// </summary> private void ProcessTable(IHTMLTable table, HtmlTableProperty tableProperties) { try { // obtain a reference to the body node and indicate table present var document = ContentUtils.GetFocusedHtmlDocument(); var bodyNode = document?.body; bool tableCreated = false; // ensure a table node has been defined to work with if (table.IsNull()) { // create the table and indicate it was created table = (IHTMLTable)document.createElement("<table>"); tableCreated = true; } // define the table border, width, cell padding and spacing table.border = tableProperties.BorderSize; if (tableProperties.TableWidth > 0) { table.width = (tableProperties.TableWidthMeasurement == MeasurementOption.Pixel) ? string.Format("{0}", tableProperties.TableWidth) : string.Format("{0}%", tableProperties.TableWidth); } else { table.width = string.Empty; } table.align = tableProperties.TableAlignment != HorizontalAlignOption.Default ? tableProperties.TableAlignment.ToString().ToLower() : string.Empty; table.cellPadding = tableProperties.CellPadding.ToString(CultureInfo.InvariantCulture); table.cellSpacing = tableProperties.CellSpacing.ToString(CultureInfo.InvariantCulture); // define the given table caption and alignment string caption = tableProperties.CaptionText; IHTMLTableCaption tableCaption = table.caption; if (!caption.IsNullOrEmpty()) { // ensure table caption correctly defined if (tableCaption.IsNull()) { tableCaption = table.createCaption(); } ((IHTMLElement)tableCaption).innerText = caption; if (tableProperties.CaptionAlignment != HorizontalAlignOption.Default) { tableCaption.align = tableProperties.CaptionAlignment.ToString().ToLower(); } if (tableProperties.CaptionLocation != VerticalAlignOption.Default) { tableCaption.vAlign = tableProperties.CaptionLocation.ToString().ToLower(); } } else { // if no caption specified remove the existing one if (!tableCaption.IsNull()) { // prior to deleting the caption the contents must be cleared ((IHTMLElement)tableCaption).innerText = null; table.deleteCaption(); } } // determine the number of rows one has to insert int numberRows, numberCols; if (tableCreated) { numberRows = Math.Max((int)tableProperties.TableRows, 1); } else { numberRows = Math.Max((int)tableProperties.TableRows, 1) - table.rows.length; } // layout the table structure in terms of rows and columns table.cols = tableProperties.TableColumns; if (tableCreated) { // this section is an optimization based on creating a new table // the section below works but not as efficiently numberCols = Math.Max((int)tableProperties.TableColumns, 1); // insert the appropriate number of rows IHTMLTableRow tableRow; for (int idxRow = 0; idxRow < numberRows; idxRow++) { tableRow = (IHTMLTableRow)table.insertRow(); // add the new columns to the end of each row for (int idxCol = 0; idxCol < numberCols; idxCol++) { tableRow.insertCell(); } } } else { // if the number of rows is increasing insert the decrepency if (numberRows > 0) { // insert the appropriate number of rows for (int idxRow = 0; idxRow < numberRows; idxRow++) { table.insertRow(); } } else { // remove the extra rows from the table for (int idxRow = numberRows; idxRow < 0; idxRow++) { table.deleteRow(table.rows.length - 1); } } // have the rows constructed // now ensure the columns are correctly defined for each row IHTMLElementCollection rows = table.rows; foreach (IHTMLTableRow tableRow in rows) { numberCols = Math.Max((int)tableProperties.TableColumns, 1) - tableRow.cells.length; if (numberCols > 0) { // add the new column to the end of each row for (int idxCol = 0; idxCol < numberCols; idxCol++) { tableRow.insertCell(); } } else { // reduce the number of cells in the given row // remove the extra rows from the table for (int idxCol = numberCols; idxCol < 0; idxCol++) { tableRow.deleteCell(tableRow.cells.length - 1); } } } } // if the table was created then it requires insertion into the DOM // otherwise property changes are sufficient if (tableCreated) { // table processing all complete so insert into the DOM var tableNode = (IHTMLDOMNode)table; var tableElement = (IHTMLElement)table; IHTMLTxtRange textRange = ContentUtils.GetTextSelectionObject(); // final insert dependant on what user has selected if (!textRange.IsNull()) { // text range selected so overwrite with a table try { string selectedText = textRange.text; if (!selectedText.IsNull()) { // place selected text into first cell var tableRow = (IHTMLTableRow)table.rows.item(0, null); ((IHTMLElement)tableRow.cells.item(0, null)).innerText = selectedText; } textRange.pasteHTML(tableElement.outerHTML); } catch (RemotingException) { } catch (UnauthorizedAccessException) { } } else { IHTMLControlRange controlRange = ContentUtils.GetControlSelectionObject(); if (!controlRange.IsNull()) { // overwrite any controls the user has selected try { // clear the selection and insert the table // only valid if multiple selection is enabled for (int idx = 1; idx < controlRange.length; idx++) { controlRange.remove(idx); } controlRange.item(0).outerHTML = tableElement.outerHTML; // this should work with initial count set to zero // controlRange.add((mshtmlControlElement)table); } catch (RemotingException) { } catch (UnauthorizedAccessException) { } } else { // insert the table at the end of the HTML ((IHTMLDOMNode)bodyNode).appendChild(tableNode); } } } else { // table has been correctly defined as being the first selected item // need to remove other selected items IHTMLControlRange controlRange = ContentUtils.GetControlSelectionObject(); if (!controlRange.IsNull()) { // clear the controls selected other than than the first table // only valid if multiple selection is enabled for (int idx = 1; idx < controlRange.length; idx++) { controlRange.remove(idx); } } } } catch (RemotingException) { } catch (UnauthorizedAccessException) { } } //ProcessTable