コード例 #1
0
        public void Parse()
        {
            CssParser parser = new CssParser(_css);
            OnDocumentBegin();
            while (true)
            {
                StyleElement element = parser.Next();

                if (element == null)
                {
                    OnDocumentEnd();
                    return;
                }

                StyleText styleText = element as StyleText;
                if (styleText != null)
                    OnStyleText(styleText);

                StyleLiteral styleLiteral = element as StyleLiteral;
                if (styleLiteral != null)
                    OnStyleLiteral(styleLiteral);

                StyleUrl styleUrl = element as StyleUrl;
                if (styleUrl != null)
                    OnStyleUrl(styleUrl);

                StyleImport styleImport = element as StyleImport;
                if (styleImport != null)
                    OnStyleImport(styleImport);

                StyleComment styleComment = element as StyleComment;
                if (styleComment != null)
                    OnStyleComment(styleComment);
            }
        }
コード例 #2
0
 private string LowerCaseCss(string val)
 {
     StringBuilder output = new StringBuilder();
     CssParser parser = new CssParser(val);
     for (StyleElement el; null != (el = parser.Next());)
     {
         if (el is StyleText)
             output.Append(el.RawText.ToLower(CultureInfo.InvariantCulture));
         else
             output.Append(el.RawText);
     }
     return output.ToString();
 }
コード例 #3
0
        /// <summary>
        /// Returns the .NET color that matches an element's background color.
        /// </summary>
        /// <param name="element"></param>
        /// <param name="detectImage">
        /// Takes background-image into account when looking for background color.
        /// This is positionally sensitive so if you're not sure if your elements
        /// are in the "correct" positions relative to each other (such as in
        /// Web Layout view) you may want to set this to false.
        /// </param>
        /// <param name="pageUrl">The URL that should be used to escape relative background image paths. Can be null.</param>
        /// <param name="defaultColor"></param>
        /// <returns></returns>
        public static Color GetBackgroundColor(IHTMLElement element, bool detectImage, string pageUrl, Color defaultColor)
        {
            Rectangle childBounds = new Rectangle(HTMLElementHelper.CalculateOffset(element), new Size(element.offsetWidth, element.offsetHeight));

            while (element != null)
            {
                IHTMLCurrentStyle style = ((IHTMLElement2)element).currentStyle;
                string colorStr = style.backgroundColor as string;
                if (!string.IsNullOrEmpty(colorStr) && colorStr != "transparent")
                {
                    if (colorStr == "inherit")
                        detectImage = false;
                    else
                        return GetColorFromHexColor(ParseColorToHex(colorStr), defaultColor);
                }

                string imageUrl = style.backgroundImage;
                if (detectImage
                    && !string.IsNullOrEmpty(imageUrl)
                    && imageUrl != "none"
                    && (style.backgroundRepeat == "repeat" || style.backgroundRepeat == "repeat-y"))
                {
                    StyleUrl styleUrl = new CssParser(imageUrl.Trim()).Next() as StyleUrl;
                    Trace.Assert(styleUrl != null, "Style URL could not be parsed");
                    if (styleUrl != null)
                    {
                        // If there's a background image URL...
                        string url = styleUrl.LiteralText;
                        using (Stream imageStream = GetStreamForUrl(url, pageUrl, element))
                        {
                            if (imageStream != null)
                            {
                                // ...and we were able to open/download it...
                                using (Image image = Image.FromStream(imageStream))
                                {
                                    if (image is Bitmap)
                                    {
                                        // ...and it's a Bitmap, then use it to get the color.
                                        Rectangle containerBounds = new Rectangle(HTMLElementHelper.CalculateOffset(element), new Size(element.offsetWidth, element.offsetHeight));
                                        Color color = GetColorFromBackgroundImage((Bitmap)image,
                                                                        childBounds,
                                                                        containerBounds,
                                                                        style);
                                        // We can't use semi-transparent backgrounds, keep looking
                                        if (color.A == 255)
                                            return color;
                                    }
                                }
                            }
                        }
                    }
                }

                element = element.parentElement;
            }

            return defaultColor;
        }