public string CleanStyles(string original, string toLeave) { //Remove all styles except that are specified in toLeave param (separated with | ) var styleObj = new StyleAttributeString(original); Dictionary<String, bool> leaveDic = new Dictionary<string, bool>(); foreach (string style in toLeave.Split('|')) { leaveDic.Add(style, true); } List<String> toRemoveList = new List<string>(); foreach (var stylePair in styleObj.Properties) { if (!leaveDic.ContainsKey(stylePair.Key)) { toRemoveList.Add(stylePair.Key); } } foreach (string removeName in toRemoveList) { styleObj.DeleteProperty(removeName); } return styleObj.ToString(); }
public static void ApplyTableModes(XmlDocument document, XmlNamespaceManager xmlns) { var tables = document.DocumentElement.SelectNodes("//table"); if (tables == null) return; foreach (XmlElement table in tables) { var rows = table.SelectNodes(@"tr | tbody/tr"); if (rows == null) continue; var tableModel = new TableCell[100, 100]; int tableModelRowCount = 0; int tableModelColCount = 0; var tableCellsList = new List<TableCell>(); for (int rowIndex = 0; rowIndex < rows.Count; rowIndex++) { var row = (XmlElement)rows[rowIndex]; var cells = row.SelectNodes("th | td"); if (cells == null) continue; for (int colIndex = 0; colIndex < cells.Count; colIndex++) { var cell = (XmlElement)cells[colIndex]; var colSpan = cell.Attributes["colSpan"] != null ? Convert.ToInt32(cell.Attributes["colSpan"].Value) : 1; var rowSpan = cell.Attributes["rowSpan"] != null ? Convert.ToInt32(cell.Attributes["rowSpan"].Value) : 1; var styleAttr = cell.Attributes["style"]; var style = new StyleAttributeString(styleAttr != null ? styleAttr.Value : String.Empty); var leftBorderVisible = IsCellBorderVisible(style, "border-left"); var rightBorderVisible = IsCellBorderVisible(style, "border-right"); var topBorderVisible = IsCellBorderVisible(style, "border-top"); var bottomBorderVisible = IsCellBorderVisible(style, "border-bottom"); var tableCell = new TableCell(cell) { RowNumber = rowIndex, ColumnNumber = colIndex, TopBorderVisible = topBorderVisible, RightBorderVisible = rightBorderVisible, BottomBorderVisible = bottomBorderVisible, LeftBorderVisible = leftBorderVisible }; tableCellsList.Add(tableCell); tableModelRowCount = Math.Max(tableModelRowCount, rowIndex + rowSpan); tableModelColCount = Math.Max(tableModelColCount, colIndex + colSpan); for (int i = 0; i < rowSpan; i++) { var colOffset = 0; for (int j = 0; j < colSpan; j++) { while (tableModel[rowIndex + i, colIndex + colOffset + j] != null) { colOffset++; } tableModel[rowIndex + i, colIndex + colOffset + j] = tableCell; } } } } for (int row = 0; row < tableModelRowCount; row++) { for (int col = 0; col < tableModelColCount; col++) { var cell = tableModel[row, col]; if (!cell.LeftBorderVisible && (col == 0 || tableModel[row, col - 1].CellType == 0)) { cell.CellType = InternalCellType.Outher; } else if (!cell.TopBorderVisible && (row == 0 || tableModel[row - 1, col].CellType == 0)) { cell.CellType = InternalCellType.Outher; } } } for (int row = 0; row < tableModelRowCount; row++) { for (int col = 0; col < tableModelColCount; col++) { var cell = tableModel[row, col]; if (cell.CellType == InternalCellType.Inner) { // Top if (row == 0 || tableModel[row - 1, col].CellType == InternalCellType.Outher) { cell.Edges |= CellEdgeType.Top; } // Left if (col == 0 || tableModel[row, col - 1].CellType == InternalCellType.Outher) { cell.Edges |= CellEdgeType.Left; } // Bottom if (row == tableModelRowCount - 1 || tableModel[row + 1, col].CellType == InternalCellType.Outher) { cell.Edges |= CellEdgeType.Bottom; } // Right if (col == tableModelColCount - 1 || tableModel[row, col + 1].CellType == InternalCellType.Outher) { cell.Edges |= CellEdgeType.Right; } } } } foreach (var tableCell in tableCellsList) { if (tableCell.CellType == InternalCellType.Inner) SetElementClass(tableCell.XmlElement, "inner-cell"); if (tableCell.CellType == InternalCellType.Outher) SetElementClass(tableCell.XmlElement, "outher-cell"); if ((tableCell.Edges & CellEdgeType.Top) == CellEdgeType.Top && (tableCell.Edges & CellEdgeType.Left) == CellEdgeType.Left) SetElementClass(tableCell.XmlElement, "ui-corner-tl"); if ((tableCell.Edges & CellEdgeType.Bottom) == CellEdgeType.Bottom && (tableCell.Edges & CellEdgeType.Left) == CellEdgeType.Left) SetElementClass(tableCell.XmlElement, "ui-corner-bl"); if ((tableCell.Edges & CellEdgeType.Top) == CellEdgeType.Top && (tableCell.Edges & CellEdgeType.Right) == CellEdgeType.Right) SetElementClass(tableCell.XmlElement, "ui-corner-tr"); if ((tableCell.Edges & CellEdgeType.Bottom) == CellEdgeType.Bottom && (tableCell.Edges & CellEdgeType.Right) == CellEdgeType.Right) SetElementClass(tableCell.XmlElement, "ui-corner-br"); if ((tableCell.Edges & CellEdgeType.Top) == CellEdgeType.Top) SetElementClass(tableCell.XmlElement, "cell-top"); if ((tableCell.Edges & CellEdgeType.Right) == CellEdgeType.Right) SetElementClass(tableCell.XmlElement, "cell-right"); if ((tableCell.Edges & CellEdgeType.Bottom) == CellEdgeType.Bottom) SetElementClass(tableCell.XmlElement, "cell-bottom"); if ((tableCell.Edges & CellEdgeType.Left) == CellEdgeType.Left) SetElementClass(tableCell.XmlElement, "cell-left"); } } }
private static bool IsCellBorderVisible(StyleAttributeString style, string styleProperty) { var solidBorders = new[] { "solid", "dashed", "dotted", "double", "groove", "ridge", "inset", "outset" }; bool borderVisible = false; if (style.Properties.ContainsKey(styleProperty)) { foreach (var solidBorder in solidBorders) { borderVisible = style.Properties[styleProperty].Contains(solidBorder); if (borderVisible) break; } } return borderVisible; }
public string SetStyle(string original, string style, string value) { var styleObj = new StyleAttributeString(original); styleObj.AssignProperty(style, value); return styleObj.ToString(); }
public string RemoveStyle(string original, string style) { var styleObj = new StyleAttributeString(original); styleObj.DeleteProperty(style); return styleObj.ToString(); }
public string GetStyle(string original, string style) { var styleObj = new StyleAttributeString(original); return styleObj.GetProperty(style); }
private static void ApplyTextboxMods(XmlDocument document, XmlNamespaceManager xmlns) { //<input type="text" hideFocus="1" class="xdTextBox" var ns = xmlns.LookupPrefix(@"http://schemas.microsoft.com/office/infopath/2003"); var ctrlIdAttrName = ns + ":CtrlId"; var textboxes = document.SelectNodes(String.Format("//span[@{0}:xctname='PlainText']", ns), xmlns); foreach ( XmlElement textbox in textboxes ) { var controlName = textbox.Attributes[ctrlIdAttrName].Value; var styleAttr = textbox.Attributes["style"]; var parent = ((XmlElement) textbox.ParentNode); StyleAttributeString style = null; if ( styleAttr != null ) style = new StyleAttributeString(styleAttr.Value); XmlElement inputField = null; if ( style != null && style.Properties.ContainsKey("overflow-y") ) { inputField = document.CreateElement("textarea"); style.AssignProperty("height", "100%"); style.AssignProperty("min-height", "36px"); SetElementClass(parent, "textarea-container"); } else { inputField = document.CreateElement("input"); if ( style != null && style.Properties.ContainsKey("height") ) { style.Properties.Remove("height"); } } inputField.SetAttribute("id", controlName); inputField.SetAttribute("name", controlName); inputField.SetAttribute("type", "text"); if ( style != null ) inputField.SetAttribute("style", style.ToString()); parent.AppendChild(inputField); } }
public static void ApplySignboxMods(XmlDocument document, XmlNamespaceManager xmlns) { var body = (XmlElement) document.DocumentElement.SelectSingleNode("//body"); var dialog = document.CreateElement("div"); dialog.SetAttribute("data-role", "page"); dialog.SetAttribute("id", "popup"); dialog.SetAttribute("data-theme", "c"); var header = document.CreateElement("div"); header.SetAttribute("data-role", "header"); header.SetAttribute("data-position", "inline"); var headerText = document.CreateElement("h1"); headerText.InnerText = "Signature Pad"; var clearButton = document.CreateElement("a"); clearButton.SetAttribute("id", "sigPadClearButton"); clearButton.SetAttribute("href", "#"); clearButton.SetAttribute("data-icon", "refresh"); clearButton.InnerText = "Clear"; var content = document.CreateElement("div"); content.SetAttribute("data-role", "content"); var form = document.CreateElement("form"); form.SetAttribute("method", "post"); form.SetAttribute("class", "sigPad"); form.SetAttribute("action", "#"); form.SetAttribute("id", "sigDialogPad"); var sigWrapper = document.CreateElement("div"); SetElementClass(sigWrapper, "sig"); SetElementClass(sigWrapper, "sigWrapper"); var sigCanvas = document.CreateElement("canvas"); SetElementClass(sigCanvas, "pad"); sigCanvas.SetAttribute("height", "200"); sigCanvas.SetAttribute("width", "500"); var sigInput = document.CreateElement("input"); sigInput.SetAttribute("type", "hidden"); sigInput.SetAttribute("name", "output"); sigInput.SetAttribute("class", "output"); var footer = document.CreateElement("div"); footer.SetAttribute("data-role", "footer"); var footerText = document.CreateElement("h4"); footerText.InnerText = ""; header.AppendChild(headerText); header.AppendChild(clearButton); sigWrapper.AppendChild(sigCanvas); sigWrapper.AppendChild(sigInput); form.AppendChild(sigWrapper); content.AppendChild(form); footer.AppendChild(footerText); dialog.AppendChild(header); dialog.AppendChild(content); dialog.AppendChild(footer); body.AppendChild(dialog); var ns = xmlns.LookupPrefix(@"http://schemas.microsoft.com/office/infopath/2003"); var ctrlIdAttrName = ns + ":CtrlId"; var signpads = document.SelectNodes(String.Format("//object[@{0}:xctname='inkpicture']", ns), xmlns); foreach ( XmlElement signpad in signpads ) { /* <a class="signLink" href="#popup" id="CTRL88_5" data-role="button" data-rel="dialog" data-transition="none"> <input type="hidden" class="output" /> <img class="preview"/> </a */ var controlName = signpad.Attributes[ctrlIdAttrName].Value; var styleAttr = signpad.Attributes["style"]; var style = new StyleAttributeString(styleAttr); var parent = (XmlElement) signpad.ParentNode; if ( parent.LocalName == "if" ) parent = (XmlElement) parent.ParentNode; var button = document.CreateElement("a"); button.SetAttribute("class", "signLink"); button.SetAttribute("href", "#popup"); button.SetAttribute("id", controlName); //button.SetAttribute("data-role", "button"); button.SetAttribute("data-rel", "dialog"); button.SetAttribute("data-transition", "none"); //button.InnerText = "Signature pad"; var input = document.CreateElement("input"); input.SetAttribute("type", "hidden"); input.SetAttribute("class", "output"); var img = document.CreateElement("img"); img.SetAttribute("class", "preview"); img.SetAttribute("src", "sign-here.gif"); if ( style.Properties.ContainsKey("width") ) { var newStyle = new StyleAttributeString(); newStyle.AssignProperty("width", style.Properties["width"]); img.SetAttribute("style", newStyle.ToString()); } button.AppendChild(input); button.AppendChild(img); parent.AppendChild(button); } }
public static void RemoveStyleAttribute(XmlElement element, double widthAdj) { if ( element == null ) return; var preserveStyles = new List<string>(new[] { "width", "height", "border", "border-bottom", "border-top", "border-left", "border-right", "border-bottom-color", "border-bottom-left-radius", "border-bottom-right-radius", "border-bottom-style", "border-bottom-width", "border-left-color", "border-left-style", "border-left-width", "border-right-color", "border-right-style", "border-right-width", "border-top-color", "border-top-left-radius", "border-top-right-radius", "border-top-style", "border-top-width", "vertical-align", "overflow-x", "overflow-y" }); var styleAdjustments = new Dictionary<string, string> { {" 0.5pt", " 0.75pt"}, }; var removeAttributes = new List<string>(new[] { "size", "face", "color" }); if ( element.Attributes["style"] != null ) { var style = new StyleAttributeString(element.Attributes["style"].Value); var newStyle = new StyleAttributeString(""); foreach (var property in style.Properties) { if ( preserveStyles.Contains(property.Key) ) { string newValue = property.Value; foreach ( var adj in styleAdjustments ) { newValue = newValue.Replace(adj.Key, adj.Value); } if ( property.Key == "width" && newValue != "auto" && !newValue.Contains("%") ) { double tmp = GetElementWidth(newValue) * widthAdj; newValue = String.Concat((int) tmp, "px"); } newStyle.Properties.Add(property.Key, newValue); } } element.Attributes.RemoveNamedItem("style"); element.SetAttribute("style", newStyle.ToString()); } foreach (var removeAttribute in removeAttributes) { if ( element.Attributes[removeAttribute] != null ) { element.Attributes.RemoveNamedItem(removeAttribute); } } foreach (XmlNode node in element.ChildNodes) { RemoveStyleAttribute(node as XmlElement, widthAdj); } }
public static double CalculateWidthAdjustment(XmlDocument xmlDocument) { const double screenPadding = 4; const double screenWidth = 768; var sections = xmlDocument.SelectNodes("//*[contains(@class,'xdLayout')] | //*[contains(@class,'xdSection')]"); double maxWidth = 0; foreach (XmlElement section in sections) { var style = new StyleAttributeString(section.Attributes["style"].Value); if ( style.Properties.ContainsKey("width") ) { double tmp = GetElementWidth(style.Properties["width"]); maxWidth = maxWidth > tmp ? maxWidth : tmp; } } return (screenWidth - (2 * screenPadding)) / maxWidth; }