Beispiel #1
0
        /// <summary>
        /// Generate css tree by parsing the given html and applying the given css style data on it.
        /// </summary>
        /// <param name="html">the html to parse</param>
        /// <param name="htmlContainer">the html container to use for reference resolve</param>
        /// <param name="cssData">the css data to use</param>
        /// <returns>the root of the generated tree</returns>
        public CssBox GenerateCssTree(string html, HtmlContainerInt htmlContainer, CssDataWithChanged cssData)
        {
            var root = HtmlParser.ParseDocument(html);

            if (root != null)
            {
                root.HtmlContainer = htmlContainer;

                CascadeParseStyles(root, htmlContainer, cssData);

                CascadeApplyStyles(root, cssData.cssData);

                SetTextSelectionStyle(htmlContainer, cssData.cssData);

                CorrectTextBoxes(root);

                CorrectImgBoxes(root);

                bool followingBlock = true;
                CorrectLineBreaksBlocks(root, ref followingBlock);

                CorrectInlineBoxesParent(root);

                CorrectBlockInsideInline(root);

                CorrectInlineBoxesParent(root);
            }
            return(root);
        }
Beispiel #2
0
        /// <summary>
        /// Read styles defined inside the dom structure in links and style elements.<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">the box to parse style data in</param>
        /// <param name="htmlContainer">the html container to use for reference resolve</param>
        /// <param name="cssData">the style data to fill with found styles</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 void CascadeParseStyles(CssBox box, HtmlContainerInt htmlContainer, CssDataWithChanged cssData)
        {
            if (box.HtmlTag != null)
            {
                // Check for the <link rel=stylesheet> tag
                if (box.HtmlTag.Name.Equals("link", StringComparison.CurrentCultureIgnoreCase) &&
                    box.GetAttribute("rel", string.Empty).Equals("stylesheet", StringComparison.CurrentCultureIgnoreCase))
                {
                    cssData.CloneCssData();

                    var stylesheetData = htmlContainer.ResourceServer.GetCssData(box.GetAttribute("href", string.Empty), box.HtmlTag.Attributes);

                    /*
                     * string stylesheet;
                     * CssData stylesheetData;
                     * StylesheetLoadHandler.LoadStylesheet(htmlContainer,
                     *  box.GetAttribute("href", string.Empty),
                     *  box.HtmlTag.Attributes, out stylesheet, out stylesheetData);
                     */
                    /*
                     * if (stylesheet != null)
                     *  _cssParser.ParseStyleSheet(cssData, stylesheet);
                     * else */
                    if (stylesheetData != null)
                    {
                        cssData.cssData.Combine(stylesheetData);
                    }
                }

                // Check for the <style> tag
                if (box.HtmlTag.Name.Equals("style", StringComparison.CurrentCultureIgnoreCase) && box.Boxes.Count > 0)
                {
                    cssData.CloneCssData();
                    foreach (var child in box.Boxes)
                    {
                        _cssParser.ParseStyleSheet(cssData.cssData, child.Text.CutSubstring());
                    }
                }
            }

            foreach (var childBox in box.Boxes)
            {
                CascadeParseStyles(childBox, htmlContainer, cssData);
            }
        }