public void FlattenFlattensStartAndEndTagTagHelpers() { // Arrange var spanFactory = SpanFactory.CreateCsHtml(); var blockFactory = new BlockFactory(spanFactory); var tagHelper = (TagHelperBlock)blockFactory.TagHelperBlock( tagName: "div", tagMode: TagMode.StartTagAndEndTag, start: SourceLocation.Zero, startTag: blockFactory.MarkupTagBlock("<div>"), children: new SyntaxTreeNode[0], endTag: blockFactory.MarkupTagBlock("</div>")); spanFactory.Reset(); var expectedStartTag = spanFactory.Markup("<div>"); var expectedEndTag = spanFactory.Markup("</div>"); // Act var flattenedNodes = tagHelper.Flatten(); // Assert Assert.Collection( flattenedNodes, first => { Assert.True(first.EquivalentTo(expectedStartTag)); }, second => { Assert.True(second.EquivalentTo(expectedEndTag)); }); }
public void TreesAreDifferentReturnsFalseForAddedContent(string content) { // Arrange var factory = SpanFactory.CreateCsHtml(); var blockFactory = new BlockFactory(factory); var original = new MarkupBlock( blockFactory.MarkupTagBlock("<p>"), blockFactory.TagHelperBlock( tagName: "div", tagMode: TagMode.StartTagAndEndTag, start: new SourceLocation(3, 0, 3), startTag: blockFactory.MarkupTagBlock("<div>"), children: new SyntaxTreeNode[] { factory.Markup($"{Environment.NewLine}{Environment.NewLine}") }, endTag: blockFactory.MarkupTagBlock("</div>")), blockFactory.MarkupTagBlock("</p>")); factory.Reset(); var modified = new MarkupBlock( blockFactory.MarkupTagBlock("<p>"), blockFactory.TagHelperBlock( tagName: "div", tagMode: TagMode.StartTagAndEndTag, start: new SourceLocation(3, 0, 3), startTag: blockFactory.MarkupTagBlock("<div>"), children: new SyntaxTreeNode[] { factory.Markup($"{Environment.NewLine}{content}{Environment.NewLine}") }, endTag: blockFactory.MarkupTagBlock("</div>")), blockFactory.MarkupTagBlock("</p>")); original.LinkNodes(); modified.LinkNodes(); var oldBuffer = new StringTextBuffer($"<p><div>{Environment.NewLine}{Environment.NewLine}</div></p>"); var newBuffer = new StringTextBuffer( $"<p><div>{Environment.NewLine}{content}{Environment.NewLine}</div></p>"); // Act var treesAreDifferent = BackgroundParser.TreesAreDifferent( original, modified, new[] { new TextChange( position: 8 + Environment.NewLine.Length, oldLength: 0, oldBuffer: oldBuffer, newLength: content.Length, newBuffer: newBuffer) }); // Assert Assert.False(treesAreDifferent); }
public void Rewrite_CanHandleInvalidChildrenWithWhitespace() { // Arrange var factory = CreateDefaultSpanFactory(); var blockFactory = new BlockFactory(factory); var documentContent = $"<p>{Environment.NewLine} <strong>{Environment.NewLine} Hello" + $"{Environment.NewLine} </strong>{Environment.NewLine}</p>"; var newLineLength = Environment.NewLine.Length; var expectedErrors = new[] { new RazorError( RazorResources.FormatTagHelperParseTreeRewriter_InvalidNestedTag("strong", "p", "br"), absoluteIndex: 7 + newLineLength, lineIndex: 1, columnIndex: 9 + newLineLength, length: 8), new RazorError( RazorResources.FormatTagHelperParseTreeRewriter_CannotHaveNonTagContent("p", "br"), absoluteIndex: 23 + newLineLength * 2, lineIndex: 2, columnIndex: 23 + newLineLength * 2, length: 5), new RazorError( RazorResources.FormatTagHelperParseTreeRewriter_InvalidNestedTag("strong", "p", "br"), absoluteIndex: 32 + newLineLength * 3, lineIndex: 3, columnIndex: 32 + newLineLength * 3, length: 9), }; var expectedOutput = new MarkupBlock( new MarkupTagHelperBlock("p", factory.Markup(Environment.NewLine + " "), blockFactory.MarkupTagBlock("<strong>"), factory.Markup(Environment.NewLine + " Hello" + Environment.NewLine + " "), blockFactory.MarkupTagBlock("</strong>"), factory.Markup(Environment.NewLine))); var descriptors = new TagHelperDescriptor[] { new TagHelperDescriptor { TagName = "p", TypeName = "PTagHelper", AssemblyName = "SomeAssembly", AllowedChildren = new[] { "br" }, } }; var descriptorProvider = new TagHelperDescriptorProvider(descriptors); // Act & Assert EvaluateData(descriptorProvider, documentContent, expectedOutput, expectedErrors); }
public void Rewrite_RecoversWhenRequiredAttributeMismatchAndRestrictedChildren() { // Arrange var factory = CreateDefaultSpanFactory(); var blockFactory = new BlockFactory(factory); var documentContent = "<strong required><strong></strong></strong>"; var expectedErrors = new[] { new RazorError( RazorResources.FormatTagHelperParseTreeRewriter_InvalidNestedTag("strong", "strong", "br"), absoluteIndex: 17, lineIndex: 0, columnIndex: 17, length: 8), new RazorError( RazorResources.FormatTagHelperParseTreeRewriter_InvalidNestedTag("strong", "strong", "br"), absoluteIndex: 25, lineIndex: 0, columnIndex: 25, length: 9), }; var expectedOutput = new MarkupBlock( new MarkupTagHelperBlock("strong", new List<KeyValuePair<string, SyntaxTreeNode>> { new KeyValuePair<string, SyntaxTreeNode>("required", null) }, blockFactory.MarkupTagBlock("<strong>"), blockFactory.MarkupTagBlock("</strong>"))); var descriptors = new TagHelperDescriptor[] { new TagHelperDescriptor { TagName = "strong", TypeName = "StrongTagHelper", AssemblyName = "SomeAssembly", RequiredAttributes = new[] { "required" }, AllowedChildren = new[] { "br" } } }; var descriptorProvider = new TagHelperDescriptorProvider(descriptors); // Act & Assert EvaluateData(descriptorProvider, documentContent, expectedOutput, expectedErrors); }
public void FlattenFlattensSelfClosingTagHelpers() { // Arrange var spanFactory = SpanFactory.CreateCsHtml(); var blockFactory = new BlockFactory(spanFactory); var tagHelper = (TagHelperBlock)blockFactory.TagHelperBlock( tagName: "input", tagMode: TagMode.SelfClosing, start: SourceLocation.Zero, startTag: blockFactory.MarkupTagBlock("<input />"), children: new SyntaxTreeNode[0], endTag: null); spanFactory.Reset(); var expectedNode = spanFactory.Markup("<input />"); // Act var flattenedNodes = tagHelper.Flatten(); // Assert var node = Assert.Single(flattenedNodes); Assert.True(node.EquivalentTo(expectedNode)); }
protected ParserTestBase() { Factory = CreateSpanFactory(); BlockFactory = CreateBlockFactory(); }
public void Rewrite_AllowsCompatibleTagStructures( string documentContent, TagStructure structure1, TagStructure structure2, MarkupBlock expectedOutput) { // Arrange var factory = CreateDefaultSpanFactory(); var blockFactory = new BlockFactory(factory); var descriptors = new TagHelperDescriptor[] { new TagHelperDescriptor { TagName = "input", TypeName = "InputTagHelper1", AssemblyName = "SomeAssembly", TagStructure = structure1 }, new TagHelperDescriptor { TagName = "input", TypeName = "InputTagHelper2", AssemblyName = "SomeAssembly", TagStructure = structure2 } }; var descriptorProvider = new TagHelperDescriptorProvider(descriptors); // Act & Assert EvaluateData(descriptorProvider, documentContent, expectedOutput, expectedErrors: new RazorError[0]); }
public void Rewrite_CreatesErrorForInconsistentTagStructures() { // Arrange var factory = CreateDefaultSpanFactory(); var blockFactory = new BlockFactory(factory); var expectedError = new RazorError( RazorResources.FormatTagHelperParseTreeRewriter_InconsistentTagStructure( "InputTagHelper1", "InputTagHelper2", "input", nameof(TagHelperDescriptor.TagStructure)), absoluteIndex: 0, lineIndex: 0, columnIndex: 0, length: 7); var documentContent = "<input>"; var expectedOutput = new MarkupBlock(new MarkupTagHelperBlock("input", TagMode.StartTagOnly)); var descriptors = new TagHelperDescriptor[] { new TagHelperDescriptor { TagName = "input", TypeName = "InputTagHelper1", AssemblyName = "SomeAssembly", TagStructure = TagStructure.WithoutEndTag }, new TagHelperDescriptor { TagName = "input", TypeName = "InputTagHelper2", AssemblyName = "SomeAssembly", TagStructure = TagStructure.NormalOrSelfClosing } }; var descriptorProvider = new TagHelperDescriptorProvider(descriptors); // Act & Assert EvaluateData(descriptorProvider, documentContent, expectedOutput, expectedErrors: new[] { expectedError }); }
public void Rewrite_CreatesErrorForWithoutEndTagTagStructureForEndTags() { // Arrange var factory = CreateDefaultSpanFactory(); var blockFactory = new BlockFactory(factory); var expectedError = new RazorError( RazorResources.FormatTagHelperParseTreeRewriter_EndTagTagHelperMustNotHaveAnEndTag( "input", "InputTagHelper", TagStructure.WithoutEndTag), absoluteIndex: 0, lineIndex: 0, columnIndex: 0, length: 8); var documentContent = "</input>"; var expectedOutput = new MarkupBlock(blockFactory.MarkupTagBlock("</input>")); var descriptors = new TagHelperDescriptor[] { new TagHelperDescriptor { TagName = "input", TypeName = "InputTagHelper", AssemblyName = "SomeAssembly", TagStructure = TagStructure.WithoutEndTag } }; var descriptorProvider = new TagHelperDescriptorProvider(descriptors); // Act & Assert EvaluateData(descriptorProvider, documentContent, expectedOutput, expectedErrors: new[] { expectedError }); }