Beispiel #1
0
        public void ReplaceAspComments_Properly_Reformats_Server_Side_Comments()
        {
            var inputText      = $"{WebFormsCommentStartTag} {NormalExpressionContent} {WebFormsCommentEndTag}";
            var expectedOutput = $"{RazorCommentStartTag} {NormalExpressionContent} {RazorCommentEndTag}";
            var actualOutput   = EmbeddedCodeReplacers.ReplaceAspComments(inputText);

            Assert.AreEqual(expectedOutput, actualOutput);
        }
Beispiel #2
0
        public void ReplaceEmbeddedCodeBlocks_Properly_Reformats_Code_Blocks()
        {
            var inputText      = $"{WebFormsCodeBlockStartTag} {NormalExpressionContent} {WebFormsNormalEndTag}";
            var expectedOutput = $"{RazorCodeBlockStartTag} {NormalExpressionContent} {RazorCodeBlockEndTag}";
            var actualOutput   = EmbeddedCodeReplacers.ReplaceEmbeddedCodeBlocks(inputText);

            Assert.AreEqual(expectedOutput, actualOutput);
        }
Beispiel #3
0
        public void ReplaceAspExprs_Properly_Replaces_Unknown_Type_Tag()
        {
            var inputText      = $"{WebFormsAspExpressionStartTag} {UnknownAspExpressionType}: {UnknownAspExpressionContent} {WebFormsNormalEndTag}";
            var expectedOutput = $"@({UnknownAspExpressionType}.{UnknownAspExpressionContent})";
            var actualOutput   = EmbeddedCodeReplacers.ReplaceAspExprs(inputText);

            Assert.AreEqual(expectedOutput, actualOutput);
        }
Beispiel #4
0
        public void ReplaceAspExprs_Properly_Replaces_AppSettings_Type_Tag()
        {
            var inputText      = $"{WebFormsAspExpressionStartTag} {AppSettingsAspExpressionType}: {AppSettingsAspExpressionContent} {WebFormsNormalEndTag}";
            var expectedOutput = $"{RazorExplicitBindingStartTag}Configuration[\"{AppSettingsSectionName}:{AppSettingsAspExpressionContent}\"]{RazorExplicitBindingEndTag}";
            var actualOutput   = EmbeddedCodeReplacers.ReplaceAspExprs(inputText);

            Assert.AreEqual(expectedOutput, actualOutput);
        }
Beispiel #5
0
        public void ReplaceHTMLEncodedExprs_Properly_Reformats_Tag()
        {
            var inputText      = $"{WebFormsHTMLEncodedExpressionStartTag} {NormalExpressionContent} {WebFormsNormalEndTag}";
            var expectedOutput = $"{RazorExplicitBindingStartTag}{NormalExpressionContent}{RazorExplicitBindingEndTag}";
            var actualOutput   = EmbeddedCodeReplacers.ReplaceHTMLEncodedExprs(inputText);

            Assert.AreEqual(expectedOutput, actualOutput);
        }
Beispiel #6
0
        public static string ConvertEmbeddedCode(string htmlString, string originalFilePath, ViewImportService viewImportService)
        {
            htmlString = EmbeddedCodeReplacers.ReplaceOneWayDataBinds(htmlString);
            htmlString = EmbeddedCodeReplacers.ReplaceRawExprs(htmlString);
            htmlString = EmbeddedCodeReplacers.ReplaceHTMLEncodedExprs(htmlString);
            htmlString = EmbeddedCodeReplacers.ReplaceAspExprs(htmlString);
            htmlString = EmbeddedCodeReplacers.ReplaceAspComments(htmlString);
            htmlString = EmbeddedCodeReplacers.ReplaceEmbeddedCodeBlocks(htmlString);

            return(htmlString);
        }
Beispiel #7
0
        // View file converters will return razor file contents with
        // only view layer, code behind will be created in another file
        public override Task <IEnumerable <FileInformation> > MigrateFileAsync()
        {
            LogStart();
            _metricsContext.CollectActionMetrics(WebFormsActionType.FileConversion, ChildActionType);

            var result = new List <FileInformation>();

            try
            {
                var htmlString = File.ReadAllText(FullPath);

                // Replace directives first to build up list of user controls to be converted later by the ControlConverters
                var projectName = Path.GetFileName(ProjectPath);
                htmlString = EmbeddedCodeReplacers.ReplaceDirectives(htmlString, RelativePath, projectName, _viewImportService, _metricsContext);

                // Convert the Web Forms controls to Blazor equivalent
                var migratedDocument = GetRazorContents(htmlString);
                var contents         = migratedDocument.DocumentNode.WriteTo();

                // We comment out the unknown user controls here instead of during
                // traversal because the post-order nature may comment out controls
                // that are migrated as part of an ancestor control before that ancestor
                // can be processed
                contents = ControlConverter.ConvertEmbeddedCode(contents, RelativePath, _viewImportService);
                contents = UnknownControlRemover.RemoveUnknownTags(contents);

                // Currently just changing extension to .razor, keeping filename and directory the same
                // but Razor files are renamed and moved around, can't always use same filename/directory in the future
                var newRelativePath = FilePathHelper.AlterFileName(RelativePath, newExtension: ".razor");

                if (RelativePath.EndsWith(Constants.WebFormsPageMarkupFileExtension, StringComparison.InvariantCultureIgnoreCase))
                {
                    newRelativePath = Path.Combine(Constants.RazorPageDirectoryName, newRelativePath);
                }
                else if (RelativePath.EndsWith(Constants.WebFormsMasterPageMarkupFileExtension, StringComparison.InvariantCultureIgnoreCase))
                {
                    newRelativePath = Path.Combine(Constants.RazorLayoutDirectoryName, newRelativePath);
                }
                else if (RelativePath.EndsWith(Constants.WebFormsControlMarkupFileExtenion, StringComparison.InvariantCultureIgnoreCase))
                {
                    newRelativePath = Path.Combine(Constants.RazorComponentDirectoryName, newRelativePath);
                }
                else
                {
                    // Default action: file type is not supported. Set newRelativePath to null to
                    // prevent file creation.
                    newRelativePath = null;
                }

                DoCleanUp();
                LogEnd();

                if (newRelativePath != null)
                {
                    var fileInformation = new FileInformation(FilePathHelper.RemoveDuplicateDirectories(newRelativePath),
                                                              Encoding.UTF8.GetBytes(contents));
                    result.Add(fileInformation);
                }
            }
            catch (Exception e)
            {
                LogHelper.LogError(e, $"{Rules.Config.Constants.WebFormsErrorTag}Error migrating view file {FullPath}. A new file could not be generated.");
            }

            return(Task.FromResult((IEnumerable <FileInformation>)result));
        }