Exemple #1
0
        public void VisitSpan(Span span)
        {
            // We only minify the static text
            var markupSpan = span as MarkupSpan;

            if (markupSpan == null)
            {
                // When we have a dynamic markup, we can't know if the last char will be whitespace
                // => to make it work in all cases, we won't minifiy whitespace just after code.
                _previousIsWhiteSpace = false;
                _previousTokenEndsWithBlockElement = false;
                return;
            }

            var content = markupSpan.Content;

            content = _minifier.Minify(content, _previousIsWhiteSpace, _previousTokenEndsWithBlockElement);

            _minifier.AnalyseContent(content, ref _previousIsWhiteSpace, ref _previousTokenEndsWithBlockElement);

            span.Content = content;
        }
Exemple #2
0
        private void MinifyMarkup(BlockBuilder block)
        {
            var codeGenerator        = new MarkupCodeGenerator();
            var previousIsWhiteSpace = true;
            var previousTokenEndsWithBlockElement = true;
            var insideScript = false;

            for (int i = 0; i < block.Children.Count; i++)
            {
                var node = block.Children[i];
                var span = node as Span;
                if (span == null)
                {
                    // When we have a dynamic markup, we can't know if the last char will be whitespace
                    // => to make it work in all cases, we won't minifiy whitespace just after code.
                    previousIsWhiteSpace = false;
                    previousTokenEndsWithBlockElement = false;

                    var section = node as Block;
                    if ((section != null) && (section.Type == BlockType.Section))
                    {
                        // Sections are special as they force us to recurse the minification
                        block.Children[i]    = MinifySectionBlock(section);
                        previousIsWhiteSpace = false;
                        previousTokenEndsWithBlockElement = false;
                    }
                    continue;
                }

                // There may be several HTML tokens just one after the other.
                // => we concatenate everything in a single token to minify everyting in a single scan
                // (we is better for Javascript minification).
                var sb = new StringBuilder();
                sb.Append(span.Content);
                if (i < block.Children.Count - 1)
                {
                    var markup = block.Children[i + 1] as Span;
                    while ((markup != null) && (markup.Kind == SpanKind.Markup) && ((markup.Next == null) || ((markup.Next != null) && ((markup.Next.Kind == SpanKind.Markup) || ((markup.Next.Kind != SpanKind.Markup) && !markup.Content.EndsWith("\""))))))
                    {
                        block.Children.RemoveAt(i + 1);
                        sb.Append(markup.Content);
                        markup = i + 1 < block.Children.Count ? block.Children[i + 1] as Span : null;
                    }
                }

                var content = sb.ToString();
                if (string.IsNullOrEmpty(content))
                {
                    // Nothing to minify
                    block.Children.RemoveAt(i);
                    continue;
                }

                content = _minifier.Minify(content, previousIsWhiteSpace, previousTokenEndsWithBlockElement, insideScript);

                _minifier.AnalyseContent(content, ref previousIsWhiteSpace, ref previousTokenEndsWithBlockElement, ref insideScript);

                // We replace the content with the minified markup
                // and then let the CSharp/VB generator do their jobs.
                var builder = new SpanBuilder()
                {
                    CodeGenerator = codeGenerator, EditHandler = span.EditHandler, Kind = span.Kind, Start = span.Start
                };
                var symbol = new MarkupSymbol()
                {
                    Content = content
                };
                builder.Accept(symbol);
                span.ReplaceWith(builder);
            }
        }