private void FeedStyleBlock(string media, string block)
        {
            if (string.IsNullOrEmpty(media))
            {
                media = "all";
            }
            int    num = block.IndexOf("{");
            string str = block.Substring(num).Replace("{", string.Empty).Replace("}", string.Empty);

            if (num < 0)
            {
                return;
            }
            string[] strArrays = block.Substring(0, num).Split(new char[] { ',' });
            for (int i = 0; i < (int)strArrays.Length; i++)
            {
                string str1 = strArrays[i].Trim();
                if (!string.IsNullOrEmpty(str1))
                {
                    CssBlock cssBlock = new CssBlock(str);
                    if (!this.MediaBlocks.ContainsKey(media))
                    {
                        this.MediaBlocks.Add(media, new Dictionary <string, CssBlock>());
                    }
                    if (this.MediaBlocks[media].ContainsKey(str1))
                    {
                        CssBlock item = this.MediaBlocks[media][str1];
                        foreach (string key in cssBlock.Properties.Keys)
                        {
                            if (!item.Properties.ContainsKey(key))
                            {
                                item.Properties.Add(key, cssBlock.Properties[key]);
                            }
                            else
                            {
                                item.Properties[key] = cssBlock.Properties[key];
                            }
                        }
                        item.UpdatePropertyValues();
                    }
                    else
                    {
                        this.MediaBlocks[media].Add(str1, cssBlock);
                    }
                }
            }
        }
        private void CascadeStyles(CssBox startBox)
        {
            bool flag = false;

            foreach (CssBox box in startBox.Boxes)
            {
                box.InheritStyle();
                if (box.HtmlTag != null)
                {
                    if (this.MediaBlocks["all"].ContainsKey(box.HtmlTag.TagName))
                    {
                        this.MediaBlocks["all"][box.HtmlTag.TagName].AssignTo(box);
                    }
                    if (box.HtmlTag.HasAttribute("class") && this.MediaBlocks["all"].ContainsKey(string.Concat(".", box.HtmlTag.Attributes["class"])))
                    {
                        this.MediaBlocks["all"][string.Concat(".", box.HtmlTag.Attributes["class"])].AssignTo(box);
                    }
                    box.HtmlTag.TranslateAttributes(box);
                    if (box.HtmlTag.HasAttribute("style"))
                    {
                        CssBlock cssBlock = new CssBlock(box.HtmlTag.Attributes["style"]);
                        cssBlock.AssignTo(box);
                    }
                    if (box.HtmlTag.TagName.Equals("style", StringComparison.CurrentCultureIgnoreCase) && box.Boxes.Count == 1)
                    {
                        this.FeedStyleSheet(box.Boxes[0].Text);
                    }
                    if (box.HtmlTag.TagName.Equals("link", StringComparison.CurrentCultureIgnoreCase) && box.GetAttribute("rel", string.Empty).Equals("stylesheet", StringComparison.CurrentCultureIgnoreCase))
                    {
                        this.FeedStyleSheet(CssValue.GetStyleSheet(box.GetAttribute("href", string.Empty)));
                    }
                }
                this.CascadeStyles(box);
            }
            if (flag)
            {
                foreach (CssBox cssBox in startBox.Boxes)
                {
                    cssBox.Display = "block";
                }
            }
        }
        /// <summary>
        /// Feeds the style with a block about the specific media.
        /// When no media is specified, "all" will be used
        /// </summary>
        /// <param name="media"></param>
        /// <param name="block"></param>
        private void FeedStyleBlock(string media, string block)
        {
            if (string.IsNullOrEmpty(media))
            {
                media = "all";
            }

            int    bracketIndex = block.IndexOf("{");
            string blockSource  = block.Substring(bracketIndex).Replace("{", string.Empty).Replace("}", string.Empty);

            if (bracketIndex < 0)
            {
                return;
            }

            ///TODO: Only supporting definitions like:
            /// h1, h2, h3 {...
            ///Support needed for definitions like:
            ///* {...
            ///h1 h2 {...
            ///h1 > h2 {...
            ///h1:before {...
            ///h1:hover {...
            string[] classes = block.Substring(0, bracketIndex).Split(',');

            for (int i = 0; i < classes.Length; i++)
            {
                string className = classes[i].Trim(); if (string.IsNullOrEmpty(className))
                {
                    continue;
                }

                CssBlock newblock = new CssBlock(blockSource);

                //Create media blocks if necessary
                if (!MediaBlocks.ContainsKey(media))
                {
                    MediaBlocks.Add(media, new Dictionary <string, CssBlock>());
                }

                if (!MediaBlocks[media].ContainsKey(className))
                {
                    //Create block
                    MediaBlocks[media].Add(className, newblock);
                }
                else
                {
                    //Merge newblock and oldblock's properties

                    CssBlock oldblock = MediaBlocks[media][className];

                    foreach (string property in newblock.Properties.Keys)
                    {
                        if (oldblock.Properties.ContainsKey(property))
                        {
                            oldblock.Properties[property] = newblock.Properties[property];
                        }
                        else
                        {
                            oldblock.Properties.Add(property, newblock.Properties[property]);
                        }
                    }

                    oldblock.UpdatePropertyValues();
                }
            }
        }
Example #4
0
        /// <summary>
        /// Feeds the style with a block about the specific media.
        /// When no media is specified, "all" will be used
        /// </summary>
        /// <param name="media"></param>
        /// <param name="block"></param>
        private void FeedStyleBlock(string media, string block)
        {
            if (string.IsNullOrEmpty(media)) media = "all";

            int bracketIndex = block.IndexOf("{");
            string blockSource = block.Substring(bracketIndex).Replace("{", string.Empty).Replace("}", string.Empty);

            if (bracketIndex < 0) return;

            ///TODO: Only supporting definitions like:
            /// h1, h2, h3 {...
            ///Support needed for definitions like:
            ///* {...
            ///h1 h2 {...
            ///h1 > h2 {...
            ///h1:before {...
            ///h1:hover {...
            string[] classes = block.Substring(0, bracketIndex).Split(',');

            for (int i = 0; i < classes.Length; i++)
            {
                string className = classes[i].Trim(); if(string.IsNullOrEmpty(className)) continue;

                CssBlock newblock = new CssBlock(blockSource);

                //Create media blocks if necessary
                if (!MediaBlocks.ContainsKey(media)) MediaBlocks.Add(media, new Dictionary<string, CssBlock>());

                if (!MediaBlocks[media].ContainsKey(className))
                {
                    //Create block
                    MediaBlocks[media].Add(className, newblock);
                }
                else
                {
                    //Merge newblock and oldblock's properties

                    CssBlock oldblock = MediaBlocks[media][className];

                    foreach (string property in newblock.Properties.Keys)
                    {
                        if (oldblock.Properties.ContainsKey(property))
                        {
                            oldblock.Properties[property] = newblock.Properties[property];
                        }
                        else
                        {
                            oldblock.Properties.Add(property, newblock.Properties[property]);
                        }
                    }

                    oldblock.UpdatePropertyValues();
                }
            }
        }
Example #5
0
        /// <summary>
        /// Applies style to all boxes in the tree
        /// </summary>
        private void CascadeStyles(CssBox startBox)
        {
            bool someBlock = false;

            foreach (CssBox b in startBox.Boxes)
            {
                b.InheritStyle();

                if (b.HtmlTag != null)
                {
                    //Check if tag name matches with a defined class
                    if (MediaBlocks["all"].ContainsKey(b.HtmlTag.TagName))
                    {
                        MediaBlocks["all"][b.HtmlTag.TagName].AssignTo(b);
                    }

                    //Check if class="" attribute matches with a defined style
                    if (b.HtmlTag.HasAttribute("class") &&
                        MediaBlocks["all"].ContainsKey("." + b.HtmlTag.Attributes["class"]))
                    {
                        MediaBlocks["all"]["." + b.HtmlTag.Attributes["class"]].AssignTo(b);
                    }

                    b.HtmlTag.TranslateAttributes(b);

                    //Check for the style="" attribute
                    if (b.HtmlTag.HasAttribute("style"))
                    {
                        CssBlock block = new CssBlock(b.HtmlTag.Attributes["style"]);
                        block.AssignTo(b);
                    }

                    //Check for the <style> tag
                    if (b.HtmlTag.TagName.Equals("style", StringComparison.CurrentCultureIgnoreCase) &&
                        b.Boxes.Count == 1)
                    {
                        FeedStyleSheet(b.Boxes[0].Text);
                    }

                    //Check for the <link rel=stylesheet> tag
                    if (b.HtmlTag.TagName.Equals("link", StringComparison.CurrentCultureIgnoreCase) &&
                        b.GetAttribute("rel", string.Empty).Equals("stylesheet", StringComparison.CurrentCultureIgnoreCase))
                    {
                        FeedStyleSheet(CssValue.GetStyleSheet(b.GetAttribute("href", string.Empty)));
                    }
                }

                CascadeStyles(b);
            }

            if (someBlock)
            {
                foreach (CssBox box in startBox.Boxes)
                {
                    box.Display = CssConstants.Block;
                }
            }
        }