private void InterpretStyles(HtmlNode htmlNode)
        {
            string style;

            if (!HtmlStyle.IsNonStyleElement(htmlNode.Tag))
            {
                if (htmlNode.Attributes.TryGetValue("style", out style))
                {
                    CSSParser cssParser = new CSSParser();
                    htmlNode.AddStyles(cssParser.ParseRules(style, SelectorType.Inline));
                }

                Parse(htmlNode);
            }

            foreach (HtmlNode node in htmlNode.GetChildren())
            {
                InterpretStyles(node);
            }

            //This loop only needs when the parent is null. If parent is not null, it will loop through all the 
            //child elements thus next nodes processed without this loop.
            if (htmlNode.Parent == null && htmlNode.Next != null)
            {
                InterpretStyles(htmlNode.GetNext());
            }
        }
        internal bool Process(string selectorText, string styleText, List<MediaQuery> mediaQuries, ref int position)
        {
            CSSParser cssParser = new CSSParser();
            List<CSSElement> elements = new List<CSSElement>();
            Match match = mediaRegex.Match(selectorText);

            if (string.IsNullOrEmpty(styleText))
            {
                return false;
            }

            if(!match.Success)
            {
                return false;
            }

            int closeBraceIndex = 0;
            string style = CSSTokenizer.FindOpenCloseBraceArea(styleText, position + 1, out closeBraceIndex);

            if (closeBraceIndex > position)
            {
                if (!string.IsNullOrEmpty(style))
                {
                    cssParser.ParseCSS(style, elements, mediaQuries);
                   
                    selectorText = selectorText.Substring(match.Length + 1);
                    mediaQuries.Add(new MediaQuery(selectorText, elements));
                }

                position = closeBraceIndex + 1;
            }

            return match.Success;
        }
        private void TraverseHtmlNodes(HtmlNode node, StyleSheet styleSheet)
        {
            string style = string.Empty;

            if (string.Compare(node.Tag, HtmlTag.STYLE, StringComparison.InvariantCultureIgnoreCase) == 0)
            {
                style = node.InnerHtml == null ? string.Empty : node.InnerHtml.Trim();
            }
            else if (string.Compare(node.Tag, HtmlTag.LINK, StringComparison.InvariantCultureIgnoreCase) == 0)
            {
                string relValue = node.ExtractAttributeValue("rel");
                string media = node.ExtractAttributeValue("media");

                if (string.Compare(rel, relValue, StringComparison.InvariantCultureIgnoreCase) == 0 &&
                    (string.IsNullOrEmpty(media) || media.CompareInvariantCultureIgnoreCase("screen")
                    || media.CompareInvariantCultureIgnoreCase("all")))
                {
                    string url = node.ExtractAttributeValue("href");

                    if (!string.IsNullOrEmpty(url))
                    {
                        WebManager web = new WebManager(uriSchema, baseUrl);
                        style = web.ExtractStylesFromLink(url);
                    }
                }
            }

            if (!string.IsNullOrEmpty(style))
            {
                List<CSSElement> styles = new List<CSSElement>();
                List<MediaQuery> mediaQueries = new List<MediaQuery>();

                CSSParser parser = new CSSParser();
                parser.ParseCSS(style, styles, mediaQueries);

                styleSheet.AddRange(styles);
                styleSheet.AddMediaQueryRange(mediaQueries);
            }

            foreach (HtmlNode n in node.GetChildren())
            {
                TraverseHtmlNodes(n, styleSheet);
            }
        }