Esempio n. 1
0
        protected override void Visit(ExpressionBlockChunk chunk)
        {
            var             firstChildExpression = chunk.Children.FirstOrDefault() as ExpressionChunk;
            var             padding          = 0;
            MappingLocation documentLocation = null;

            if (firstChildExpression != null)
            {
                padding          = _paddingBuilder.CalculateExpressionPadding((Span)firstChildExpression.Association);
                documentLocation = CreateMappingLocation(firstChildExpression.Start, firstChildExpression.Code.Length);
            }

            var expressionBlock = new CSharpBlock();

            using (_context.Builder.UseBlock(expressionBlock))
            {
                Accept(chunk.Children);
            }

            var renderExpression = new RenderExpression
            {
                Expression       = expressionBlock,
                Padding          = padding,
                DocumentLocation = documentLocation
            };

            _context.Builder.Add(renderExpression);
        }
Esempio n. 2
0
        protected override void Visit(RazorDirectiveChunk chunk)
        {
            var tokensBlock = new CSharpBlock();

            using (_context.Builder.UseBlock(tokensBlock))
            {
                Accept(chunk.Children);
            }

            IRazorDirective directive;
            var             tokens = tokensBlock.Children.OfType <RazorDirectiveToken>().ToList();

            if (chunk.Descriptor.Type == RazorDirectiveDescriptorType.SingleLine)
            {
                directive = new RazorSingleLineDirective()
                {
                    Name   = chunk.Name,
                    Tokens = tokens,
                };
            }
            else
            {
                var directiveChildren = tokensBlock.Children.Except(tokens);
                var directiveBlock    = new RazorBlockDirective()
                {
                    Name   = chunk.Name,
                    Tokens = tokens,
                };

                directiveBlock.Children.AddRange(directiveChildren);
                directive = directiveBlock;
            }

            _context.Builder.Add(directive);
        }
Esempio n. 3
0
        public IDisposable UseBlock(CSharpBlock csharpBlock)
        {
            _activeScopes.Push(csharpBlock);

            var builderScope = new BlockBuilderScope(_endBlock);

            return(builderScope);
        }
Esempio n. 4
0
        public void Walk(CSharpBlock block)
        {
            for (var i = 0; i < block.Children.Count; i++)
            {
                var             literal         = false;
                MappingLocation mappingLocation = null;
                var             current         = block.Children[i];

                if (current is CSharpBlock)
                {
                    Walk((CSharpBlock)current);
                }
                else if (current is RenderHtml)
                {
                    var renderHtml = current as RenderHtml;
                    literal         = true;
                    mappingLocation = renderHtml.DocumentLocation;
                }
                else if (current is RenderExpression)
                {
                    var renderExpression = current as RenderExpression;
                    literal         = false;
                    mappingLocation = renderExpression.DocumentLocation;
                }
                else if (current is ExecuteTagHelpers)
                {
                    Debug.Assert(block is RenderTagHelper);
                    var renderTagHelper   = block as RenderTagHelper;
                    var executeTagHelpers = current as ExecuteTagHelpers;
                    literal         = false;
                    mappingLocation = renderTagHelper.DocumentLocation;
                }

                if (mappingLocation != null)
                {
                    var beginInstrumentation = new BeginInstrumentation
                    {
                        DocumentLocation = mappingLocation,
                        Literal          = literal,
                    };
                    block.Children.Insert(i, beginInstrumentation);
                    var endInstrumentation = new EndInstrumentation();
                    block.Children.Insert(i + 2, endInstrumentation);
                    i += 2;

                    mappingLocation = null;
                }
            }
        }
Esempio n. 5
0
        protected override void Visit(DynamicCodeAttributeChunk chunk)
        {
            var value = new CSharpBlock();

            using (_context.Builder.UseBlock(value))
            {
                Accept(chunk.Children);
            }

            var attributePiece = new ConditionalAttributePiece
            {
                DocumentLocation = CreateMappingLocation(chunk.Start, chunk.Association.Length),
                Prefix           = chunk.Prefix,
                Value            = value
            };

            _context.Builder.Add(attributePiece);
        }
Esempio n. 6
0
        protected override void Visit(CodeAttributeChunk chunk)
        {
            var valueBlock = new CSharpBlock();

            using (_context.Builder.UseBlock(valueBlock))
            {
                Accept(chunk.Children);
            }

            var attribute = new RenderConditionalAttribute
            {
                DocumentLocation = CreateMappingLocation(chunk.Start, chunk.Association.Length),
                Name             = chunk.Attribute,
                Prefix           = chunk.Prefix,
                ValuePieces      = valueBlock.Children,
                Suffix           = chunk.Suffix,
            };

            _context.Builder.Add(attribute);
        }
Esempio n. 7
0
            public void Walk(CSharpBlock block)
            {
                for (var i = 0; i < block.Children.Count; i++)
                {
                    var current = block.Children[i];

                    if (current is ViewClassDeclaration)
                    {
                        _classDeclaration    = (ViewClassDeclaration)current;
                        _variableCountOffset = _classDeclaration.Children.Count;
                    }

                    if (current is CSharpBlock && !(current is RenderTagHelper))
                    {
                        Walk((CSharpBlock)current);
                    }
                    else
                    {
                        WalkSource(current);
                    }
                }
            }
Esempio n. 8
0
        private static ViewClassDeclaration FindClassDeclaration(CSharpBlock block)
        {
            if (block is ViewClassDeclaration)
            {
                return((ViewClassDeclaration)block);
            }

            for (var i = 0; i < block.Children.Count; i++)
            {
                var currentBlock = block.Children[i] as CSharpBlock;
                if (currentBlock == null)
                {
                    continue;
                }

                var classDeclaration = FindClassDeclaration(currentBlock);
                if (classDeclaration != null)
                {
                    return(classDeclaration);
                }
            }

            return(null);
        }
Esempio n. 9
0
        private void AddTagHelperAttributes(IList <TagHelperAttributeTracker> attributes, IEnumerable <TagHelperDescriptor> descriptors)
        {
            var renderedBoundAttributeNames = new HashSet <string>(StringComparer.OrdinalIgnoreCase);

            foreach (var attribute in attributes)
            {
                var attributeValueChunk   = attribute.Value;
                var associatedDescriptors = descriptors.Where(descriptor =>
                                                              descriptor.Attributes.Any(attributeDescriptor => attributeDescriptor.IsNameMatch(attribute.Name)));

                if (associatedDescriptors.Any() && renderedBoundAttributeNames.Add(attribute.Name))
                {
                    if (attributeValueChunk == null)
                    {
                        // Minimized attributes are not valid for bound attributes. TagHelperBlockRewriter has already
                        // logged an error if it was a bound attribute; so we can skip.
                        continue;
                    }

                    foreach (var associatedDescriptor in associatedDescriptors)
                    {
                        var valueBlock = new CSharpBlock();
                        using (_context.Builder.UseBlock(valueBlock))
                        {
                            Accept(attribute.Value);
                        }

                        var associatedAttributeDescriptor = associatedDescriptor.Attributes.First(
                            attributeDescriptor => attributeDescriptor.IsNameMatch(attribute.Name));
                        var associationBlock     = attribute.Value.Association as Block;
                        var documentLocation     = attribute.Value.Start; // TODO: Do we need to handle @ at the beginning of non-string TH attributes?
                        var contentLength        = attribute.Value.Association.Length;
                        var setTagHelperProperty = new SetTagHelperProperty
                        {
                            PropertyName         = associatedAttributeDescriptor.PropertyName,
                            AttributeName        = attribute.Name,
                            TagHelperTypeName    = associatedDescriptor.TypeName,
                            Value                = valueBlock,
                            DocumentLocation     = CreateMappingLocation(documentLocation, contentLength),
                            AssociatedDescriptor = associatedAttributeDescriptor,
                            ValueStyle           = attribute.ValueStyle
                        };
                        _context.Builder.Add(setTagHelperProperty);
                    }
                }
                else
                {
                    var addHtmlAttribute = new AddTagHelperHtmlAttribute
                    {
                        Name       = attribute.Name,
                        ValueStyle = attribute.ValueStyle
                    };

                    if (attributeValueChunk != null)
                    {
                        var valueBlock = new CSharpBlock();
                        using (_context.Builder.UseBlock(valueBlock))
                        {
                            Accept(attribute.Value);
                        }
                        addHtmlAttribute.ValuePieces = valueBlock.Children;
                    }

                    _context.Builder.Add(addHtmlAttribute);
                }
            }
        }