Example #1
0
        public static IEnumerable <string> ConvertFiles(IEnumerable <string> filesToConvert)
        {
            var parser = new WebFormsParser(new WebFormsNodeFactory(),
                                            new WebFormsNodeFilterProvider(new WebFormsCodeGroupFactory()));
            var renderer  = new RazorViewRenderer(new RazorNodeRendererProvider());
            var converter =
                new WebFormsToRazorConverter(new RazorNodeConverterProvider(new RazorDirectiveNodeFactory(),
                                                                            new RazorSectionNodeFactory(),
                                                                            new RazorCodeNodeFactory(),
                                                                            new RazorTextNodeFactory(),
                                                                            new RazorCommentNodeFactory(),
                                                                            new RazorExpressionNodeFactory(),
                                                                            new ContentTagConverterConfiguration()));
            var convertedFiles = new List <string>();

            foreach (var file in filesToConvert)
            {
                var webFormsPageSource = File.ReadAllText(file, Encoding.UTF8);
                var webFormsDocument   = parser.Parse(webFormsPageSource);
                var razorDom           = converter.Convert(webFormsDocument);
                var razorPage          = renderer.Render(razorDom);
                var outputFileName     = Path.Combine(Path.GetDirectoryName(file), Path.GetFileNameWithoutExtension(file) + ".cshtml");
                File.WriteAllText(outputFileName, razorPage, Encoding.UTF8);
                convertedFiles.Add(outputFileName);
            }
            return(convertedFiles);
        }
Example #2
0
        static async Task <CreateEditorResult> CreateEditor(string text, string extension)
        {
            string          editorText;
            TestViewContent sev;
            string          parsedText;
            int             cursorPosition = text.IndexOf('$');
            int             endPos         = text.IndexOf('$', cursorPosition + 1);

            if (endPos == -1)
            {
                parsedText = editorText = text.Substring(0, cursorPosition) + text.Substring(cursorPosition + 1);
            }
            else
            {
                parsedText     = text.Substring(0, cursorPosition) + new string (' ', endPos - cursorPosition) + text.Substring(endPos + 1);
                editorText     = text.Substring(0, cursorPosition) + text.Substring(cursorPosition + 1, endPos - cursorPosition - 1) + text.Substring(endPos + 1);
                cursorPosition = endPos - 1;
            }

            var project = Services.ProjectService.CreateDotNetProject("C#");

            project.References.Add(ProjectReference.CreateAssemblyReference("System"));
            project.References.Add(ProjectReference.CreateAssemblyReference("System.Web"));
            project.FileName = UnitTests.TestBase.GetTempFile(".csproj");
            string file = UnitTests.TestBase.GetTempFile(extension);

            project.AddFile(file);

            sev                = new TestViewContent();
            sev.Project        = project;
            sev.ContentName    = file;
            sev.Text           = editorText;
            sev.CursorPosition = cursorPosition;

            var tww = new TestWorkbenchWindow();

            tww.ViewContent = sev;

            var doc = new TestDocument(tww);

            doc.Editor.FileName = sev.ContentName;
            var parser  = new WebFormsParser();
            var options = new ParseOptions {
                Project  = project,
                FileName = sev.ContentName,
                Content  = new StringTextSource(parsedText)
            };
            var parsedDoc = await parser.Parse(options, default(CancellationToken)) as WebFormsParsedDocument;

            doc.HiddenParsedDocument = parsedDoc;

            return(new CreateEditorResult {
                Extension = new WebFormsTestingEditorExtension(doc),
                EditorText = editorText,
                ViewContent = sev
            });
        }
 public WebFormsParserFilteringTests()
 {
     filterProviderMock    = new Mock <IWebFormsNodeFilterProvider>();
     postprocessingFilters = new List <IWebFormsNodeFilter>();
     filterProviderMock.Setup(fp => fp.Filters).Returns(postprocessingFilters);
     filterMock = new Mock <IWebFormsNodeFilter>();
     postprocessingFilters.Add(filterMock.Object);
     parser = new WebFormsParser(new WebFormsNodeFactory(), filterProviderMock.Object);
 }
Example #4
0
        static async Task <CreateEditorResult> CreateEditor(string text, string extension)
        {
            string          editorText;
            TestViewContent sev;
            string          parsedText;
            int             cursorPosition = text.IndexOf('$');
            int             endPos         = text.IndexOf('$', cursorPosition + 1);

            if (endPos == -1)
            {
                parsedText = editorText = text.Substring(0, cursorPosition) + text.Substring(cursorPosition + 1);
            }
            else
            {
                parsedText     = text.Substring(0, cursorPosition) + new string (' ', endPos - cursorPosition) + text.Substring(endPos + 1);
                editorText     = text.Substring(0, cursorPosition) + text.Substring(cursorPosition + 1, endPos - cursorPosition - 1) + text.Substring(endPos + 1);
                cursorPosition = endPos - 1;
            }

            var project = Services.ProjectService.CreateDotNetProject("C#");

            project.References.Add(ProjectReference.CreateAssemblyReference("System"));
            project.References.Add(ProjectReference.CreateAssemblyReference("System.Web"));
            project.FileName = UnitTests.TestBase.GetTempFile(".csproj");
            string file = UnitTests.TestBase.GetTempFile(extension);

            project.AddFile(file);

            sev = new TestViewContent();
            await sev.Initialize(new FileDescriptor (file, null, project), null);

            sev.Text           = editorText;
            sev.CursorPosition = cursorPosition;

            var parser  = new WebFormsParser();
            var options = new ParseOptions {
                Project  = project,
                FileName = file,
                Content  = new StringTextSource(parsedText)
            };

            sev.Editor.FileName = sev.FilePath;

            var parsedDoc = await parser.Parse(options, default(CancellationToken)) as WebFormsParsedDocument;

            var documentContext = sev.GetContent <RoslynDocumentContext> ();

            documentContext.SetParsedDocument(parsedDoc);

            return(new CreateEditorResult {
                Extension = new WebFormsTestingEditorExtension(sev.Editor, documentContext),
                EditorText = editorText,
                ViewContent = sev
            });
        }
        static WebFormsTestingEditorExtension CreateEditor(string text, string extension, out string editorText, out TestViewContent sev)
        {
            string parsedText;
            int    cursorPosition = text.IndexOf('$');
            int    endPos         = text.IndexOf('$', cursorPosition + 1);

            if (endPos == -1)
            {
                parsedText = editorText = text.Substring(0, cursorPosition) + text.Substring(cursorPosition + 1);
            }
            else
            {
                parsedText     = text.Substring(0, cursorPosition) + new string (' ', endPos - cursorPosition) + text.Substring(endPos + 1);
                editorText     = text.Substring(0, cursorPosition) + text.Substring(cursorPosition + 1, endPos - cursorPosition - 1) + text.Substring(endPos + 1);
                cursorPosition = endPos - 1;
            }

            var project = new AspNetAppProject("C#");

            project.References.Add(new ProjectReference(ReferenceType.Package, "System"));
            project.References.Add(new ProjectReference(ReferenceType.Package, "System.Web"));
            project.FileName = UnitTests.TestBase.GetTempFile(".csproj");
            string file = UnitTests.TestBase.GetTempFile(extension);

            project.AddFile(file);

            var pcw = TypeSystemService.LoadProject(project);

            TypeSystemService.ForceUpdate(pcw);
            pcw.ReconnectAssemblyReferences();

            sev                = new TestViewContent();
            sev.Project        = project;
            sev.ContentName    = file;
            sev.Text           = editorText;
            sev.CursorPosition = cursorPosition;

            var tww = new TestWorkbenchWindow();

            tww.ViewContent = sev;

            var doc = new TestDocument(tww);

            doc.Editor.Document.FileName = sev.ContentName;
            var parser    = new WebFormsParser();
            var parsedDoc = (WebFormsParsedDocument)parser.Parse(false, sev.ContentName, new StringReader(parsedText), project);

            doc.HiddenParsedDocument = parsedDoc;

            return(new WebFormsTestingEditorExtension(doc));
        }
Example #6
0
        public virtual WebFormContainer ParseString(string input, string filePath)
        {
            WebFormContainer returnValue;
            var            nodeFactory          = new WebFormsNodeFactory();
            var            codeGroupNodeFactory = new WebFormsCodeGroupFactory();
            var            nodeFilterProvider   = new WebFormsNodeFilterProvider(codeGroupNodeFactory);
            WebFormsParser telerikParser        = new WebFormsParser(nodeFactory, nodeFilterProvider);
            var            document             = telerikParser.Parse(input);

            var directiveNode = document.RootNode.Children.FirstOrDefault(i => i is DirectiveNode && !i.Attributes.ContainsKey("Register"));

            if (directiveNode == null)
            {
                throw new ArgumentException("Failed to find directive node");
            }
            returnValue = new WebFormContainer
            {
                CodeBehindFile = directiveNode.Attributes["codebehind"],
                ClassFullName  = directiveNode.Attributes["inherits"],
                FilePath       = filePath
            };
            if (directiveNode.Attributes.ContainsKey("page"))
            {
                returnValue.ContainerType = EnumWebFormContainerType.WebPage;
            }
            else if (directiveNode.Attributes.ContainsKey("master"))
            {
                returnValue.ContainerType = EnumWebFormContainerType.MasterPage;
            }
            else if (directiveNode.Attributes.ContainsKey("control"))
            {
                returnValue.ContainerType = EnumWebFormContainerType.UserControl;
            }
            else
            {
                throw new Exception("Unrecognized directive");
            }
            var controlList = document.RootNode.Children.Where(i => i is ServerControlNode);

            foreach (ServerControlNode control in controlList)
            {
                this.LoadControl(control, returnValue);
            }
            return(returnValue);
        }
 public WebFormsParserTests()
 {
     filterProviderMock = new Mock <IWebFormsNodeFilterProvider>();
     filterProviderMock.Setup(fp => fp.Filters).Returns(new IWebFormsNodeFilter[] { });
     parser = new WebFormsParser(new WebFormsNodeFactory(), filterProviderMock.Object);
 }