Exemple #1
0
        private void OnCssError(object sender, CssErrorEventArgs e)
        {
            CssException error = e.Exception;

            if (error.Severity <= WarningLevel)
            {
                // the error code is the lower half of the error number, in decimal, prepended with "JS"
                // again, NOT LOCALIZABLE so the format is not in the resources
                string code = string.Format(
                    CultureInfo.InvariantCulture,
                    "CSS{0}",
                    error.Error & 0xffff);

                // the location is the file name followed by the line and start/end columns within parens.
                // if the file context is empty, use "stdin" as the file name.
                // this string is NOT LOCALIZABLE, so not putting the format in the resources
                string location = string.Format(
                    CultureInfo.InvariantCulture,
                    "{0}({1},{2})",
                    string.IsNullOrEmpty(FileName) ? "MinifyStyleSheet" : FileName,
                    error.Line,
                    error.Char);

                AddErrorMessage(
                    location,           // not localized
                    string.Empty,       // localizable, optional
                    error.Severity < 2, // NOT localized, only two options
                    code,               // not localized, cannot contain spaces
                    error.Message);     // localizable with optional arguments
            }
        }
    /// <summary>
    /// Returns a minified version of a given CSS.
    /// </summary>
    /// <param name="resource">CSS to be minified</param>
    /// <returns>Minified CSS</returns>
    public string Minify(string resource)
    {
        if (String.IsNullOrEmpty(resource))
        {
            return(resource);
        }

        // Reset error
        minificationError = null;

        try
        {
            CssParser parser = new CssParser();

            parser.CssError += parser_CssError;

            // Parse the resource
            string parsed = parser.Parse(resource);
            if (!String.IsNullOrEmpty(parsed) && (minificationError == null))
            {
                resource = parsed;
            }
        }
        catch (CssException ex)
        {
            minificationError = ex;
        }

        if (minificationError != null)
        {
            if (LogMinifierParseError)
            {
                // Log exception to event log if allowed
                EventLogProvider.LogException("CSS Compression", "MINIFYCSS", minificationError);
            }

            // Add error info in front of non-minified resource
            resource += "\r\n\r\n/* Minification failed (line " + minificationError.Line.ToString() + "): " + minificationError.Message + " */";
        }

        return(resource);
    }
Exemple #3
0
        void OnCssError(object sender, CssErrorEventArgs e)
        {
            CssException error = e.Exception;

            // ignore severity values greater than our severity level
            if (error.Severity <= m_warningLevel)
            {
                // we found an error
                m_errorsFound = true;

                // the error code is the lower half of the error number, in decimal, prepended with "JS"
                // again, NOT LOCALIZABLE so the format is not in the resources
                string code = string.Format(
                    CultureInfo.InvariantCulture,
                    "CSS{0}",
                    (error.Error & (0xffff))
                    );

                // the location is the file name followed by the line and start/end columns within parens.
                // if the file context is empty, use "stdin" as the file name.
                // this string is NOT LOCALIZABLE, so not putting the format in the resources
                string context  = ((CssParser)sender).FileContext;
                string location = string.Format(
                    CultureInfo.InvariantCulture,
                    "{0}({1},{2})",
                    (string.IsNullOrEmpty(context) ? "stdin" : context),
                    error.Line,
                    error.Char
                    );

                WriteError(CreateBuildError(
                               location,
                               GetSeverityString(error.Severity),
                               (error.Severity < 2), // severity 0 and 1 are errors; rest are warnings
                               code,
                               error.Message
                               ));
                WriteError(string.Empty);
            }
        }
 /// <summary>
 /// Parsing error occured - event handler.
 /// </summary>
 private void parser_CssError(object sender, CssErrorEventArgs e)
 {
     // Do not minify in the case of parse exception and log the error
     minificationError = e.Exception;
 }