コード例 #1
0
        public void TestAdd()
        {
            HContainer container = new HElement("div");

            // Nothing appening
            container.Add(null);

            Assert.Equal(0, container.Nodes().Count());

            container.Add(123.45);
            String s = (123.45).ToString();

            Assert.Equal(1, container.Nodes().Count());
            Assert.Equal("<div>" + s + "</div>", container.ToString());

            container.Add(null);
            Assert.Equal(1, container.Nodes().Count());
            Assert.Equal("<div>" + s + "</div>", container.ToString());

            container.Add("Other Content");
            Assert.Equal(1, container.Nodes().Count());
            Assert.Equal("<div>" + s + "Other Content</div>", container.ToString());

            container.Add(new HElement("span", "SPAN"));
            Assert.Equal(2, container.Nodes().Count());
            Assert.Equal("<div>" + s + "Other Content<span>SPAN</span></div>", container.ToString());
        }
コード例 #2
0
ファイル: ElementTest.cs プロジェクト: ygrenier/HDocument
        public void TestReplaceWith()
        {
            var n1 = new HText("Content");
            var n2 = new HElement("span", n1);
            var n3 = new HElement("span", "Another content");
            var n4 = new HElement("div", n2, n3);

            Assert.Same(n2, n2.ReplaceWith(new HElement("div", "Div Content"), new HElement("p", "P content")));
            Assert.Equal("<div><div>Div Content</div><p>P content</p><span>Another content</span></div>", n4.ToString());

            Assert.Same(n3, n3.ReplaceWith(null));
            Assert.Equal("<div><div>Div Content</div><p>P content</p></div>", n4.ToString());

            n2 = null;
            Assert.Null(n2.ReplaceWith(new HElement("div", "Div Content"), new HElement("p", "P content")));
            Assert.Equal("<div><div>Div Content</div><p>P content</p></div>", n4.ToString());
        }
コード例 #3
0
ファイル: ElementTest.cs プロジェクト: ygrenier/HDocument
        public void TestReplaceWithElements()
        {
            var n1 = new HText("Content");
            var n2 = new HElement("span", n1);
            var n3 = new HElement("span", "Another content");
            var n4 = new HElement("div", n2, n3);

            IEnumerable <HElement> elements = new HElement[] { n2, n3 };

            Assert.Same(elements, elements.ReplaceWith(new HElement("div", "Div Content"), new HElement("p", "P content")));
            Assert.Equal("<div><div>Div Content</div><p>P content</p><div>Div Content</div><p>P content</p></div>", n4.ToString());

            n4.Descendants("div").ReplaceWith(null, null, null);
            Assert.Equal("<div><p>P content</p><p>P content</p></div>", n4.ToString());

            elements = null;
            Assert.Null(elements.ReplaceWith(new HElement("div", "Div Content"), new HElement("p", "P content")));
            Assert.Equal("<div><p>P content</p><p>P content</p></div>", n4.ToString());
        }
コード例 #4
0
ファイル: ElementTest.cs プロジェクト: ygrenier/HDocument
        public void TestPrependElement()
        {
            var element = new HElement("div", new HElement("p"));

            Assert.Same(element, element.Prepend(new HElement("span"), "test", null, new HAttribute("attr", "value")));
            Assert.Equal(3, element.Nodes().Count());
            Assert.Equal("value", element.Attr("attr"));
            Assert.Equal("<div attr=\"value\"><span></span>test<p></p></div>", element.ToString());

            element = null;
            Assert.Null(element.Prepend(new HElement("span"), "test", null, new HAttribute("attr", "value")));
        }
コード例 #5
0
ファイル: ElementTest.cs プロジェクト: ygrenier/HDocument
        public void TestUnwrapElement()
        {
            var n1   = new HElement("span", "Content");
            var root = new HElement("div", new HElement("strong", new HElement("em", n1)));

            Assert.Equal("<div><strong><em><span>Content</span></em></strong></div>", root.ToString());

            Assert.Same(n1, n1.Unwrap());
            Assert.Equal("<div><strong><span>Content</span></strong></div>", root.ToString());

            n1 = null;
            Assert.Null(n1.Unwrap());
        }
コード例 #6
0
ファイル: ElementTest.cs プロジェクト: ygrenier/HDocument
        public void TestPrependToElement()
        {
            var content = new HElement("span", "Content");
            var element = new HElement("div", "First Content");

            Assert.Same(content, content.PrependTo(element));
            Assert.Equal("<div><span>Content</span>First Content</div>", element.ToString());

            Assert.Same(content, content.PrependTo(null));

            content = null;
            Assert.Null(content.PrependTo(element));
        }
コード例 #7
0
ファイル: ElementTest.cs プロジェクト: ygrenier/HDocument
        public void TestSetTextElement()
        {
            var n1   = new HElement("div", "Content 1");
            var n2   = new HElement("div", "Content 2");
            var n3   = new HElement("div", "Content 3");
            var n4   = new HElement("div", "Content 4");
            var n5   = new HElement("div", "Content 5");
            var root = new HElement("div", n1, n2, n3, n4, n5);

            Assert.Same(root, root.Text("Test Content"));
            Assert.Equal("<div>Test Content</div>", root.ToString());

            root = null;
            Assert.Equal(null, root.Text("Other content"));
        }
コード例 #8
0
ファイル: ElementTest.cs プロジェクト: ygrenier/HDocument
        public void TestRemove()
        {
            var n1 = new HText("Content");
            var n2 = new HElement("span", n1);
            var n3 = new HElement("span", "Another content");
            var n4 = new HElement("div", n2, n3);

            var elements = new HNode[] { n3, n1 };

            Assert.Same(elements, elements.Remove());
            Assert.Equal("<div><span></span></div>", n4.ToString());

            elements = null;
            Assert.Null(elements.Remove());
        }
コード例 #9
0
        protected override string GetContents(SessionData sessionData)
        {
            // Get the default layout around the elements retrieved by GetElements()
            HElement page = MainPage.GetPage(GetElements(), nameof(Tut01) + ".cs");

            // To get the HTML-string of an HElement, call GetContent with the current session data.
            return(page.GetContent(sessionData));

#pragma warning disable CS0162
            // You might as well just use .ToString();
            return(page.ToString());

            // Or for all you fishy dudes out there: there's also this alternative
            return(page * sessionData);

#pragma warning restore CS0162
        }
コード例 #10
0
ファイル: ElementTest.cs プロジェクト: ygrenier/HDocument
        public void TestWrapElements()
        {
            var n1 = new HElement("span", "Content 1");
            var n2 = new HElement("span", "Content 2");
            var n3 = new HElement("div", n1, n2);

            Assert.Equal("<div><span>Content 1</span><span>Content 2</span></div>", n3.ToString());

            IEnumerable <HElement> elements = new HElement[] { n1, n2 };

            Assert.Same(elements, elements.Wrap(new HElement("strong", new HElement("em"))));
            Assert.Equal("<div><strong><em><span>Content 1</span></em></strong><strong><em><span>Content 2</span></em></strong></div>", n3.ToString());

            elements = null;
            Assert.Null(elements.Wrap(new HElement("strong", new HElement("em"))));
            Assert.Equal("<div><strong><em><span>Content 1</span></em></strong><strong><em><span>Content 2</span></em></strong></div>", n3.ToString());
        }
コード例 #11
0
ファイル: ElementTest.cs プロジェクト: ygrenier/HDocument
        public void TestWrapInnerElementsByCallback()
        {
            var n1 = new HElement("span", "Content 1");
            var n2 = new HElement("span", "Content 2");
            var n3 = new HElement("div", n1, n2);

            Assert.Equal("<div><span>Content 1</span><span>Content 2</span></div>", n3.ToString());

            var elements = new HElement[] { n1, n2 };

            Assert.Same(elements, elements.WrapInner((e, i) => new HElement("strong", "C" + i.ToString(), new HElement("em"))));
            Assert.Equal("<div><span><strong>C0<em>Content 1</em></strong></span><span><strong>C1<em>Content 2</em></strong></span></div>", n3.ToString());

            elements = null;
            Assert.Null(elements.WrapInner((e, i) => new HElement("strong", "C" + i.ToString(), new HElement("em"))));
            Assert.Equal("<div><span><strong>C0<em>Content 1</em></strong></span><span><strong>C1<em>Content 2</em></strong></span></div>", n3.ToString());
        }
コード例 #12
0
ファイル: ElementTest.cs プロジェクト: ygrenier/HDocument
        public void TestSetTextElements()
        {
            var n1   = new HElement("div", "Content 1");
            var n2   = new HElement("div", "Content 2");
            var n3   = new HElement("div", "Content 3");
            var n4   = new HElement("div", "Content 4");
            var n5   = new HElement("div", "Content 5");
            var root = new HElement("div", n1, n2, n3, n4, n5);

            var elements = new HElement[] { n2, null, n4 };

            Assert.Same(elements, elements.Text("Test Content"));
            Assert.Equal("<div><div>Content 1</div><div>Test Content</div><div>Content 3</div><div>Test Content</div><div>Content 5</div></div>", root.ToString());

            elements = null;
            Assert.Equal(null, elements.Text("Other content"));
        }
コード例 #13
0
ファイル: ElementTest.cs プロジェクト: ygrenier/HDocument
        public void TestUnwrapElements()
        {
            var n1   = new HElement("span", "Content");
            var n2   = new HText("Content");
            var root = new HElement("div", new HElement("a", new HElement("strong", new HElement("em", n1))), new HElement("div", n2));

            Assert.Equal("<div><a><strong><em><span>Content</span></em></strong></a><div>Content</div></div>", root.ToString());

            IEnumerable <HNode> elements = new HNode[] { n1, n2 };

            Assert.Same(elements, elements.Unwrap());
            Assert.Equal("<div><a><strong><span>Content</span></strong></a>Content</div>", root.ToString());

            elements = null;
            Assert.Null(elements.Unwrap());
        }
コード例 #14
0
ファイル: ElementTest.cs プロジェクト: ygrenier/HDocument
        public void TestPrependToElements()
        {
            var content1 = new HElement("span", "Content1");
            var content2 = new HElement("span", "Content2");
            var element  = new HElement("div", "First Content");

            IEnumerable <HNode> contents = new HNode[] { content1, null, content2 };

            Assert.Same(contents, contents.PrependTo(element));
            Assert.Equal("<div><span>Content1</span><span>Content2</span>First Content</div>", element.ToString());

            Assert.Same(contents, contents.PrependTo(null));

            contents = null;
            Assert.Null(contents.PrependTo(element));
        }
コード例 #15
0
ファイル: ElementTest.cs プロジェクト: ygrenier/HDocument
        public void TestWrapInnerElement()
        {
            var n1   = new HElement("span", "Content");
            var n2   = new HElement("span");
            var root = new HElement("div", n1, n2);

            Assert.Same(n1, n1.WrapInner(new HElement("strong", new HElement("em"))));
            Assert.Equal("<div><span><strong><em>Content</em></strong></span><span></span></div>", root.ToString());

            Assert.Same(n2, n2.WrapInner(new HElement("p", new HElement("a"))));
            Assert.Equal("<div><span><strong><em>Content</em></strong></span><span><p><a></a></p></span></div>", root.ToString());

            n1 = null;
            Assert.Null(n1.WrapInner(new HElement("strong", new HElement("em"))));
            Assert.Equal("<div><span><strong><em>Content</em></strong></span><span><p><a></a></p></span></div>", root.ToString());
        }
コード例 #16
0
ファイル: ElementTest.cs プロジェクト: ygrenier/HDocument
        public void TestWrapAll()
        {
            var n1   = new HElement("span", "Content 1");
            var n2   = new HElement("span", "Content 2");
            var n3   = new HElement("span", "Content 3");
            var n4   = new HElement("span", "Content 4");
            var root = new HElement("div", n1, n2, n3, n4);

            var elements = new HElement[] { n1, n3, n4 };

            Assert.Same(elements, elements.WrapAll(new HElement("p", new HElement("a"))));
            Assert.Equal("<div><p><a><span>Content 1</span><span>Content 3</span><span>Content 4</span></a></p><span>Content 2</span></div>", root.ToString());
        }
コード例 #17
0
ファイル: ElementTest.cs プロジェクト: ygrenier/HDocument
        public void TestReplaceAll()
        {
            var n1 = new HText("Content");
            var n2 = new HElement("span", n1);
            var n3 = new HElement("span", "Another content");
            var n4 = new HElement("div", n2, n3);

            var elements = new HElement[] { new HElement("p", "p 1"), null, new HElement("div", "div 2") };

            Assert.Same(elements, elements.ReplaceAll(n3, n2));
            Assert.Equal("<div><p>p 1</p><div>div 2</div><p>p 1</p><div>div 2</div></div>", n4.ToString());

            elements = null;
            Assert.Null(elements.ReplaceAll(n3, n2));
            Assert.Equal("<div><p>p 1</p><div>div 2</div><p>p 1</p><div>div 2</div></div>", n4.ToString());
        }
コード例 #18
0
ファイル: ElementTest.cs プロジェクト: ygrenier/HDocument
        public void TestReplaceWithElementsByCallback()
        {
            var n1 = new HText("Content");
            var n2 = new HElement("span", n1);
            var n3 = new HElement("span", "Another content");
            var n4 = new HElement("div", n2, n3);

            var elements = new HElement[] { n2, n3 };

            Assert.Same(elements, elements.ReplaceWith((e, i) => {
                return(new HNode[] { new HElement("div", "Div Content " + i.ToString()), new HElement("span", "Span Content " + i.ToString()) });
            }));
            Assert.Equal("<div><div>Div Content 0</div><span>Span Content 0</span><div>Div Content 1</div><span>Span Content 1</span></div>", n4.ToString());

            n4.Descendants("div").ReplaceWith((e, i) => null);
            Assert.Equal("<div><span>Span Content 0</span><span>Span Content 1</span></div>", n4.ToString());

            elements = null;
            Assert.Null(elements.ReplaceWith((e, i) => {
                return(new HNode[] { new HElement("div", "Div Content " + i.ToString()), new HElement("span", "Span Content " + i.ToString()) });
            }));
            Assert.Equal("<div><span>Span Content 0</span><span>Span Content 1</span></div>", n4.ToString());
        }
コード例 #19
0
ファイル: ElementTest.cs プロジェクト: ygrenier/HDocument
        public void TestSetTextElementsByCallback()
        {
            var n1   = new HElement("div", "Content 1");
            var n2   = new HElement("div", "Content 2");
            var n3   = new HElement("div", "Content 3");
            var n4   = new HElement("div", "Content 4");
            var n5   = new HElement("div", "Content 5");
            var root = new HElement("div", n1, n2, n3, n4, n5);

            var elements = new HElement[] { n2, null, n4 };

            Assert.Same(elements, elements.Text((e, i) => "New Content " + i.ToString()));
            Assert.Equal("<div><div>Content 1</div><div>New Content 0</div><div>Content 3</div><div>New Content 2</div><div>Content 5</div></div>", root.ToString());

            elements = null;
            Assert.Equal(null, elements.Text((e, i) => "New Content " + i.ToString()));
        }
コード例 #20
0
ファイル: ElementTest.cs プロジェクト: ygrenier/HDocument
        public void TestInsertBeforeElement()
        {
            var n1      = new HText("Content 1");
            var n2      = new HElement("span", "Content 2");
            var n3      = new HText("Content 3");
            var element = new HElement("div", n1, n2, n3);
            var n4      = new HText("Insertion");

            Assert.Same(n4, n4.InsertBefore(n2));
            Assert.Equal("<div>Content 1Insertion<span>Content 2</span>Content 3</div>", element.ToString());
        }
コード例 #21
0
ファイル: ElementTest.cs プロジェクト: ygrenier/HDocument
        public void TestInsertBeforeElements()
        {
            var n1      = new HText("Content 1");
            var n2      = new HElement("span", "Content 2");
            var n3      = new HText("Content 3");
            var element = new HElement("div", n1, n2, n3);
            var n4      = new HText("Insertion1");
            var n5      = new HText("Insertion2");

            IEnumerable <HNode> elements = new HNode[] { n4, n5 };

            Assert.Same(elements, elements.InsertBefore(n2));
            Assert.Equal("<div>Content 1Insertion1Insertion2<span>Content 2</span>Content 3</div>", element.ToString());
        }