Exemplo n.º 1
0
        /// <summary>
        /// Produces a code minifiction of CSS content by using the Microsoft Ajax CSS Minifier
        /// </summary>
        /// <param name="content">CSS content</param>
        /// <param name="isInlineCode">Flag whether the content is inline code</param>
        /// <param name="encoding">Text encoding</param>
        /// <returns>Minification result</returns>
        public CodeMinificationResult Minify(string content, bool isInlineCode, Encoding encoding)
        {
            if (string.IsNullOrWhiteSpace(content))
            {
                return(new CodeMinificationResult(string.Empty));
            }

            string newContent = string.Empty;
            var    errors     = new List <MinificationErrorInfo>();
            var    warnings   = new List <MinificationErrorInfo>();

            lock (_minificationSynchronizer)
            {
                if (_errorReporter == null)
                {
                    _errorReporter = new MsAjaxErrorReporter(_settings.WarningLevel);
                }

                CssParser originalCssParser = isInlineCode ?
                                              _originalInlineCssParser : _originalEmbeddedCssParser;
                if (originalCssParser == null)
                {
                    originalCssParser = CreateOriginalCssParserInstance(_settings, isInlineCode);
                    if (isInlineCode)
                    {
                        _originalInlineCssParser = originalCssParser;
                    }
                    else
                    {
                        _originalEmbeddedCssParser = originalCssParser;
                    }
                }

                originalCssParser.CssError += _errorReporter.ParseErrorHandler;

                try
                {
                    // Parse the input
                    newContent = originalCssParser.Parse(content);
                }
                finally
                {
                    originalCssParser.CssError -= _errorReporter.ParseErrorHandler;

                    errors.AddRange(_errorReporter.Errors);
                    warnings.AddRange(_errorReporter.Warnings);

                    _errorReporter.Clear();
                }
            }

            return(new CodeMinificationResult(newContent, errors, warnings));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Produces a code minifiction of JS content by using the Microsoft Ajax JS Minifier
        /// </summary>
        /// <param name="content">JS content</param>
        /// <param name="isInlineCode">Flag whether the content is inline code</param>
        /// <param name="encoding">Text encoding</param>
        /// <returns>Minification result</returns>
        public CodeMinificationResult Minify(string content, bool isInlineCode, Encoding encoding)
        {
            if (string.IsNullOrWhiteSpace(content))
            {
                return(new CodeMinificationResult(string.Empty));
            }

            string newContent = string.Empty;
            var    errors     = new List <MinificationErrorInfo>();
            var    warnings   = new List <MinificationErrorInfo>();

            lock (_minificationSynchronizer)
            {
                if (_errorReporter == null)
                {
                    _errorReporter = new MsAjaxErrorReporter(_settings.WarningLevel);
                }

                JSParser originalJsParser = isInlineCode ?
                                            _originalInlineJsParser : _originalEmbeddedJsParser;
                if (originalJsParser == null)
                {
                    originalJsParser = CreateOriginalJsParserInstance(_settings, isInlineCode);
                    if (isInlineCode)
                    {
                        _originalInlineJsParser = originalJsParser;
                    }
                    else
                    {
                        _originalEmbeddedJsParser = originalJsParser;
                    }
                }

                originalJsParser.CompilerError += _errorReporter.ParseErrorHandler;

                var           stringBuilderPool = AdvancedStringBuilderPool.Shared;
                StringBuilder contentBuilder    = stringBuilderPool.Rent(content.Length);
                var           documentContext   = new DocumentContext(content)
                {
                    FileContext = string.Empty
                };

                try
                {
                    using (var stringWriter = new StringWriter(contentBuilder, CultureInfo.InvariantCulture))
                    {
                        // Parse the input
                        Block scriptBlock = originalJsParser.Parse(documentContext);
                        if (scriptBlock != null)
                        {
                            // Use normal output visitor
                            OutputVisitor.Apply(stringWriter, scriptBlock, originalJsParser.Settings);
                        }
                    }

                    newContent = contentBuilder.ToString();
                }
                finally
                {
                    originalJsParser.CompilerError -= _errorReporter.ParseErrorHandler;
                    stringBuilderPool.Return(contentBuilder);

                    errors.AddRange(_errorReporter.Errors);
                    warnings.AddRange(_errorReporter.Warnings);

                    _errorReporter.Clear();
                }
            }

            return(new CodeMinificationResult(newContent, errors, warnings));
        }