示例#1
0
        private ParserResults ParseCore(ITextDocument input)
        {
            // Setup the parser context
            var context = new ParserContext(input, CodeParser, MarkupParser, MarkupParser)
            {
                DesignTimeMode = DesignTimeMode
            };

            MarkupParser.Context = context;
            CodeParser.Context   = context;

            // Execute the parse
            MarkupParser.ParseDocument();

            // Get the result
            var results = context.CompleteParse();

            // Rewrite whitespace if supported
            var rewritingContext = new RewritingContext(results.Document);

            foreach (ISyntaxTreeRewriter rewriter in Optimizers)
            {
                rewriter.Rewrite(rewritingContext);
            }

            if (TagHelperDescriptorResolver != null)
            {
                var descriptors       = GetTagHelperDescriptors(rewritingContext.SyntaxTree);
                var tagHelperProvider = new TagHelperDescriptorProvider(descriptors);

                var tagHelperParseTreeRewriter = new TagHelperParseTreeRewriter(tagHelperProvider);
                // Rewrite the document to utilize tag helpers
                tagHelperParseTreeRewriter.Rewrite(rewritingContext);
            }

            var syntaxTree = rewritingContext.SyntaxTree;

            // Link the leaf nodes into a chain
            Span prev = null;

            foreach (Span node in syntaxTree.Flatten())
            {
                node.Previous = prev;
                if (prev != null)
                {
                    prev.Next = node;
                }
                prev = node;
            }

            // We want to surface both the parsing and rewriting errors as one unified list of errors because
            // both parsing and rewriting errors affect the end users Razor page.
            var errors = results.ParserErrors.Concat(rewritingContext.Errors).ToList();

            // Return the new result
            return(new ParserResults(syntaxTree, errors));
        }
示例#2
0
        private ParserResults ParseCore(ITextDocument input)
        {
            // Setup the parser context
            var errorSink = new ErrorSink();
            var context   = new ParserContext(input, CodeParser, MarkupParser, MarkupParser, errorSink)
            {
                DesignTimeMode = DesignTimeMode
            };

            MarkupParser.Context = context;
            CodeParser.Context   = context;

            // Execute the parse
            MarkupParser.ParseDocument();

            // Get the result
            var results = context.CompleteParse();

            // Rewrite whitespace if supported
            var rewritingContext = new RewritingContext(results.Document, errorSink);

            foreach (ISyntaxTreeRewriter rewriter in Optimizers)
            {
                rewriter.Rewrite(rewritingContext);
            }

            var descriptors = Enumerable.Empty <TagHelperDescriptor>();

            if (TagHelperDescriptorResolver != null)
            {
                descriptors = GetTagHelperDescriptors(rewritingContext.SyntaxTree, rewritingContext.ErrorSink);
                var tagHelperProvider = new TagHelperDescriptorProvider(descriptors);

                var tagHelperParseTreeRewriter = new TagHelperParseTreeRewriter(tagHelperProvider);
                // Rewrite the document to utilize tag helpers
                tagHelperParseTreeRewriter.Rewrite(rewritingContext);
            }

            var syntaxTree = rewritingContext.SyntaxTree;

            // Link the leaf nodes into a chain
            Span prev = null;

            foreach (Span node in syntaxTree.Flatten())
            {
                node.Previous = prev;
                if (prev != null)
                {
                    prev.Next = node;
                }
                prev = node;
            }

            // Return the new result
            return(new ParserResults(syntaxTree, descriptors, errorSink));
        }
示例#3
0
        protected override void ExecuteCore(RazorCodeDocument codeDocument)
        {
            var syntaxTree = codeDocument.GetSyntaxTree();

            ThrowForMissingDocumentDependency(syntaxTree);

            var descriptors = codeDocument.GetTagHelpers();

            if (descriptors == null)
            {
                var feature = Engine.Features.OfType <ITagHelperFeature>().FirstOrDefault();
                if (feature == null)
                {
                    // No feature, nothing to do.
                    return;
                }

                descriptors = feature.GetDescriptors();
            }

            // We need to find directives in all of the *imports* as well as in the main razor file
            //
            // The imports come logically before the main razor file and are in the order they
            // should be processed.
            var visitor = new DirectiveVisitor(descriptors);
            var imports = codeDocument.GetImportSyntaxTrees();

            if (imports != null)
            {
                for (var i = 0; i < imports.Count; i++)
                {
                    var import = imports[i];
                    visitor.Visit(import.Root);
                }
            }

            visitor.Visit(syntaxTree.Root);

            var tagHelperPrefix = visitor.TagHelperPrefix;

            descriptors = visitor.Matches.ToArray();

            var context = TagHelperDocumentContext.Create(tagHelperPrefix, descriptors);

            codeDocument.SetTagHelperContext(context);

            if (descriptors.Count == 0)
            {
                // No descriptors, no-op.
                return;
            }

            var rewrittenSyntaxTree = TagHelperParseTreeRewriter.Rewrite(syntaxTree, tagHelperPrefix, descriptors);

            codeDocument.SetSyntaxTree(rewrittenSyntaxTree);
        }
    internal void EvaluateData(
        IEnumerable <TagHelperDescriptor> descriptors,
        string documentContent,
        string tagHelperPrefix = null,
        RazorParserFeatureFlags featureFlags = null)
    {
        var syntaxTree = ParseDocument(documentContent, featureFlags: featureFlags);

        var rewrittenTree = TagHelperParseTreeRewriter.Rewrite(syntaxTree, tagHelperPrefix, descriptors);

        BaselineTest(rewrittenTree);
    }
示例#5
0
        public void IsComment_ReturnsTrueForSpanInHtmlCommentBlock()
        {
            // Arrange
            SpanFactory spanFactory = new SpanFactory();

            Span  content      = spanFactory.Markup("<!-- comment -->");
            Block commentBlock = new HtmlCommentBlock(content);

            // Act
            bool actualResult = TagHelperParseTreeRewriter.IsComment(content);

            // Assert
            Assert.True(actualResult);
        }
示例#6
0
        public RazorSyntaxTree Execute(RazorCodeDocument document, RazorSyntaxTree syntaxTree)
        {
            var resolver = Engine.Features.OfType <TagHelperFeature>().FirstOrDefault()?.Resolver;

            if (resolver == null)
            {
                // No TagHelpers, so nothing to do.
                return(syntaxTree);
            }

            var visitor     = new TagHelperDirectiveSpanVisitor(resolver, document.ErrorSink);
            var descriptors = visitor.GetDescriptors(syntaxTree.Root);

            var rewriter = new TagHelperParseTreeRewriter(new TagHelperDescriptorProvider(descriptors));

            var context = new RewritingContext(syntaxTree.Root, document.ErrorSink);

            rewriter.Rewrite(context);
            return(RazorSyntaxTree.Create(context.SyntaxTree, context.ErrorSink.Errors));
        }
示例#7
0
    protected override void ExecuteCore(RazorCodeDocument codeDocument)
    {
        var syntaxTree = codeDocument.GetSyntaxTree();

        ThrowForMissingDocumentDependency(syntaxTree);

        var descriptors = codeDocument.GetTagHelpers();

        if (descriptors == null)
        {
            var feature = Engine.GetFeature <ITagHelperFeature>();
            if (feature == null)
            {
                // No feature, nothing to do.
                return;
            }

            descriptors = feature.GetDescriptors();
        }

        var parserOptions = codeDocument.GetParserOptions();

        // We need to find directives in all of the *imports* as well as in the main razor file
        //
        // The imports come logically before the main razor file and are in the order they
        // should be processed.
        DirectiveVisitor visitor;

        if (FileKinds.IsComponent(codeDocument.GetFileKind()) &&
            (parserOptions == null || parserOptions.FeatureFlags.AllowComponentFileKind))
        {
            codeDocument.TryComputeNamespace(fallbackToRootNamespace: true, out var currentNamespace);
            visitor = new ComponentDirectiveVisitor(codeDocument.Source.FilePath, descriptors, currentNamespace);
        }
        else
        {
            visitor = new TagHelperDirectiveVisitor(descriptors);
        }
        var imports = codeDocument.GetImportSyntaxTrees();

        if (imports != null)
        {
            for (var i = 0; i < imports.Count; i++)
            {
                var import = imports[i];
                visitor.Visit(import);
            }
        }

        visitor.Visit(syntaxTree);

        // This will always be null for a component document.
        var tagHelperPrefix = visitor.TagHelperPrefix;

        descriptors = visitor.Matches.ToArray();

        var context = TagHelperDocumentContext.Create(tagHelperPrefix, descriptors);

        codeDocument.SetTagHelperContext(context);

        if (descriptors.Count == 0)
        {
            // No descriptors, no-op.
            return;
        }

        var rewrittenSyntaxTree = TagHelperParseTreeRewriter.Rewrite(syntaxTree, tagHelperPrefix, descriptors);

        codeDocument.SetSyntaxTree(rewrittenSyntaxTree);
    }
示例#8
0
        protected override void ExecuteCore(RazorCodeDocument codeDocument)
        {
            var syntaxTree = codeDocument.GetSyntaxTree();

            ThrowForMissingDocumentDependency(syntaxTree);

            var feature = Engine.Features.OfType <ITagHelperFeature>().FirstOrDefault();

            if (feature == null)
            {
                // No feature, nothing to do.
                return;
            }

            // We need to find directives in all of the *imports* as well as in the main razor file
            //
            // The imports come logically before the main razor file and are in the order they
            // should be processed.
            var descriptors = feature.GetDescriptors();
            var visitor     = new DirectiveVisitor(descriptors);
            var imports     = codeDocument.GetImportSyntaxTrees();

            if (imports != null)
            {
                for (var i = 0; i < imports.Count; i++)
                {
                    var import = imports[i];
                    visitor.VisitBlock(import.Root);
                }
            }

            visitor.VisitBlock(syntaxTree.Root);

            var tagHelperPrefix = visitor.TagHelperPrefix;

            descriptors = visitor.Matches.ToArray();

            var context = TagHelperDocumentContext.Create(tagHelperPrefix, descriptors);

            codeDocument.SetTagHelperContext(context);

            if (descriptors.Count == 0)
            {
                // No descriptors, no-op.
                return;
            }

            var errorSink = new ErrorSink();
            var rewriter  = new TagHelperParseTreeRewriter(tagHelperPrefix, descriptors, syntaxTree.Options.FeatureFlags);

            var root = syntaxTree.Root;

            root = rewriter.Rewrite(root, errorSink);

            // Temporary code while we're still using legacy diagnostics in the SyntaxTree.
            var errorList = new List <RazorDiagnostic>();

            errorList.AddRange(errorSink.Errors.Select(error => RazorDiagnostic.Create(error)));

            errorList.AddRange(descriptors.SelectMany(d => d.GetAllDiagnostics()));

            var diagnostics = CombineErrors(syntaxTree.Diagnostics, errorList);

            var newSyntaxTree = RazorSyntaxTree.Create(root, syntaxTree.Source, diagnostics, syntaxTree.Options);

            codeDocument.SetSyntaxTree(newSyntaxTree);
        }