Beispiel #1
0
    /// <summary>
    /// Prepare Dictionary with requests for CSS validation
    /// </summary>
    /// <param name="parameter">Asynchronous parameter containing current url data to resolve absolute URL </param>
    private Dictionary <string, string> GetValidationRequests(string parameter)
    {
        string html = GetHtml(Url);
        Dictionary <string, string> cssRequests = null;

        string[] urlParams = parameter.Split(';');

        if (!String.IsNullOrEmpty(html))
        {
            cssRequests = new Dictionary <string, string>();

            // Get inline CSS
            AddLog(GetString("validation.css.preparinginline"));
            StringBuilder sbInline = new StringBuilder();
            foreach (Match m in InlineStylesRegex.Matches(html))
            {
                string captured = m.Groups["css"].Value;
                sbInline.AppendLine(captured);
            }

            cssRequests.Add(DocumentValidationHelper.InlineCSSSource, sbInline.ToString());

            // Get linked styles URLs
            foreach (Match m in LinkedStylesRegex.Matches(html))
            {
                string url = m.Groups["url"].Value;
                url = Server.HtmlDecode(url);

                if (!String.IsNullOrEmpty(url))
                {
                    bool     processCss   = true;
                    string[] excludedCsss = EXCLUDED_CSS.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);

                    // Check if CSS is not excluded (CMS stylesheets)
                    foreach (string excludedCss in excludedCsss)
                    {
                        if (url.EndsWithCSafe(excludedCss, true))
                        {
                            processCss = false;
                            break;
                        }
                    }

                    if (processCss && !cssRequests.ContainsKey(url))
                    {
                        AddLog(String.Format(GetString("validation.css.preparinglinkedstyles"), url));

                        try
                        {
                            // Get CSS data from URL
                            string readUrl = DocumentValidationHelper.DisableMinificationOnUrl(URLHelper.GetAbsoluteUrl(url, urlParams[0], urlParams[1], urlParams[2]));

                            var request = WebRequest.CreateHttp(readUrl);
                            EnsureCertificateValidation(request);

                            using (var stream = request.GetResponse().GetResponseStream())
                                using (var reader = StreamReader.New(stream))
                                {
                                    string css = reader.ReadToEnd();
                                    if (!String.IsNullOrEmpty(css))
                                    {
                                        cssRequests.Add(url, css.Trim(new[] { '\r', '\n' }));
                                    }
                                }
                        }
                        catch (Exception ex)
                        {
                            Service.Resolve <IEventLogService>().LogException("CSSValidator", "GetValidationRequests", ex);
                        }
                    }
                }
            }
        }

        return(cssRequests);
    }
    /// <summary>
    /// Prepare Dictionary with requests for CSS validation
    /// </summary>
    /// <param name="parameter">Asynchronous parameter containing current url data to resolve absolute URL </param>
    private Dictionary <string, string> GetValidationRequests(string parameter)
    {
        string html = GetHtml(Url);
        Dictionary <string, string> cssRequests = null;

        string[] urlParams = parameter.Split(';');

        if (!String.IsNullOrEmpty(html))
        {
            cssRequests = new Dictionary <string, string>();

            // Get inline CSS
            AddLog(GetString("validation.css.preparinginline"));
            StringBuilder sbInline = new StringBuilder();
            foreach (Match m in InlineStylesRegex.Matches(html))
            {
                string captured = m.Groups["css"].Value;
                sbInline.Append(captured);
                sbInline.Append("\n");
            }

            cssRequests.Add(DocumentValidationHelper.InlineCSSSource, sbInline.ToString());

            // Get linked styles URLs
            WebClient client = new WebClient();
            foreach (Match m in LinkedStylesRegex.Matches(html))
            {
                string url = m.Groups["url"].Value;
                url = Server.HtmlDecode(url);

                string css = null;
                if (!String.IsNullOrEmpty(url))
                {
                    bool     processCss   = true;
                    string[] excludedCsss = mExcludedCSS.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);

                    // Check if CSS is not excluded (CMS stylesheets)
                    foreach (string excludedCss in excludedCsss)
                    {
                        if (url.EndsWithCSafe(excludedCss, true))
                        {
                            processCss = false;
                            break;
                        }
                    }

                    if (processCss && !cssRequests.ContainsKey(url))
                    {
                        AddLog(String.Format(GetString("validation.css.preparinglinkedstyles"), url));

                        try
                        {
                            // Get CSS data from URL
                            string readUrl = DocumentValidationHelper.DisableMinificationOnUrl(URLHelper.GetAbsoluteUrl(url, urlParams[0], urlParams[1], urlParams[2]));

                            StreamReader reader = StreamReader.New(client.OpenRead(readUrl));
                            css = reader.ReadToEnd();
                            if (!String.IsNullOrEmpty(css))
                            {
                                cssRequests.Add(url, css.Trim(new char[] { '\r', '\n' }));
                            }
                        }
                        catch
                        {
                        }
                    }
                }
            }
        }

        return(cssRequests);
    }