Ejemplo n.º 1
0
        public static RazorParserFeatureFlags Create(RazorLanguageVersion version, string fileKind)
        {
            if (fileKind == null)
            {
                throw new ArgumentNullException(nameof(fileKind));
            }

            var allowMinimizedBooleanTagHelperAttributes = false;
            var allowHtmlCommentsInTagHelpers            = false;
            var allowComponentFileKind             = false;
            var allowRazorInAllCodeBlocks          = false;
            var allowUsingVariableDeclarations     = false;
            var allowConditionalDataDashAttributes = false;
            var allowCSharpInMarkupAttributeArea   = true;
            var allowNullableForgivenessOperator   = false;

            if (version.CompareTo(RazorLanguageVersion.Version_2_1) >= 0)
            {
                // Added in 2.1
                allowMinimizedBooleanTagHelperAttributes = true;
                allowHtmlCommentsInTagHelpers            = true;
            }

            if (version.CompareTo(RazorLanguageVersion.Version_3_0) >= 0)
            {
                // Added in 3.0
                allowComponentFileKind           = true;
                allowRazorInAllCodeBlocks        = true;
                allowUsingVariableDeclarations   = true;
                allowNullableForgivenessOperator = true;
            }

            if (FileKinds.IsComponent(fileKind))
            {
                allowConditionalDataDashAttributes = true;
                allowCSharpInMarkupAttributeArea   = false;
            }

            if (version.CompareTo(RazorLanguageVersion.Experimental) >= 0)
            {
                allowConditionalDataDashAttributes = true;
            }

            return(new DefaultRazorParserFeatureFlags(
                       allowMinimizedBooleanTagHelperAttributes,
                       allowHtmlCommentsInTagHelpers,
                       allowComponentFileKind,
                       allowRazorInAllCodeBlocks,
                       allowUsingVariableDeclarations,
                       allowConditionalDataDashAttributes,
                       allowCSharpInMarkupAttributeArea,
                       allowNullableForgivenessOperator));
        }
        public RazorSyntaxTree Execute(RazorCodeDocument codeDocument, RazorSyntaxTree syntaxTree)
        {
            if (FileKinds.IsComponent(codeDocument.GetFileKind()))
            {
                // Nothing to do here.
                return(syntaxTree);
            }

            var sectionVerifier = new NestedSectionVerifier(syntaxTree);

            return(sectionVerifier.Verify());
        }
Ejemplo n.º 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.
            DirectiveVisitor visitor = null;
            var parserOptions        = codeDocument.GetParserOptions();

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