public void TestAddCommentBefore() { // Arrange var docElement = new ZptHtmlElement(_document.DocumentNode, _sourceFile, Mock.Of<IZptDocument>()); var sut = docElement.GetChildElements()[0].GetChildElements()[1].GetChildElements()[0].GetChildElements().First(); string expectedResult = @"<html> <head> <title>Document title</title> </head> <body> <header> <!--Foo bar baz--><div custom:parent_attrib=""Attribute value one"" class=""class_one class_two""> <ul> <li custom:child_attrib=""foo"">Foo content</li> <li custom:child_attrib=""bar"">Bar content</li> <li custom:child_attrib=""baz"">Baz content</li> </ul> </div> Page header </header> <section> <header> <h1 id=""page_heading"">Page heading</h1> </header> <p>A paragraph of content</p> </section> <footer>Page footer</footer> </body> </html>"; // Act sut.AddCommentBefore("Foo bar baz"); // Assert Assert.AreEqual(expectedResult, docElement.ToString()); }
/// <summary> /// Gets a collection of elements in the document which are defined as METAL macros. /// </summary> /// <returns>Elements representing the METAL macros.</returns> public override ITalesPathHandler GetMacros() { var output = this.Document .DocumentNode .DescendantsAndSelf() .Where(ele => ele.Attributes.Any(attr => attr.Name == String.Format("{0}:{1}", ZptConstants.Metal.Namespace.Prefix, ZptConstants.Metal.DefineMacroAttribute))) .Select(x => { var element = new ZptHtmlElement(x, this.SourceFile, this, isImported: true); var context = new RenderingContext(Model.Empty, Model.Empty, element, GetDefaultOptions()); return new Metal.MetalMacro(context.GetMetalAttribute(ZptConstants.Metal.DefineMacroAttribute).Value, element); }) .ToArray(); return new CSF.Zpt.Metal.MetalMacroCollection(output); }
public void TestToString() { // Arrange var sut = new ZptHtmlElement(_document.DocumentNode, _sourceFile, Mock.Of<IZptDocument>()); // Act var result = sut.ToString(); // Assert Assert.AreEqual(_htmlSource, result); }
public void TestSetAttributeWithNamespace() { // Arrange var sut = new ZptHtmlElement(_document.DocumentNode.FirstChild.ChildNodes[3].ChildNodes[1].ChildNodes[1], _sourceFile, Mock.Of<IZptDocument>()); // Act sut.SetAttribute(new ZptNamespace(prefix: "ns"), "foo", "bar"); // Assert var expectedDom = @"<html> <head> <title>Document title</title> </head> <body> <header> <div custom:parent_attrib=""Attribute value one"" class=""class_one class_two"" ns:foo=""bar""> <ul> <li custom:child_attrib=""foo"">Foo content</li> <li custom:child_attrib=""bar"">Bar content</li> <li custom:child_attrib=""baz"">Baz content</li> </ul> </div> Page header </header> <section> <header> <h1 id=""page_heading"">Page heading</h1> </header> <p>A paragraph of content</p> </section> <footer>Page footer</footer> </body> </html>"; Assert.AreEqual(expectedDom, _document.DocumentNode.OuterHtml, "Correct modified HTML"); }
public void TestSearchChildrenByAttribute() { // Arrange var sut = new ZptHtmlElement(_document.DocumentNode, _sourceFile, Mock.Of<IZptDocument>()); // Act var results = sut.SearchChildrenByAttribute(new ZptNamespace(prefix: "custom"), "parent_attrib"); // Assert Assert.NotNull(results, "Result nullability"); Assert.AreEqual(1, results.Length, "Count of results"); Assert.AreEqual("div", results[0].Name, "Name of found element"); }
public void TestReplaceWithStringStructure() { // Arrange var sut = new ZptHtmlElement(_document.DocumentNode.FirstChild.ChildNodes[3].ChildNodes[1].ChildNodes[1], _sourceFile, Mock.Of<IZptDocument>()); // Act var result = sut.ReplaceWith("<p>Replacement <strong>element</strong></p>", true); // Assert Assert.NotNull(result, "Result nullability"); Assert.AreEqual(1, result.Length, "Count of results"); var expectedDom = @"<html> <head> <title>Document title</title> </head> <body> <header> <p>Replacement <strong>element</strong></p> Page header </header> <section> <header> <h1 id=""page_heading"">Page heading</h1> </header> <p>A paragraph of content</p> </section> <footer>Page footer</footer> </body> </html>"; Assert.AreEqual(expectedDom, _document.DocumentNode.OuterHtml, "Correct modified HTML"); }
public void TestReplaceWith() { // Arrange var sut = new ZptHtmlElement(_document.DocumentNode.FirstChild.ChildNodes[3].ChildNodes[1].ChildNodes[1], _sourceFile, Mock.Of<IZptDocument>()); var replacementHtml = "<p>Replacement element</p>"; var secondDocument = new HtmlDocument(); secondDocument.LoadHtml(replacementHtml); var secondElement = new ZptHtmlElement(secondDocument.DocumentNode.FirstChild, _sourceFile, Mock.Of<IZptDocument>()); // Act var result = sut.ReplaceWith(secondElement); // Assert Assert.NotNull(result, "Result nullability"); Assert.AreEqual("p", result.Name, "Result name"); var expectedDom = @"<html> <head> <title>Document title</title> </head> <body> <header> <p>Replacement element</p> Page header </header> <section> <header> <h1 id=""page_heading"">Page heading</h1> </header> <p>A paragraph of content</p> </section> <footer>Page footer</footer> </body> </html>"; Assert.AreEqual(expectedDom, _document.DocumentNode.OuterHtml, "Correct modified HTML"); }
public void TestReplaceChildrenWithStringStructure() { // Arrange var sut = new ZptHtmlElement(_document.DocumentNode.FirstChild.ChildNodes[3].ChildNodes[1].ChildNodes[1], _sourceFile, Mock.Of<IZptDocument>()); // Act sut.ReplaceChildrenWith("<p>Replacement <strong>element</strong></p>", true); // Assert var expectedDom = @"<html> <head> <title>Document title</title> </head> <body> <header> <div custom:parent_attrib=""Attribute value one"" class=""class_one class_two""><p>Replacement <strong>element</strong></p></div> Page header </header> <section> <header> <h1 id=""page_heading"">Page heading</h1> </header> <p>A paragraph of content</p> </section> <footer>Page footer</footer> </body> </html>"; Assert.AreEqual(expectedDom, _document.DocumentNode.OuterHtml, "Correct modified HTML"); }
public void TestOmit() { // Arrange var sut = new ZptHtmlElement(_document.DocumentNode.FirstChild.ChildNodes[3].ChildNodes[1].ChildNodes[1], _sourceFile, Mock.Of<IZptDocument>()); // Act var result = sut.Omit(); // Assert Assert.NotNull(result, "Result nullability"); Assert.AreEqual(1, result.Length, "Result length"); var expectedDom = @"<html> <head> <title>Document title</title> </head> <body> <header> <ul> <li custom:child_attrib=""foo"">Foo content</li> <li custom:child_attrib=""bar"">Bar content</li> <li custom:child_attrib=""baz"">Baz content</li> </ul> Page header </header> <section> <header> <h1 id=""page_heading"">Page heading</h1> </header> <p>A paragraph of content</p> </section> <footer>Page footer</footer> </body> </html>"; Assert.AreEqual(expectedDom, _document.DocumentNode.OuterHtml, "Correct modified HTML"); }
public void TestIsInNamespaceDefault() { // Arrange var doc = new HtmlDocument(); doc.LoadHtml("<foo><bar /></foo>"); var sut = new ZptHtmlElement(doc.DocumentNode.FirstChild.ChildNodes[0], _sourceFile, Mock.Of<IZptDocument>(), isRoot: true); // Act var result = sut.IsInNamespace(ZptNamespace.Default); // Assert Assert.IsTrue(result); }
public void TestInsertBefore() { // Arrange var sut = new ZptHtmlElement(_document.DocumentNode.FirstChild.ChildNodes[3].ChildNodes[1].ChildNodes[1].ChildNodes[1], _sourceFile, Mock.Of<IZptDocument>()); var replacementHtml = "<li>New element</li>"; var secondDocument = new HtmlDocument(); secondDocument.LoadHtml(replacementHtml); var secondElement = new ZptHtmlElement(secondDocument.DocumentNode.FirstChild, _sourceFile, Mock.Of<IZptDocument>()); var referenceElement = new ZptHtmlElement(_document.DocumentNode.FirstChild.ChildNodes[3].ChildNodes[1].ChildNodes[1].ChildNodes[1].ChildNodes[3], _sourceFile, Mock.Of<IZptDocument>()); // Act var result = sut.InsertBefore(referenceElement, secondElement); // Assert Assert.NotNull(result, "Result nullability"); Assert.AreEqual("li", result.Name, "Result name"); var expectedDom = @"<html> <head> <title>Document title</title> </head> <body> <header> <div custom:parent_attrib=""Attribute value one"" class=""class_one class_two""> <ul> <li custom:child_attrib=""foo"">Foo content</li> <li>New element</li> <li custom:child_attrib=""bar"">Bar content</li> <li custom:child_attrib=""baz"">Baz content</li> </ul> </div> Page header </header> <section> <header> <h1 id=""page_heading"">Page heading</h1> </header> <p>A paragraph of content</p> </section> <footer>Page footer</footer> </body> </html>"; Assert.AreEqual(expectedDom, _document.DocumentNode.OuterHtml, "Correct modified HTML"); }
public void TestGetChildElements() { // Arrange var sut = new ZptHtmlElement(_document.DocumentNode.FirstChild, _sourceFile, Mock.Of<IZptDocument>()); // Act var result = sut.GetChildElements(); // Assert Assert.AreEqual(2, result.Length, "Count of child element"); Assert.AreEqual("head", result[0].Name, "Name of first child"); Assert.AreEqual("body", result[1].Name, "Name of second child"); }
public void TestGetAttributes() { // Arrange var sut = new ZptHtmlElement(_document.DocumentNode.FirstChild.ChildNodes[3].ChildNodes[1].ChildNodes[1], _sourceFile, Mock.Of<IZptDocument>()); // Act var results = sut.GetAttributes(); // Assert Assert.NotNull(results, "Result nullability"); Assert.AreEqual(2, results.Length, "Count of results"); Assert.AreEqual("custom:parent_attrib", results[0].Name, "Attribute name one"); Assert.AreEqual("class", results[1].Name, "Attribute name two"); }
public void TestGetAttribute(string prefix, string name, string expectedName, string expectedValue) { // Arrange var sut = new ZptHtmlElement(_document.DocumentNode.FirstChild.ChildNodes[3].ChildNodes[1].ChildNodes[1], _sourceFile, Mock.Of<IZptDocument>()); // Act var result = sut.GetAttribute(new ZptNamespace(prefix: prefix), name); // Assert Assert.NotNull(result, "Result nullability"); Assert.AreEqual(expectedName, result.Name, "Result name"); Assert.AreEqual(expectedValue, result.Value, "Result value"); }