/// <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; }
internal CssErrorEventArgs(CssException exc, ContextError error) { Error = error; Exception = exc; }
protected void OnCssError(CssException exception) { if (CssError != null && exception != null) { CssError(this, new CssErrorEventArgs(exception, new ContextError( exception.Severity < 2, exception.Severity, GetSeverityString(exception.Severity), string.Format(CultureInfo.InvariantCulture, "CSS{0}", (exception.Error & (0xffff))), exception.HelpLink, FileContext, exception.Line, exception.Char, 0, 0, exception.Message))); } }
/// <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(); // Ignore error: 'An underscore is not a valid CSS1 or CSS2 identifier character' parser.Settings.IgnoreErrorList = "CSS1008"; parser.Settings.AllowEmbeddedAspNetBlocks = true; 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 the error info to the end of non-minified resource resource += string.Format("\r\n\r\n/* Minification failed (line {0}, error number {1}): {2} */", minificationError.Line, minificationError.Error, minificationError.Message); } return resource; }
/// <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; }
protected void OnCssError(CssException exception) { if (CssError != null && exception != null && !Settings.IgnoreAllErrors) { // format our CSS error code string errorCode = "CSS{0}".FormatInvariant((exception.Error & (0xffff))); // if we have no errors in our error ignore list, or if we do but this error code is not in // that list, fire the event to whomever is listening for it. if (!Settings.IgnoreErrorCollection.Contains(errorCode)) { CssError(this, new CssErrorEventArgs(exception, new ContextError( exception.Severity < 2, exception.Severity, GetSeverityString(exception.Severity), errorCode, exception.HelpLink, FileContext, exception.Line, exception.Char, 0, 0, exception.Message))); } } }