public void TestAddCommentBefore()
        {
            // Arrange
              var docElement = new ZptXmlElement(_document.DocumentElement,
                                       _sourceFile, Mock.Of<IZptDocument>());
              var sut = docElement.GetChildElements()[1].GetChildElements()[0].GetChildElements().First();
              string expectedResult = @"<html xmlns:custom=""http://ns.csf-dev.com/custom"">
              <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());
        }
        public void TestToString()
        {
            // Arrange
              var sut = new ZptXmlElement(_document.DocumentElement,
                               _sourceFile, Mock.Of<IZptDocument>());

              // Act
              var result = sut.ToString();

              // Assert
              Assert.AreEqual(_xmlSource, result);
        }
        public void TestSetAttributeWithNamespace()
        {
            // Arrange
              var sut = new ZptXmlElement(_document.DocumentElement.ChildNodes[1].FirstChild.FirstChild,
                                  _sourceFile, Mock.Of<IZptDocument>());

              // Act
              sut.SetAttribute(new ZptNamespace(prefix: "ns", uri: "http://ns.csf-dev.com/custom2"), "foo", "bar");

              // Assert
              var expectedDom = @"<html xmlns:custom=""http://ns.csf-dev.com/custom"">
              <head>
            <title>Document title</title>
              </head>
              <body>
            <header>
              <div custom:parent_attrib=""Attribute value one"" class=""class_one class_two"" ns:foo=""bar"" xmlns:ns=""http://ns.csf-dev.com/custom2"">
            <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,
                      new ZptXmlElement(_document.DocumentElement, _sourceFile, Mock.Of<IZptDocument>()).ToString(),
                      "Correct modified XML");
        }
        public void TestSearchChildrenByAttribute()
        {
            // Arrange
              var sut = new ZptXmlElement(_document.DocumentElement,
                                  _sourceFile, Mock.Of<IZptDocument>());

              // Act
              var results = sut.SearchChildrenByAttribute(new ZptNamespace(uri: "http://ns.csf-dev.com/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 ZptXmlElement(_document.DocumentElement.ChildNodes[1].FirstChild.FirstChild,
                                  _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 xmlns:custom=""http://ns.csf-dev.com/custom"">
              <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,
                      new ZptXmlElement(_document.DocumentElement, _sourceFile, Mock.Of<IZptDocument>()).ToString(),
                      "Correct modified XML");
        }
        public void TestReplaceChildrenWithStringStructure()
        {
            // Arrange
              var sut = new ZptXmlElement(_document.DocumentElement.ChildNodes[1].FirstChild.FirstChild,
                                  _sourceFile, Mock.Of<IZptDocument>());

              // Act
              sut.ReplaceChildrenWith("<p>Replacement <strong>element</strong></p>", true);

              // Assert
              var expectedDom = @"<html xmlns:custom=""http://ns.csf-dev.com/custom"">
              <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,
                      new ZptXmlElement(_document.DocumentElement, _sourceFile, Mock.Of<IZptDocument>()).ToString(),
                      "Correct modified XML");
        }
        public void TestOmit()
        {
            // Arrange
              var sut = new ZptXmlElement(_document.DocumentElement.ChildNodes[1].FirstChild.FirstChild,
                                   _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 xmlns:custom=""http://ns.csf-dev.com/custom"">
              <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,
                      new ZptXmlElement(_document.DocumentElement, _sourceFile, Mock.Of<IZptDocument>()).ToString(),
                      "Correct modified XML");
        }
        public void TestIsInNamespace()
        {
            // Arrange
              var doc = new XmlDocument();
              doc.LoadXml(@"<foo xmlns:ns=""http://ns.csf-dev.com/sample""><ns:bar /></foo>");

              var sut = new ZptXmlElement(doc.DocumentElement.FirstChild, _sourceFile, Mock.Of<IZptDocument>(), isRoot: true);

              // Act
              var result = sut.IsInNamespace(new ZptNamespace(prefix: "ns", uri: "http://ns.csf-dev.com/sample"));

              // Assert
              Assert.IsTrue(result);
        }
        public void TestIsInNamespaceDefault()
        {
            // Arrange
              var doc = new XmlDocument();
              doc.LoadXml("<foo><bar /></foo>");

              var sut = new ZptXmlElement(doc.DocumentElement.FirstChild, _sourceFile, Mock.Of<IZptDocument>(), isRoot: true);

              // Act
              var result = sut.IsInNamespace(ZptNamespace.Default);

              // Assert
              Assert.IsTrue(result);
        }
        public void TestInsertBefore()
        {
            // Arrange
              var sut = new ZptXmlElement(_document.DocumentElement.ChildNodes[1].FirstChild.FirstChild.FirstChild,
                                  _sourceFile, Mock.Of<IZptDocument>());
              var replacementXml = "<li>New element</li>";
              var secondDocument = new XmlDocument();
              secondDocument.LoadXml(replacementXml);
              var secondElement = new ZptXmlElement(secondDocument.DocumentElement,
                                             _sourceFile, Mock.Of<IZptDocument>());

              var referenceElement = new ZptXmlElement(_document.DocumentElement.ChildNodes[1].FirstChild.FirstChild.FirstChild.ChildNodes[1],
                                                _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 xmlns:custom=""http://ns.csf-dev.com/custom"">
              <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,
                      new ZptXmlElement(_document.DocumentElement, _sourceFile, Mock.Of<IZptDocument>()).ToString(),
                      "Correct modified XML");
        }
        public void TestGetChildElements()
        {
            // Arrange
              var sut = new ZptXmlElement(_document.DocumentElement,
                                _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 ZptXmlElement(_document.DocumentElement.ChildNodes[1].FirstChild.FirstChild,
                                  _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 ns, string name, string expectedName, string expectedValue)
        {
            // Arrange
              var sut = new ZptXmlElement(_document.DocumentElement.ChildNodes[1].FirstChild.FirstChild,
                                  _sourceFile, Mock.Of<IZptDocument>());

              // Act
              var result = sut.GetAttribute(new ZptNamespace(uri: ns), name);

              // Assert
              Assert.NotNull(result, "Result nullability");
              Assert.AreEqual(expectedName, result.Name, "Result name");
              Assert.AreEqual(expectedValue, result.Value, "Result value");
        }
Exemple #14
0
        /// <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 xpath = String.Format("//*[@{0}:{1}]",
                                ZptConstants.Metal.Namespace.Prefix,
                                ZptConstants.Metal.DefineMacroAttribute);

              var nsManager = new XmlNamespaceManager(this.Document.CreateNavigator().NameTable);
              nsManager.AddNamespace(ZptConstants.Metal.Namespace.Prefix, ZptConstants.Metal.Namespace.Uri);

              var output = this.Document.DocumentElement
            .SelectNodes(xpath, nsManager)
            .Cast<XmlNode>()
            .Select(x => {
              var element = new ZptXmlElement(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);
        }