/// <summary> /// Parse color property to add only valid color. /// </summary> /// <param name="propName">the name of the css property to add</param> /// <param name="propValue">the value of the css property to add</param> /// <param name="properties">the properties collection to add to</param> private static void ParseColorProperty(string propName, string propValue, Dictionary <string, string> properties) { if (CssValueParser.IsColorValid(propValue)) { properties[propName] = propValue; } }
/// <summary> /// Parse length property to add only valid lengths. /// </summary> /// <param name="propName">the name of the css property to add</param> /// <param name="propValue">the value of the css property to add</param> /// <param name="properties">the properties collection to add to</param> private static void ParseLengthProperty(string propName, string propValue, Dictionary <string, string> properties) { if (CssValueParser.IsValidLength(propValue) || propValue.Equals(CssConstants.Auto, StringComparison.OrdinalIgnoreCase)) { properties[propName] = propValue; } }
/// <summary> /// Split multi direction value into the proper direction values (left, top, right, bottom). /// </summary> private static void SplitMultiDirectionValues(string propValue, out string left, out string top, out string right, out string bottom) { top = null; left = null; right = null; bottom = null; string[] values = CssValueParser.SplitValues(propValue); switch (values.Length) { case 1: top = left = right = bottom = values[0]; break; case 2: top = bottom = values[0]; left = right = values[1]; break; case 3: top = values[0]; left = right = values[1]; bottom = values[2]; break; case 4: top = values[0]; right = values[1]; bottom = values[2]; left = values[3]; break; } }
/// <summary> /// Applies style to all boxes in the tree.<br/> /// If the html tag has style defined for each apply that style to the css box of the tag.<br/> /// If the html tag has "class" attribute and the class name has style defined apply that style on the tag css box.<br/> /// If the html tag has "style" attribute parse it and apply the parsed style on the tag css box.<br/> /// If the html tag is "style" tag parse it content and add to the css data for all future tags parsing.<br/> /// If the html tag is "link" that point to style data parse it content and add to the css data for all future tags parsing.<br/> /// </summary> /// <param name="box"></param> /// <param name="bridge"> </param> /// <param name="cssData"> </param> /// <param name="cssDataChanged">check if the css data has been modified by the handled html not to change the base css data</param> private static void CascadeStyles(CssBox box, object bridge, ref CssData cssData, ref bool cssDataChanged) { box.InheritStyle(); if (box.HtmlTag != null) { // try assign style using the html element tag AssignCssBlocks(box, cssData, box.HtmlTag.Name); // try assign style using the "class" attribute of the html element if (box.HtmlTag.HasAttribute("class")) { AssignCssBlocks(box, cssData, "." + box.HtmlTag.Attributes["class"]); AssignCssBlocks(box, cssData, box.HtmlTag.Name + "." + box.HtmlTag.Attributes["class"]); } // try assign style using the "id" attribute of the html element if (box.HtmlTag.HasAttribute("id")) { AssignCssBlocks(box, cssData, "#" + box.HtmlTag.Attributes["id"]); } HtmlParser.TranslateAttributes(box.HtmlTag, box); // Check for the style="" attribute if (box.HtmlTag.HasAttribute("style")) { var block = CssParser.ParseCssBlockImp(box.HtmlTag.Name, box.HtmlTag.Attributes["style"]); AssignCssBlock(box, block); } // Check for the <style> tag if (box.HtmlTag.Name.Equals("style", StringComparison.CurrentCultureIgnoreCase) && box.Boxes.Count == 1) { CloneCssData(ref cssData, ref cssDataChanged); CssParser.ParseStyleSheet(cssData, box.Boxes[0].Text); } // Check for the <link rel=stylesheet> tag if (box.HtmlTag.Name.Equals("link", StringComparison.CurrentCultureIgnoreCase) && box.GetAttribute("rel", string.Empty).Equals("stylesheet", StringComparison.CurrentCultureIgnoreCase)) { CloneCssData(ref cssData, ref cssDataChanged); var styleSheet = CssValueParser.GetStyleSheet(box.GetAttribute("href", string.Empty), bridge); CssParser.ParseStyleSheet(cssData, styleSheet); } } foreach (var childBox in box.Boxes) { CascadeStyles(childBox, bridge, ref cssData, ref cssDataChanged); } }
/// <summary> /// Set the selected text style (selection text color and background color). /// </summary> /// <param name="htmlContainer"> </param> /// <param name="cssData">the style data</param> private static void SetTextSelectionStyle(HtmlContainer htmlContainer, CssData cssData) { htmlContainer.SelectionForeColor = Color.Empty; htmlContainer.SelectionBackColor = Color.Empty; if (cssData.ContainsCssBlock("::selection")) { var blocks = cssData.GetCssBlock("::selection"); foreach (var block in blocks) { if (block.Properties.ContainsKey("color")) { htmlContainer.SelectionForeColor = CssValueParser.GetActualColor(block.Properties["color"]); } if (block.Properties.ContainsKey("background-color")) { htmlContainer.SelectionBackColor = CssValueParser.GetActualColor(block.Properties["background-color"]); } } } }
/// <summary> /// Parse a complex border property value that contains multiple css properties into specific css properties. /// </summary> /// <param name="propValue">the value of the property to parse to specific values</param> /// <param name="direction">the left, top, right or bottom direction of the border to parse</param> /// <param name="properties">the properties collection to add the specific properties to</param> private static void ParseBorderProperty(string propValue, string direction, Dictionary <string, string> properties) { string borderWidth; string borderStyle; string borderColor; CssValueParser.ParseBorder(propValue, out borderWidth, out borderStyle, out borderColor); if (direction != null) { if (borderWidth != null) { properties["border" + direction + "-width"] = borderWidth; } if (borderStyle != null) { properties["border" + direction + "-style"] = borderStyle; } if (borderColor != null) { properties["border" + direction + "-color"] = borderColor; } } else { if (borderWidth != null) { ParseBorderWidthProperty(borderWidth, properties); } if (borderStyle != null) { ParseBorderStyleProperty(borderStyle, properties); } if (borderColor != null) { ParseBorderColorProperty(borderColor, properties); } } }