protected override SyntaxTreeNode RewriteSpan (BlockBuilder parent, Span span)
		{
			var b = new SpanBuilder (span);
			var old = (LiteralAttributeCodeGenerator)span.CodeGenerator;
			b.CodeGenerator = old.ValueGenerator != null
				? new PreprocessedLiteralAttributeCodeGenerator (old.Prefix, old.ValueGenerator)
				: new PreprocessedLiteralAttributeCodeGenerator (old.Prefix, old.Value);
			return b.Build ();
		}
        protected override SyntaxTreeNode RewriteBlock(BlockBuilder parent, Block block)
        {
            // Collect the content of this node
            string content = String.Concat(block.Children.Cast<Span>().Select(s => s.Content));

            // Create a new span containing this content
            SpanBuilder span = new SpanBuilder();
            FillSpan(span, block.Children.Cast<Span>().First().Start, content);
            return span.Build();
        }
        public void ConstructorWithBlockBuilderSetsParent()
        {
            // Arrange
            BlockBuilder builder = new BlockBuilder() { Type = BlockType.Comment };
            Span span = new SpanBuilder() { Kind = SpanKind.Code }.Build();
            builder.Children.Add(span);

            // Act
            Block block = builder.Build();

            // Assert
            Assert.Same(block, span.Parent);
        }
        public override void VisitSpan(Span span)
        {
            if (span.Kind == SpanKind.Markup && !m_DebugStatusReader.IsDebuggingEnabled())
            {
                string content = m_HtmlPageMinifier.Minify(span.Content, true, true);

                SpanBuilder builder = new SpanBuilder { CodeGenerator = span.CodeGenerator, EditHandler = span.EditHandler, Kind = span.Kind, Start = span.Start };
                MarkupSymbol symbol = new MarkupSymbol { Content = content };
                builder.Accept(symbol);
                span.ReplaceWith(builder);
            }
       
            base.VisitSpan(span);
        }
        protected override SyntaxTreeNode RewriteSpan(BlockBuilder parent, Span span)
        {
            // Only rewrite if we have a previous that is also markup (CanRewrite does this check for us!)
            Span previous = parent.Children.LastOrDefault() as Span;
            if (previous == null || !CanRewrite(previous))
            {
                return span;
            }

            // Merge spans
            parent.Children.Remove(previous);
            SpanBuilder merged = new SpanBuilder();
            FillSpan(merged, previous.Start, previous.Content + span.Content);
            return merged.Build();
        }
        public void ConstructorTransfersChildrenFromBlockBuilder()
        {
            // Arrange
            Span expected = new SpanBuilder() { Kind = SpanKind.Code }.Build();
            BlockBuilder builder = new BlockBuilder()
            {
                Type = BlockType.Functions
            };
            builder.Children.Add(expected);

            // Act
            Block block = builder.Build();

            // Assert
            Assert.Same(expected, block.Children.Single());
        }
Exemple #7
0
        public void ReplaceWith(SpanBuilder builder)
        {
            Debug.Assert(!builder.Symbols.Any() || builder.Symbols.All(s => s != null));

            Kind = builder.Kind;
            Symbols = builder.Symbols;
            EditHandler = builder.EditHandler;
            CodeGenerator = builder.CodeGenerator ?? SpanCodeGenerator.Null;
            _start = builder.Start;

            // Since we took references to the values in SpanBuilder, clear its references out
            builder.Reset();

            // Calculate other properties
            Content = Symbols.Aggregate(new StringBuilder(), (sb, sym) => sb.Append(sym.Content), sb => sb.ToString());
        }
 protected virtual SpanBuilder UpdateSpan(Span target, TextChange normalizedChange)
 {
     string newContent = normalizedChange.ApplyChange(target);
     SpanBuilder newSpan = new SpanBuilder(target);
     newSpan.ClearSymbols();
     foreach (ISymbol sym in Tokenizer(newContent))
     {
         sym.OffsetStart(target.Start);
         newSpan.Accept(sym);
     }
     if (target.Next != null)
     {
         SourceLocation newEnd = SourceLocationTracker.CalculateNewLocation(target.Start, newContent);
         target.Next.ChangeStart(newEnd);
     }
     return newSpan;
 }
        public override void VisitSpan(Span span)
        {
            if (span.Kind == SpanKind.Markup)
              {
            string content = span.Content;

            content = _minifier.Minify(content);

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

              base.VisitSpan(span);
        }
Exemple #10
0
        public void ReplaceWith(SpanBuilder builder)
        {
            Debug.Assert(!builder.Symbols.Any() || builder.Symbols.All(s => s != null));

            Kind          = builder.Kind;
            Symbols       = builder.Symbols;
            EditHandler   = builder.EditHandler;
            CodeGenerator = builder.CodeGenerator ?? SpanCodeGenerator.Null;
            _start        = builder.Start;

            // Since we took references to the values in SpanBuilder, clear its references out
            builder.Reset();

            // Calculate other properties
            Content = Symbols.Aggregate(
                new StringBuilder(),
                (sb, sym) => sb.Append(sym.Content),
                sb => sb.ToString()
                );
        }
        //public override void VisitBlock(Block block)
        //{
        //    BlockBuilder parent = null;
        //    if (_blocks.Count > 0)
        //    {
        //        parent = _blocks.Peek();
        //    }
        //    BlockBuilder newBlock = new BlockBuilder(block);
        //    newBlock.Children.Clear();
        //    _blocks.Push(newBlock);
        //    if (block.Type == BlockType.Expression && parent != null)
        //    {
        //        VisitExpressionBlock(block, parent);
        //    }
        //    else
        //    {
        //        base.VisitBlock(block);
        //    }
        //    if (_blocks.Count > 1)
        //    {
        //        parent.Children.Add(_blocks.Pop().Build());
        //    }
        //}

        //public override void VisitSpan(Span span)
        //{
        //    Debug.Assert(_blocks.Count > 0);
        //    _blocks.Peek().Children.Add(span);
        //}

        protected override SyntaxTreeNode RewriteBlock(BlockBuilder parent, Block block)
        {
            BlockBuilder newBlock = new BlockBuilder(block);
            newBlock.Children.Clear();
            Span ws = block.Children.FirstOrDefault() as Span;
            IEnumerable<SyntaxTreeNode> newNodes = block.Children;
            if (ws.Content.All(Char.IsWhiteSpace))
            {
                // Add this node to the parent
                SpanBuilder builder = new SpanBuilder(ws);
                builder.ClearSymbols();
                FillSpan(builder, ws.Start, ws.Content);
                parent.Children.Add(builder.Build());

                // Remove the old whitespace node
                newNodes = block.Children.Skip(1);
            }

            foreach (SyntaxTreeNode node in newNodes)
            {
                newBlock.Children.Add(node);
            }
            return newBlock.Build();
        }
        private void MinifyMarkup(BlockBuilder block)
        {
            var codeGenerator = new MarkupCodeGenerator();
            var previousIsWhiteSpace = true;
            var previousTokenEndsWithBlockElement = true;

            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;
                    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);

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

                // 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);
            }
        }
 protected void FillSpan(SpanBuilder builder, SourceLocation start, string content)
 {
     _markupSpanFactory(builder, start, content);
 }
        public void AddSpanAddsSpanToCurrentBlockBuilder()
        {
            // Arrange
            var factory = SpanFactory.CreateCsHtml();
            Mock<ParserVisitor> mockListener = new Mock<ParserVisitor>();
            ParserContext context = SetupTestContext("phoo");

            SpanBuilder builder = new SpanBuilder()
            {
                Kind = SpanKind.Code
            };
            builder.Accept(new CSharpSymbol(1, 0, 1, "foo", CSharpSymbolType.Identifier));
            Span added = builder.Build();

            using (context.StartBlock(BlockType.Functions))
            {
                context.AddSpan(added);
            }

            BlockBuilder expected = new BlockBuilder()
            {
                Type = BlockType.Functions,
            };
            expected.Children.Add(added);

            // Assert
            ParserTestBase.EvaluateResults(context.CompleteParse(), expected.Build());
        }
 public override void BuildSpan(SpanBuilder span, Razor.Text.SourceLocation start, string content)
 {
     throw new NotImplementedException();
 }
Exemple #16
0
 public abstract void BuildSpan(SpanBuilder span, SourceLocation start, string content);
 public EditResult(PartialParseResult result, SpanBuilder editedSpan)
 {
     Result = result;
     EditedSpan = editedSpan;
 }
Exemple #18
0
 public Span(SpanBuilder builder)
 {
     ReplaceWith(builder);
 }
Exemple #19
0
 public void Change(Action<SpanBuilder> changes)
 {
     SpanBuilder builder = new SpanBuilder(this);
     changes(builder);
     ReplaceWith(builder);
 }
Exemple #20
0
 public Span(SpanBuilder builder)
 {
     ReplaceWith(builder);
 }
 public SpanConstructor(SpanKind kind, IEnumerable<ISymbol> symbols)
 {
     Builder = new SpanBuilder();
     Builder.Kind = kind;
     Builder.EditHandler = SpanEditHandler.CreateDefault(TestTokenizer);
     foreach (ISymbol sym in symbols)
     {
         Builder.Accept(sym);
     }
 }
 private void DefaultMarkupSpan(SpanBuilder span)
 {
     span.CodeGenerator = new MarkupCodeGenerator();
     span.EditHandler = new SpanEditHandler(Language.TokenizeString, AcceptedCharacters.Any);
 }
Exemple #23
0
 public override void BuildSpan(System.Web.Razor.Parser.SyntaxTree.SpanBuilder span, System.Web.Razor.Text.SourceLocation start, string content)
 {
     _other.BuildSpan(span, start, content);
 }