public TablePropertyWdw(HtmlTableProperty props) { InitializeComponent(); // Set Table this.TableCaption = props.CaptionText; this.BorderSize = props.BorderSize.ToString(); this.TableRows = props.TableRows.ToString(); this.TableColumns = props.TableColumns.ToString(); this.TableWidth = props.TableWidth.ToString(); this.CellPadding = props.CellPadding.ToString(); this.CellSpacing = props.CellSpacing.ToString(); DataContext = this; }
} //TableInsert // public function to modify a tables properties // ensure a table is currently selected or insertion point is within a table public bool TableModify(HtmlTableProperty tableProperties) { // define the Html Table element HtmlTable table = GetTableElement(); // if a table has been selected then process if (table != null) { ProcessTable(table, tableProperties); return(true); } else { return(false); } } //TableModify
public void TableModify_ContainingTable() { var result = ""; var tableCount = 0; System.Threading.Thread t = new System.Threading.Thread(() => { var editor = new NoteEditor(); var form = CreateForm(); form.Shown += (sender, args) => { //insert table editor.HTML = "Some Text There"; var body = editor.Document.Body.DomElement as IHTMLBodyElement; IHTMLTxtRange r = body.createTextRange() as IHTMLTxtRange; r.findText("Text"); r.select(); var sut = new HtmlTableHelper(editor); sut.TableInsert(new HtmlTableProperty(true)); //move inside table IHTMLTxtRange r2 = body.createTextRange() as IHTMLTxtRange; r2.findText("Text"); r2.select(); //modify table var prop = new HtmlTableProperty(true); prop.CaptionText = "Table modified"; sut.TableModify(prop); form.Close(); }; form.Controls.Add(editor); form.ShowDialog(); result = editor.HTML; tableCount = editor.Document.GetElementsByTagName("table").Count; }); t.SetApartmentState(System.Threading.ApartmentState.STA); t.Start(); t.Join(); Assert.IsTrue(result.Contains("Table modified")); Assert.AreEqual(1, tableCount); }
public void TableModify_GivenTable() { var result = ""; var tableCount = 0; System.Threading.Thread t = new System.Threading.Thread(() => { var editor = new NoteEditor(); var form = CreateForm(); form.Shown += (sender, args) => { //insert table editor.HTML = null; var sut = new HtmlTableHelper(editor); sut.TableInsert(new HtmlTableProperty(true)); //find table IHTMLTable table = editor.Document.GetElementsByTagName("table")[0].DomElement as IHTMLTable; //modify table var prop = new HtmlTableProperty(true); prop.CaptionText = "Table modified"; sut.TableModify(table, prop); form.Close(); }; form.Controls.Add(editor); form.ShowDialog(); result = editor.HTML; tableCount = editor.Document.GetElementsByTagName("table").Count; }); t.SetApartmentState(System.Threading.ApartmentState.STA); t.Start(); t.Join(); Assert.IsTrue(result.Contains("Table modified")); Assert.AreEqual(1, tableCount); }
public void GetTableProperties() { var tableProperties = new HtmlTableProperty(false); System.Threading.Thread t = new System.Threading.Thread(() => { var editor = new NoteEditor(); var form = CreateForm(); form.Shown += (sender, args) => { //insert table var sut = new HtmlTableHelper(editor); sut.TableInsert(new HtmlTableProperty(true)); //fill table FillTable((editor.Document.GetElementsByTagName("table")[0].DomElement) as IHTMLTable); //move inside table var body = editor.Document.Body.DomElement as IHTMLBodyElement; IHTMLTxtRange r2 = body.createTextRange() as IHTMLTxtRange; r2.findText("r2c2"); r2.select(); //modify table sut.DeleteRow(); tableProperties = sut.GetTableProperties(GetTable(editor)); form.Close(); }; form.Controls.Add(editor); form.ShowDialog(); }); t.SetApartmentState(System.Threading.ApartmentState.STA); t.Start(); t.Join(); Assert.AreEqual(2, tableProperties.TableRows); }
private void CreateButtonClick(object sender, RoutedEventArgs e) { var props = new HtmlTableProperty(); // Caption Text props.CaptionText = TableCaptionBox.Text ?? string.Empty; // Caption Alignment if (CaptionAlignmentDefault.IsChecked == true) { props.CaptionAlignment = HorizontalAlignOption.Default; } else if (CaptionAlignmentCenter.IsChecked == true) { props.CaptionAlignment = HorizontalAlignOption.Center; } else if (CaptionAlignmentLeft.IsChecked == true) { props.CaptionAlignment = HorizontalAlignOption.Left; } else if (CaptionAlignmentRight.IsChecked == true) { props.CaptionAlignment = HorizontalAlignOption.Right; } // Caption Location if (CaptionLocationBottom.IsChecked == true) { props.CaptionLocation = VerticalAlignOption.Bottom; } else if (CaptionLocationTop.IsChecked == true) { props.CaptionLocation = VerticalAlignOption.Top; } else if (CaptionLocationDefault.IsChecked == true) { props.CaptionLocation = VerticalAlignOption.Default; } // Table alignment if (TableAlignmentCenter.IsChecked == true) { props.TableAlignment = HorizontalAlignOption.Center; } else if (TableAlignmentDefault.IsChecked == true) { props.TableAlignment = HorizontalAlignOption.Default; } else if (TableAlignmentLeft.IsChecked == true) { props.TableAlignment = HorizontalAlignOption.Left; } else if (TableAlignmentRight.IsChecked == true) { props.TableAlignment = HorizontalAlignOption.Right; } // Border Size props.BorderSize = BorderSizeBox.Text.TryParseByte(2); // Table Rows props.TableRows = TableRowsBox.Text.TryParseByte(3); // Table Columns props.TableColumns = TableColumnsBox.Text.TryParseByte(3); // Table Width props.TableWidth = TableWidthBox.Text.TryParseUshort(50); // Table Width Measurement if (WidthMeasurementPercent.IsChecked == true) { props.TableWidthMeasurement = MeasurementOption.Percent; } else if (WidthMeasurementPixel.IsChecked == true) { props.TableWidthMeasurement = MeasurementOption.Pixel; } // Cell Padding props.CellPadding = CellPaddingBox.Text.TryParseByte(1); // Cell Spacing props.CellSpacing = CellSpacingBox.Text.TryParseByte(2); Value = props; Confirmed = true; Close(); }
} //TableModify public void TableModify(HtmlTable table, HtmlTableProperty tableProperties) { ProcessTable(table, tableProperties); }
// given an HtmlTable determine the table properties public HtmlTableProperty GetTableProperties(HtmlTable table) { // define a set of base table properties HtmlTableProperty tableProperties = new HtmlTableProperty(true); // if user has selected a table extract those properties if (table != null) { try { // have a table so extract the properties HtmlTableCaption caption = table.caption; // if have a caption persist the values if (caption != null) { tableProperties.CaptionText = ((HtmlElement)table.caption).innerText; if (caption.align != null) { tableProperties.CaptionAlignment = (HorizontalAlignOption)TryParseEnum(typeof(HorizontalAlignOption), caption.align, HorizontalAlignOption.Default); } if (caption.vAlign != null) { tableProperties.CaptionLocation = (VerticalAlignOption)TryParseEnum(typeof(VerticalAlignOption), caption.vAlign, VerticalAlignOption.Default); } } // look at the table properties if (table.border != null) { tableProperties.BorderSize = TryParseByte(table.border.ToString(), tableProperties.BorderSize); } if (table.align != null) { tableProperties.TableAlignment = (HorizontalAlignOption)TryParseEnum(typeof(HorizontalAlignOption), table.align, HorizontalAlignOption.Default); } // define the table rows and columns int rows = Math.Min(table.rows.length, Byte.MaxValue); int cols = Math.Min(table.cols, Byte.MaxValue); if (cols == 0 && rows > 0) { // cols value not set to get the maxiumn number of cells in the rows foreach (HtmlTableRow tableRow in table.rows) { cols = Math.Max(cols, (int)tableRow.cells.length); } } tableProperties.TableRows = (byte)Math.Min(rows, byte.MaxValue); tableProperties.TableColumns = (byte)Math.Min(cols, byte.MaxValue); // define the remaining table properties if (table.cellPadding != null) { tableProperties.CellPadding = TryParseByte(table.cellPadding.ToString(), tableProperties.CellPadding); } if (table.cellSpacing != null) { tableProperties.CellSpacing = TryParseByte(table.cellSpacing.ToString(), tableProperties.CellSpacing); } if (table.width != null) { string tableWidth = table.width.ToString(); if (tableWidth.TrimEnd(null).EndsWith("%")) { tableProperties.TableWidth = TryParseUshort(tableWidth.Remove(tableWidth.LastIndexOf("%"), 1), tableProperties.TableWidth); tableProperties.TableWidthMeasurement = MeasurementOption.Percent; } else { tableProperties.TableWidth = TryParseUshort(tableWidth, tableProperties.TableWidth); tableProperties.TableWidthMeasurement = MeasurementOption.Pixel; } } else { tableProperties.TableWidth = 0; tableProperties.TableWidthMeasurement = MeasurementOption.Pixel; } } catch (Exception ex) { // throw an exception indicating table structure change be determined throw new HtmlEditorException("Unable to determine Html Table properties.", "GetTableProperties", ex); } } // return the table properties return(tableProperties); } //GetTableProperties
// public function to create a table class // insert method then works on this table public void TableInsert(HtmlTableProperty tableProperties) { // call the private insert table method with a null table entry ProcessTable(null, tableProperties); editor._FireCursorMovedEvent(); } //TableInsert
// function to insert a basic table // will honour the existing table if passed in private void ProcessTable(HtmlTable table, HtmlTableProperty tableProperties) { try { using (new UndoUnit(Document, "Table add/modify")) { // obtain a reference to the body node and indicate table present HtmlDomNode bodyNode = (HtmlDomNode)Document.body; bool tableCreated = false; MsHtmlWrap.MarkupRange targetMarkupRange = null; // ensure a table node has been defined to work with if (table == null) { // create the table and indicate it was created table = (HtmlTable)Document.createElement("TABLE"); tableCreated = true; //markup range for selecting first cell after table creation targetMarkupRange = GetMarkupRange(); } // 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; } if (tableProperties.TableAlignment != HorizontalAlignOption.Default) { table.align = tableProperties.TableAlignment.ToString().ToLower(); } else { table.align = string.Empty; } table.cellPadding = tableProperties.CellPadding.ToString(); table.cellSpacing = tableProperties.CellSpacing.ToString(); // define the given table caption and alignment string caption = tableProperties.CaptionText; HtmlTableCaption tableCaption = table.caption; if (caption != null && caption != string.Empty) { // ensure table caption correctly defined if (tableCaption == null) { tableCaption = table.createCaption(); } ((HtmlElement)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 != null) { // prior to deleting the caption the contents must be cleared ((HtmlElement)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) - (int)table.rows.length; } // layout the table structure in terms of rows and columns table.cols = (int)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 HtmlTableRow tableRow; for (int idxRow = 0; idxRow < numberRows; idxRow++) { tableRow = table.insertRow(-1) as HtmlTableRow; // add the new columns to the end of each row for (int idxCol = 0; idxCol < numberCols; idxCol++) { tableRow.insertCell(-1); } } } 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(-1); } } 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 HtmlElementCollection rows = table.rows; foreach (HtmlTableRow tableRow in rows) { numberCols = Math.Max((int)tableProperties.TableColumns, 1) - (int)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(-1); } } 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 HtmlDomNode tableNode = (HtmlDomNode)table; HtmlElement tableElement = (HtmlElement)table; HtmlSelection selection = Document.selection; HtmlTextRange textRange = SelectionHelper.GetTextRange(Document); // final insert dependant on what user has selected if (textRange != null) { // text range selected so overwrite with a table try { string selectedText = textRange.text; if (selectedText != null) { // place selected text into first cell HtmlTableRow tableRow = table.rows.item(0, null) as HtmlTableRow; (tableRow.cells.item(0, null) as HtmlElement).innerText = selectedText; } textRange.pasteHTML(tableElement.outerHTML); } catch (Exception ex) { throw new HtmlEditorException("Invalid Text selection for the Insertion of a Table.", "ProcessTable", ex); } } else { HtmlControlRange controlRange = SelectionHelper.GetAllControls(Document); if (controlRange != null) { // 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((HtmlControlElement)table); } catch (Exception ex) { throw new HtmlEditorException("Cannot Delete all previously Controls selected.", "ProcessTable", ex); } } else { // insert the table at the end of the HTML bodyNode.appendChild(tableNode); } } } else { // table has been correctly defined as being the first selected item // need to remove other selected items HtmlControlRange controlRange = SelectionHelper.GetAllControls(Document); if (controlRange != null) { // 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); } } } //if table created, then focus the first cell if (tableCreated) { try { HtmlElement cell = targetMarkupRange.GetFirstElement(e => e is HtmlTableCell, true); if (cell != null) { SelectCell(cell as HtmlTableCell); } } catch (Exception e) { Log.Write(e); } } } } catch (Exception ex) { // throw an exception indicating table structure change error throw new HtmlEditorException("Unable to modify Html Table properties.", "ProcessTable", ex); } } //ProcessTable
public void GetTableProperties() { var tableProperties = new HtmlTableProperty(false); System.Threading.Thread t = new System.Threading.Thread(() => { var editor = new NoteEditor(); var form = CreateForm(); form.Shown += (sender, args) => { //insert table var sut = new HtmlTableHelper(editor); sut.TableInsert(new HtmlTableProperty(true)); //fill table FillTable((editor.Document.GetElementsByTagName("table")[0].DomElement) as IHTMLTable); //move inside table var body = editor.Document.Body.DomElement as IHTMLBodyElement; IHTMLTxtRange r2 = body.createTextRange() as IHTMLTxtRange; r2.findText("r2c2"); r2.select(); //modify table sut.DeleteRow(); tableProperties = sut.GetTableProperties(GetTable(editor)); form.Close(); }; form.Controls.Add(editor); form.ShowDialog(); }); t.SetApartmentState(System.Threading.ApartmentState.STA); t.Start(); t.Join(); Assert.AreEqual(2, tableProperties.TableRows); }
public void TableModify_GivenTable() { var result = ""; var tableCount = 0; System.Threading.Thread t = new System.Threading.Thread(() => { var editor = new NoteEditor(); var form = CreateForm(); form.Shown += (sender, args) => { //insert table editor.HTML = null; var sut = new HtmlTableHelper(editor); sut.TableInsert(new HtmlTableProperty(true)); //find table IHTMLTable table = editor.Document.GetElementsByTagName("table")[0].DomElement as IHTMLTable; //modify table var prop = new HtmlTableProperty(true); prop.CaptionText = "Table modified"; sut.TableModify(table, prop); form.Close(); }; form.Controls.Add(editor); form.ShowDialog(); result = editor.HTML; tableCount = editor.Document.GetElementsByTagName("table").Count; }); t.SetApartmentState(System.Threading.ApartmentState.STA); t.Start(); t.Join(); Assert.IsTrue(result.Contains("Table modified")); Assert.AreEqual(1, tableCount); }
public void TableModify_ContainingTable() { var result = ""; var tableCount = 0; System.Threading.Thread t = new System.Threading.Thread(() => { var editor = new NoteEditor(); var form = CreateForm(); form.Shown += (sender, args) => { //insert table editor.HTML = "Some Text There"; var body = editor.Document.Body.DomElement as IHTMLBodyElement; IHTMLTxtRange r = body.createTextRange() as IHTMLTxtRange; r.findText("Text"); r.select(); var sut = new HtmlTableHelper(editor); sut.TableInsert(new HtmlTableProperty(true)); //move inside table IHTMLTxtRange r2 = body.createTextRange() as IHTMLTxtRange; r2.findText("Text"); r2.select(); //modify table var prop = new HtmlTableProperty(true); prop.CaptionText = "Table modified"; sut.TableModify(prop); form.Close(); }; form.Controls.Add(editor); form.ShowDialog(); result = editor.HTML; tableCount = editor.Document.GetElementsByTagName("table").Count; }); t.SetApartmentState(System.Threading.ApartmentState.STA); t.Start(); t.Join(); Assert.IsTrue(result.Contains("Table modified")); Assert.AreEqual(1, tableCount); }