Ejemplo n.º 1
0
 /// <summary>
 /// Generate html from the given dom tree.<br/>
 /// Generate all the tyle inside the html.
 /// </summary>
 /// <param name="root">the box of the html generate html from</param>
 /// <param name="styleGen">Optional: controls the way styles are generated when html is generated</param>
 /// <param name="onlySelected">Optional: true - generate only selected html subset, false - generate all (default - false)</param>
 /// <returns>generated html</returns>
 public static string GenerateHtml(CssBox root, HtmlGenerationStyle styleGen = HtmlGenerationStyle.Inline, bool onlySelected = false)
 {
     var sb = new StringBuilder();
     if (root != null)
     {
         WriteHtml(sb, root, 0, styleGen, onlySelected ? CollectSelectedHtmlTags(root) : null);
     }
     return sb.ToString();
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Generate html from the given dom tree.<br/>
        /// Generate all the tyle inside the html.
        /// </summary>
        /// <param name="root">the box of the html generate html from</param>
        /// <param name="styleGen">Optional: controls the way styles are generated when html is generated</param>
        /// <param name="onlySelected">Optional: true - generate only selected html subset, false - generate all (default - false)</param>
        /// <returns>generated html</returns>
        public static string GenerateHtml(CssBox root, HtmlGenerationStyle styleGen = HtmlGenerationStyle.Inline, bool onlySelected = false)
        {
            var sb = new StringBuilder();

            if (root != null)
            {
                WriteHtml(sb, root, 0, styleGen, onlySelected ? CollectSelectedHtmlTags(root) : null);
            }
            return(sb.ToString());
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Write the given html dom subtree into the given string builder.
        /// </summary>
        /// <param name="sb">the string builder to write html into</param>
        /// <param name="box">the html sub-tree to write</param>
        /// <param name="indent">the indent to use for nice formating</param>
        /// <param name="styleGen">Controls the way styles are generated when html is generated</param>
        /// <param name="selectedTags">Control if to generate only selected tags, if given only tags found in collection will be generated</param>
        private static void WriteHtml(StringBuilder sb, CssBox box, int indent, HtmlGenerationStyle styleGen,
                                      Dictionary <HtmlTag, bool> selectedTags)
        {
            if (box.HtmlTag != null && selectedTags != null && !selectedTags.ContainsKey(box.HtmlTag))
            {
                return;
            }

            if (box.HtmlTag != null)
            {
                if (box.HtmlTag.Name != "link" || !box.HtmlTag.Attributes.ContainsKey("href") ||
                    (!box.HtmlTag.Attributes["href"].StartsWith("property") &&
                     !box.HtmlTag.Attributes["href"].StartsWith("method")))
                {
                    WriteHtmlTag(sb, box, indent, styleGen);
                    indent = indent + (box.HtmlTag.IsSingle ? 0 : 1);
                }

                if (styleGen == HtmlGenerationStyle.InHeader && box.HtmlTag.Name == "html" &&
                    box.HtmlContainer.CssData != null)
                {
                    sb.Append(new string(' ', indent * 4));
                    sb.AppendLine("<head>");
                    WriteStylesheet(sb, box.HtmlContainer.CssData, indent + 1);
                    sb.Append(new string(' ', indent * 4));
                    sb.AppendLine("</head>");
                }
            }

            if (box.Words.Count > 0)
            {
                sb.Append(new string(' ', indent * 4));
                foreach (CssRect word in box.Words)
                {
                    if (selectedTags == null || word.Selected)
                    {
                        string wordText = GetSelectedWord(word, selectedTags != null);
                        sb.Append(HtmlUtils.EncodeHtml(wordText));
                    }
                }
                sb.AppendLine();
            }

            foreach (CssBox childBox in box.Boxes)
            {
                WriteHtml(sb, childBox, indent, styleGen, selectedTags);
            }

            if (box.HtmlTag != null && !box.HtmlTag.IsSingle)
            {
                sb.Append(new string(' ', Math.Max((indent - 1) * 4, 0)));
                sb.AppendFormat("</{0}>", box.HtmlTag.Name);
                sb.AppendLine();
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Generate html from the given DOM tree.<br/>
        /// Generate all the style inside the html, in header or for every tag depending on <paramref name="styleGen"/> value.
        /// </summary>
        /// <param name="root">the box of the html generate html from</param>
        /// <param name="styleGen">Optional: controls the way styles are generated when html is generated</param>
        /// <param name="onlySelected">Optional: true - generate only selected html subset, false - generate all (default - false)</param>
        /// <returns>generated html</returns>
        public static string GenerateHtml(CssBox root, HtmlGenerationStyle styleGen = HtmlGenerationStyle.Inline, bool onlySelected = false)
        {
            var sb = new StringBuilder();

            if (root != null)
            {
                var selectedBoxes = onlySelected ? CollectSelectedBoxes(root) : null;
                var selectionRoot = onlySelected ? GetSelectionRoot(root, selectedBoxes) : null;
                WriteHtml(root.HtmlContainer.CssParser, sb, root, styleGen, selectedBoxes, selectionRoot);
            }
            return(sb.ToString());
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Write the given html DOM sub-tree into the given string builder.<br/>
        /// If <paramref name="selectedBoxes"/> are given write html only from those tags.
        /// </summary>
        /// <param name="sb">the string builder to write html into</param>
        /// <param name="box">the html sub-tree to write</param>
        /// <param name="styleGen">Controls the way styles are generated when html is generated</param>
        /// <param name="selectedBoxes">Control if to generate only selected boxes, if given only boxes found in hash will be generated</param>
        /// <param name="selectionRoot">the box the is the root of selected boxes (the first box to contain multiple selected boxes)</param>
        private static void WriteHtml(StringBuilder sb, CssBox box, HtmlGenerationStyle styleGen, Dictionary <CssBox, bool> selectedBoxes, CssBox selectionRoot)
        {
            if (box.HtmlTag == null || selectedBoxes == null || selectedBoxes.ContainsKey(box))
            {
                if (box.HtmlTag != null)
                {
                    if (box.HtmlTag.Name != "link" || !box.HtmlTag.Attributes.ContainsKey("href") ||
                        (!box.HtmlTag.Attributes["href"].StartsWith("property") && !box.HtmlTag.Attributes["href"].StartsWith("method")))
                    {
                        WriteHtmlTag(sb, box, styleGen);
                        if (box == selectionRoot)
                        {
                            sb.Append("<!--StartFragment-->");
                        }
                    }

                    if (styleGen == HtmlGenerationStyle.InHeader && box.HtmlTag.Name == "html" && box.HtmlContainer.CssData != null)
                    {
                        sb.AppendLine("<head>");
                        WriteStylesheet(sb, box.HtmlContainer.CssData);
                        sb.AppendLine("</head>");
                    }
                }

                if (box.Words.Count > 0)
                {
                    foreach (var word in box.Words)
                    {
                        if (selectedBoxes == null || word.Selected)
                        {
                            var wordText = GetSelectedWord(word, selectedBoxes != null);
                            sb.Append(HtmlUtils.EncodeHtml(wordText));
                        }
                    }
                }

                foreach (var childBox in box.Boxes)
                {
                    WriteHtml(sb, childBox, styleGen, selectedBoxes, selectionRoot);
                }

                if (box.HtmlTag != null && !box.HtmlTag.IsSingle)
                {
                    if (box == selectionRoot)
                    {
                        sb.Append("<!--EndFragment-->");
                    }
                    sb.AppendFormat("</{0}>", box.HtmlTag.Name);
                }
            }
        }
Ejemplo n.º 6
0
        public string GetOuterHtml(HtmlGenerationStyle generationStyle = HtmlGenerationStyle.None)
        {
            var builder = new StringBuilder();

            var selfClosing = !HasChildren && !string.IsNullOrEmpty(innerText);

            if (!selfClosing)
            {
                if (!string.IsNullOrEmpty(innerText))
                {
                    builder.Append(innerText);
                }
                if (HasChildren)
                {
                    foreach (var child in Children)
                    {
                        builder.Append(child.GetOuterHtml(generationStyle));
                    }
                }
            }

            return(builder.ToString());
        }
Ejemplo n.º 7
0
 /// <summary>
 /// Get html from the current DOM tree with style if requested.
 /// </summary>
 /// <param name="styleGen">Optional: controls the way styles are generated when html is generated (default: <see cref="HtmlGenerationStyle.Inline"/>)</param>
 /// <returns>generated html</returns>
 public string GetHtml(HtmlGenerationStyle styleGen = HtmlGenerationStyle.Inline)
 {
     return _htmlContainerInt.GetHtml(styleGen);
 }
Ejemplo n.º 8
0
        /// <summary>
        /// Write the given html tag with all its attributes and styles.
        /// </summary>
        /// <param name="cssParser">used to parse CSS data</param>
        /// <param name="sb">the string builder to write html into</param>
        /// <param name="box">the css box with the html tag to write</param>
        /// <param name="styleGen">Controls the way styles are generated when html is generated</param>
        private static void WriteHtmlTag(CssParser cssParser, StringBuilder sb, CssBox box, HtmlGenerationStyle styleGen)
        {
            sb.AppendFormat("<{0}", box.HtmlTag.Name);

            // collect all element style properties including from stylesheet
            var tagStyles   = new Dictionary <string, string>();
            var tagCssBlock = box.HtmlContainer.CssData.GetCssBlock(box.HtmlTag.Name);

            if (tagCssBlock != null)
            {
                // TODO:a handle selectors
                foreach (var cssBlock in tagCssBlock)
                {
                    foreach (var prop in cssBlock.Properties)
                    {
                        tagStyles[prop.Key] = prop.Value;
                    }
                }
            }

            if (box.HtmlTag.HasAttributes())
            {
                sb.Append(" ");
                foreach (var att in box.HtmlTag.Attributes)
                {
                    // handle image tags by inserting the image using base64 data
                    if (styleGen == HtmlGenerationStyle.Inline && att.Key == HtmlConstants.Style)
                    {
                        // if inline style add the styles to the collection
                        var block = cssParser.ParseCssBlock(box.HtmlTag.Name, box.HtmlTag.TryGetAttribute("style"));
                        foreach (var prop in block.Properties)
                        {
                            tagStyles[prop.Key] = prop.Value;
                        }
                    }
                    else if (styleGen == HtmlGenerationStyle.Inline && att.Key == HtmlConstants.Class)
                    {
                        // if inline style convert the style class to actual properties and add to collection
                        var cssBlocks = box.HtmlContainer.CssData.GetCssBlock("." + att.Value);
                        if (cssBlocks != null)
                        {
                            // TODO:a handle selectors
                            foreach (var cssBlock in cssBlocks)
                            {
                                foreach (var prop in cssBlock.Properties)
                                {
                                    tagStyles[prop.Key] = prop.Value;
                                }
                            }
                        }
                    }
                    else
                    {
                        sb.AppendFormat("{0}=\"{1}\" ", att.Key, att.Value);
                    }
                }

                sb.Remove(sb.Length - 1, 1);
            }

            // if inline style insert the style tag with all collected style properties
            if (styleGen == HtmlGenerationStyle.Inline && tagStyles.Count > 0)
            {
                var cleanTagStyles = StripDefaultStyles(box, tagStyles);
                if (cleanTagStyles.Count > 0)
                {
                    sb.Append(" style=\"");
                    foreach (var style in cleanTagStyles)
                    {
                        sb.AppendFormat("{0}: {1}; ", style.Key, style.Value);
                    }
                    sb.Remove(sb.Length - 1, 1);
                    sb.Append("\"");
                }
            }

            sb.AppendFormat("{0}>", box.HtmlTag.IsSingle ? "/" : "");
        }
Ejemplo n.º 9
0
 /// <summary>
 /// Get html from the current DOM tree with style if requested.
 /// </summary>
 /// <param name="styleGen">Optional: controls the way styles are generated when html is generated (default: <see cref="HtmlGenerationStyle.Inline"/>)</param>
 /// <returns>generated html</returns>
 public string GetHtml(HtmlGenerationStyle styleGen = HtmlGenerationStyle.Inline)
 {
     return(_htmlContainerInt.GetHtml(styleGen));
 }
Ejemplo n.º 10
0
 /// <summary>
 /// Get html from the current DOM tree with style if requested.
 /// </summary>
 /// <param name="styleGen">Optional: controls the way styles are generated when html is generated (default: <see cref="HtmlGenerationStyle.Inline"/>)</param>
 /// <returns>generated html</returns>
 public string GetHtml(HtmlGenerationStyle styleGen = HtmlGenerationStyle.Inline)
 {
     return DomUtils.GenerateHtml(_root, styleGen);
 }
Ejemplo n.º 11
0
 /// <summary>
 /// Get html from the current DOM tree with style if requested.
 /// </summary>
 /// <param name="styleGen">Optional: controls the way styles are generated when html is generated (default: <see cref="HtmlGenerationStyle.Inline"/>)</param>
 /// <returns>generated html</returns>
 public string GetHtml(HtmlGenerationStyle styleGen = HtmlGenerationStyle.Inline)
 {
     return(DomUtils.GenerateHtml(this._root, styleGen));
 }
Ejemplo n.º 12
0
        /// <summary>
        /// Write the given html tag with all its attributes and styles.
        /// </summary>
        /// <param name="sb">the string builder to write html into</param>
        /// <param name="box">the css box with the html tag to write</param>
        /// <param name="indent">the indent to use for nice formating</param>
        /// <param name="styleGen">Controls the way styles are generated when html is generated</param>
        private static void WriteHtmlTag(StringBuilder sb, CssBox box, int indent, HtmlGenerationStyle styleGen)
        {
            sb.Append(new string(' ', indent * 4));
            sb.AppendFormat("<{0}", box.HtmlTag.Name);

            // collect all element style properties incliding from stylesheet
            var tagStyles   = new Dictionary <string, string>();
            var tagCssBlock = box.HtmlContainer.CssData.GetCssBlock(box.HtmlTag.Name);

            if (tagCssBlock != null)
            {
                // atodo: handle selectors
                foreach (var cssBlock in tagCssBlock)
                {
                    foreach (var prop in cssBlock.Properties)
                    {
                        tagStyles[prop.Key] = prop.Value;
                    }
                }
            }

            if (box.HtmlTag.HasAttributes())
            {
                sb.Append(" ");
                foreach (var att in box.HtmlTag.Attributes)
                {
                    // handle image tags by inserting the image using base64 data
                    if (box.HtmlTag.Name == "img" && att.Key == "src" && (att.Value.StartsWith("property") || att.Value.StartsWith("method")))
                    {
                        var img = ((CssBoxImage)box).Image;
                        if (img != null)
                        {
                            using (var buffer = new MemoryStream())
                            {
                                img.Save(buffer, ImageFormat.Png);
                                var base64 = Convert.ToBase64String(buffer.ToArray());
                                sb.AppendFormat("{0}=\"data:image/png;base64, {1}\" ", att.Key, base64);
                            }
                        }
                    }
                    else if (styleGen == HtmlGenerationStyle.Inline && att.Key == HtmlConstants.Style)
                    {
                        // if inline style add the styles to the collection
                        var block = CssParser.ParseCssBlock(box.HtmlTag.Name, box.HtmlTag.TryGetAttribute("style"));
                        foreach (var prop in block.Properties)
                        {
                            tagStyles[prop.Key] = prop.Value;
                        }
                    }
                    else if (styleGen == HtmlGenerationStyle.Inline && att.Key == HtmlConstants.Class)
                    {
                        // if inline style convert the style class to actual properties and add to collection
                        var cssBlocks = box.HtmlContainer.CssData.GetCssBlock("." + att.Value);
                        if (cssBlocks != null)
                        {
                            // atodo: handle selectors
                            foreach (var cssBlock in cssBlocks)
                            {
                                foreach (var prop in cssBlock.Properties)
                                {
                                    tagStyles[prop.Key] = prop.Value;
                                }
                            }
                        }
                    }
                    else
                    {
                        sb.AppendFormat("{0}=\"{1}\" ", att.Key, att.Value);
                    }
                }

                sb.Remove(sb.Length - 1, 1);
            }

            // if inline style insert the style tag with all collected style properties
            if (styleGen == HtmlGenerationStyle.Inline && tagStyles.Count > 0)
            {
                sb.Append(" style=\"");
                foreach (var style in tagStyles)
                {
                    sb.AppendFormat("{0}: {1}; ", style.Key, style.Value);
                }
                sb.Remove(sb.Length - 1, 1);
                sb.Append("\"");
            }

            sb.AppendFormat("{0}>", box.HtmlTag.IsSingle ? "/" : "");
            sb.AppendLine();
        }
Ejemplo n.º 13
0
        /// <summary>
        /// Write the given html tag with all its attributes and styles.
        /// </summary>
        /// <param name="sb">the string builder to write html into</param>
        /// <param name="box">the css box with the html tag to write</param>
        /// <param name="indent">the indent to use for nice formating</param>
        /// <param name="styleGen">Controls the way styles are generated when html is generated</param>
        private static void WriteHtmlTag(StringBuilder sb, CssBox box, int indent, HtmlGenerationStyle styleGen)
        {
            sb.Append(new string(' ', indent * 4));
            sb.AppendFormat("<{0}", box.HtmlTag.Name);

            // collect all element style properties incliding from stylesheet
            var tagStyles = new Dictionary<string, string>();
            var tagCssBlock = box.HtmlContainer.CssData.GetCssBlock(box.HtmlTag.Name);
            if (tagCssBlock != null)
            {
                // atodo: handle selectors
                foreach (var cssBlock in tagCssBlock)
                    foreach (var prop in cssBlock.Properties)
                        tagStyles[prop.Key] = prop.Value;
            }

            if (box.HtmlTag.Attributes.Count > 0)
            {
                sb.Append(" ");
                foreach (var att in box.HtmlTag.Attributes)
                {
                    // handle image tags by inserting the image using base64 data
                    if (box.HtmlTag.Name == "img" && att.Key == "src" && (att.Value.StartsWith("property") || att.Value.StartsWith("method")))
                    {
                        var img = CssValueParser.GetImage(att.Value, box.HtmlContainer.Bridge);
                        if (img != null)
                        {
                            using (var buffer = new MemoryStream())
                            {
                                img.Save(buffer, ImageFormat.Png);
                                var base64 = Convert.ToBase64String(buffer.ToArray());
                                sb.AppendFormat("{0}=\"data:image/png;base64, {1}\" ", att.Key, base64);
                            }
                        }
                    }
                    else if (styleGen == HtmlGenerationStyle.Inline && att.Key == HtmlConstants.Style)
                    {
                        // if inline style add the styles to the collection
                        var block = CssParser.ParseCssBlockImp(box.HtmlTag.Name, box.HtmlTag.Attributes["style"]);
                        foreach (var prop in block.Properties)
                            tagStyles[prop.Key] = prop.Value;
                    }
                    else if (styleGen == HtmlGenerationStyle.Inline && att.Key == HtmlConstants.Class)
                    {
                        // if inline style convert the style class to actual properties and add to collection
                        var cssBlocks = box.HtmlContainer.CssData.GetCssBlock("." + att.Value);
                        if (cssBlocks != null)
                        {
                            // atodo: handle selectors
                            foreach (var cssBlock in cssBlocks)
                                foreach (var prop in cssBlock.Properties)
                                    tagStyles[prop.Key] = prop.Value;
                        }
                    }
                    else
                    {
                        sb.AppendFormat("{0}=\"{1}\" ", att.Key, att.Value);
                    }
                }

                sb.Remove(sb.Length - 1, 1);
            }

            // if inline style insert the style tag with all collected style properties
            if (styleGen == HtmlGenerationStyle.Inline && tagStyles.Count > 0)
            {
                sb.Append(" style=\"");
                foreach (var style in tagStyles)
                {
                    sb.AppendFormat("{0}: {1}; ", style.Key, style.Value);
                }
                sb.Remove(sb.Length - 1, 1);
                sb.Append("\"");
            }

            sb.AppendFormat("{0}>", box.HtmlTag.IsSingle ? "/" : "");
            sb.AppendLine();
        }
Ejemplo n.º 14
0
 public override string GetOuterHtml(HtmlGenerationStyle generationStyle = HtmlGenerationStyle.None)
 {
     return(string.Format("<!--{0}-->", innerText));
 }
Ejemplo n.º 15
0
        /// <summary>
        /// Write the given html dom subtree into the given string builder.
        /// </summary>
        /// <param name="sb">the string builder to write html into</param>
        /// <param name="box">the html sub-tree to write</param>
        /// <param name="indent">the indent to use for nice formating</param>
        /// <param name="styleGen">Controls the way styles are generated when html is generated</param>
        /// <param name="selectedTags">Control if to generate only selected tags, if given only tags found in collection will be generated</param>
        private static void WriteHtml(StringBuilder sb, CssBox box, int indent, HtmlGenerationStyle styleGen, ICollection<HtmlTag> selectedTags)
        {
            if (box.HtmlTag != null && selectedTags != null && !selectedTags.Contains(box.HtmlTag))
                return;

            if (box.HtmlTag != null)
            {
                if (box.HtmlTag.Name != "link" || !box.HtmlTag.Attributes.ContainsKey("href") ||
                    (!box.HtmlTag.Attributes["href"].StartsWith("property") && !box.HtmlTag.Attributes["href"].StartsWith("method")))
                {
                    WriteHtmlTag(sb, box, indent, styleGen);
                    indent = indent + (box.HtmlTag.IsSingle ? 0 : 1);
                }

                if (styleGen == HtmlGenerationStyle.InHeader && box.HtmlTag.Name == "html" && box.HtmlContainer.CssData != null)
                {
                    sb.Append(new string(' ', indent * 4));
                    sb.AppendLine("<head>");
                    WriteStylesheet(sb, box.HtmlContainer.CssData, indent + 1);
                    sb.Append(new string(' ', indent * 4));
                    sb.AppendLine("</head>");
                }
            }

            if (box.Words.Count > 0)
            {
                sb.Append(new string(' ', indent * 4));
                foreach (var word in box.Words)
                {
                    if (selectedTags == null || word.Selected)
                    {
                        var wordText = GetSelectedWord(word, selectedTags != null);
                        sb.Append(HtmlUtils.EncodeHtml(wordText));
                    }
                }
                sb.AppendLine();
            }

            foreach (var childBox in box.Boxes)
            {
                WriteHtml(sb, childBox, indent, styleGen, selectedTags);
            }

            if (box.HtmlTag != null && !box.HtmlTag.IsSingle)
            {
                sb.Append(new string(' ', Math.Max((indent - 1) * 4, 0)));
                sb.AppendFormat("</{0}>", box.HtmlTag.Name);
                sb.AppendLine();
            }
        }
Ejemplo n.º 16
0
        /// <summary>
        /// Write the given html tag with all its attributes and styles.
        /// </summary>
        /// <param name="cssParser">used to parse CSS data</param>
        /// <param name="sb">the string builder to write html into</param>
        /// <param name="box">the css box with the html tag to write</param>
        /// <param name="styleGen">Controls the way styles are generated when html is generated</param>
        private static void WriteHtmlTag(CssParser cssParser, StringBuilder sb, CssBox box, HtmlGenerationStyle styleGen)
        {
            sb.AppendFormat("<{0}", box.HtmlTag.Name);

            // collect all element style properties including from stylesheet
            var tagStyles = new Dictionary<string, string>();
            var tagCssBlock = box.HtmlContainer.CssData.GetCssBlock(box.HtmlTag.Name);
            if (tagCssBlock != null)
            {
                // TODO:a handle selectors
                foreach (var cssBlock in tagCssBlock)
                    foreach (var prop in cssBlock.Properties)
                        tagStyles[prop.Key] = prop.Value;
            }

            if (box.HtmlTag.HasAttributes())
            {
                sb.Append(" ");
                foreach (var att in box.HtmlTag.Attributes)
                {
                    // handle image tags by inserting the image using base64 data
                    if (styleGen == HtmlGenerationStyle.Inline && att.Key == HtmlConstants.Style)
                    {
                        // if inline style add the styles to the collection
                        var block = cssParser.ParseCssBlock(box.HtmlTag.Name, box.HtmlTag.TryGetAttribute("style"));
                        foreach (var prop in block.Properties)
                            tagStyles[prop.Key] = prop.Value;
                    }
                    else if (styleGen == HtmlGenerationStyle.Inline && att.Key == HtmlConstants.Class)
                    {
                        // if inline style convert the style class to actual properties and add to collection
                        var cssBlocks = box.HtmlContainer.CssData.GetCssBlock("." + att.Value);
                        if (cssBlocks != null)
                        {
                            // TODO:a handle selectors
                            foreach (var cssBlock in cssBlocks)
                                foreach (var prop in cssBlock.Properties)
                                    tagStyles[prop.Key] = prop.Value;
                        }
                    }
                    else
                    {
                        sb.AppendFormat("{0}=\"{1}\" ", att.Key, att.Value);
                    }
                }

                sb.Remove(sb.Length - 1, 1);
            }

            // if inline style insert the style tag with all collected style properties
            if (styleGen == HtmlGenerationStyle.Inline && tagStyles.Count > 0)
            {
                var cleanTagStyles = StripDefaultStyles(box, tagStyles);
                if (cleanTagStyles.Count > 0)
                {
                    sb.Append(" style=\"");
                    foreach (var style in cleanTagStyles)
                        sb.AppendFormat("{0}: {1}; ", style.Key, style.Value);
                    sb.Remove(sb.Length - 1, 1);
                    sb.Append("\"");
                }
            }

            sb.AppendFormat("{0}>", box.HtmlTag.IsSingle ? "/" : "");
        }
Ejemplo n.º 17
0
        /// <summary>
        /// Write the given html DOM sub-tree into the given string builder.<br/>
        /// If <paramref name="selectedBoxes"/> are given write html only from those tags.
        /// </summary>
        /// <param name="cssParser">used to parse CSS data</param>
        /// <param name="sb">the string builder to write html into</param>
        /// <param name="box">the html sub-tree to write</param>
        /// <param name="styleGen">Controls the way styles are generated when html is generated</param>
        /// <param name="selectedBoxes">Control if to generate only selected boxes, if given only boxes found in hash will be generated</param>
        /// <param name="selectionRoot">the box the is the root of selected boxes (the first box to contain multiple selected boxes)</param>
        private static void WriteHtml(CssParser cssParser, StringBuilder sb, CssBox box, HtmlGenerationStyle styleGen, Dictionary<CssBox, bool> selectedBoxes, CssBox selectionRoot)
        {
            if (box.HtmlTag == null || selectedBoxes == null || selectedBoxes.ContainsKey(box))
            {
                if (box.HtmlTag != null)
                {
                    if (box.HtmlTag.Name != "link" || !box.HtmlTag.Attributes.ContainsKey("href") ||
                        (!box.HtmlTag.Attributes["href"].StartsWith("property") && !box.HtmlTag.Attributes["href"].StartsWith("method")))
                    {
                        WriteHtmlTag(cssParser, sb, box, styleGen);
                        if (box == selectionRoot)
                            sb.Append("<!--StartFragment-->");
                    }

                    if (styleGen == HtmlGenerationStyle.InHeader && box.HtmlTag.Name == "html" && box.HtmlContainer.CssData != null)
                    {
                        sb.AppendLine("<head>");
                        WriteStylesheet(sb, box.HtmlContainer.CssData);
                        sb.AppendLine("</head>");
                    }
                }

                if (box.Words.Count > 0)
                {
                    foreach (var word in box.Words)
                    {
                        if (selectedBoxes == null || word.Selected)
                        {
                            var wordText = GetSelectedWord(word, selectedBoxes != null);
                            sb.Append(HtmlUtils.EncodeHtml(wordText));
                        }
                    }
                }

                foreach (var childBox in box.Boxes)
                {
                    WriteHtml(cssParser, sb, childBox, styleGen, selectedBoxes, selectionRoot);
                }

                if (box.HtmlTag != null && !box.HtmlTag.IsSingle)
                {
                    if (box == selectionRoot)
                        sb.Append("<!--EndFragment-->");
                    sb.AppendFormat("</{0}>", box.HtmlTag.Name);
                }
            }
        }
Ejemplo n.º 18
0
 /// <summary>
 /// Generate html from the given DOM tree.<br/>
 /// Generate all the style inside the html, in header or for every tag depending on <paramref name="styleGen"/> value.
 /// </summary>
 /// <param name="root">the box of the html generate html from</param>
 /// <param name="styleGen">Optional: controls the way styles are generated when html is generated</param>
 /// <param name="onlySelected">Optional: true - generate only selected html subset, false - generate all (default - false)</param>
 /// <returns>generated html</returns>
 public static string GenerateHtml(CssBox root, HtmlGenerationStyle styleGen = HtmlGenerationStyle.Inline, bool onlySelected = false)
 {
     var sb = new StringBuilder();
     if (root != null)
     {
         var selectedBoxes = onlySelected ? CollectSelectedBoxes(root) : null;
         var selectionRoot = onlySelected ? GetSelectionRoot(root, selectedBoxes) : null;
         WriteHtml(root.HtmlContainer.CssParser, sb, root, styleGen, selectedBoxes, selectionRoot);
     }
     return sb.ToString();
 }
Ejemplo n.º 19
0
        public virtual string GetOuterHtml(HtmlGenerationStyle generationStyle = HtmlGenerationStyle.None)
        {
            var builder = new StringBuilder();

            var selfClosing = !HasChildren && string.IsNullOrEmpty(innerText);

            if (generationStyle == HtmlGenerationStyle.Indent)
            {
                builder.Append(string.Empty.PadLeft(IndentLevel, '\t'));
            }

            if (!string.IsNullOrEmpty(Name))
            {
                builder.Append('<');
                builder.Append(Name);

                if (HasAttributes)
                {
                    foreach (var key in Attributes.AllKeys)
                    {
                        builder.AppendFormat(" {0}=\"{1}\"", key, Attributes[key]);
                    }
                }

                if (!selfClosing)
                {
                    builder.Append('>');
                }
                else
                {
                    builder.Append(" />");
                }
            }

            if (!selfClosing)
            {
                if (HasChildren)
                {
                    if (generationStyle == HtmlGenerationStyle.Indent)
                    {
                        builder.AppendLine();
                    }
                    foreach (var child in Children)
                    {
                        child.IndentLevel = IndentLevel + 1;
                        builder.Append(child.GetOuterHtml(generationStyle));
                    }

                    if (generationStyle == HtmlGenerationStyle.Indent)
                    {
                        builder.Append(string.Empty.PadLeft(IndentLevel, '\t'));
                    }
                }

                if (string.IsNullOrEmpty(innerText))
                {
                    builder.AppendFormat("</{0}>", Name);
                }
            }

            if (!string.IsNullOrEmpty(innerText))
            {
                builder.Append(innerText);
                if (!selfClosing && !string.IsNullOrEmpty(Name))
                {
                    builder.AppendFormat("</{0}>", Name);
                }
            }

            if (generationStyle == HtmlGenerationStyle.Indent)
            {
                builder.AppendLine();
            }

            return(builder.ToString());
        }