public void TestCreateFromOther() { var elm = new HElement("test", "content"); elm.Add( new HAttribute("attr1", "val1"), new HAttribute("attr3", "val3") ); Assert.Equal(1, elm.Nodes().Count()); var newElm = new HElement(elm); Assert.Equal(2, elm.Attributes().Count()); Assert.Equal(0, elm.Attributes("Attr2").Count()); Assert.Equal(1, elm.Nodes().Count()); }
public void TestPrependElementsByCallback() { var element1 = new HElement("div", new HElement("p")); var element2 = new HElement("div", new HElement("p"), new HElement("div"), new HAttribute("attr1", "val1")); var elements = new HElement[] { element1, null, element2 }; Assert.Same(elements, elements.Prepend((e, i) => { return(new object[] { new HAttribute("a" + i.ToString(), i.ToString()), "Content" }); })); Assert.Equal(2, element1.Nodes().Count()); Assert.Equal("0", element1.Attr("a0")); Assert.Equal(3, element2.Nodes().Count()); Assert.Equal("2", element2.Attr("a2")); elements = null; Assert.Null(elements.Prepend((e, i) => { return(new object[] { new HAttribute("a" + i.ToString(), i.ToString()), "Content" }); })); }
public void TestCreateWithContent() { String name = "test"; var elm = new HElement(name, null); Assert.Equal(0, elm.Nodes().Count()); elm = new HElement(name, "a"); Assert.Equal(1, elm.Nodes().Count()); }
public void TestDeserializeDocument() { var serializer = new HSerializer(); var hdoc = serializer.DeserializeDocument(new StringReader("<html><body><h1>Document</h1><p>Content & more &entity;.</p></body></html>")); Assert.Same(Encoding.UTF8, hdoc.Encoding); // Document with one root Assert.Equal(1, hdoc.Nodes().Count()); Assert.Equal("html", hdoc.Root.Name); // Root with one body Assert.Equal(1, hdoc.Root.Nodes().Count()); HElement body = hdoc.Root.FirstNode as HElement; Assert.NotNull(body); Assert.Equal("body", body.Name); // Body contains two elements Assert.Equal(2, body.Nodes().Count()); var elms = body.Elements().ToArray(); Assert.Equal(2, elms.Length); // First h1 Assert.Equal("h1", elms[0].Name); Assert.Equal(1, elms[0].Nodes().Count()); Assert.IsType <HText>(elms[0].FirstNode); Assert.Equal("Document", ((HText)elms[0].FirstNode).Value); // Second p Assert.Equal("p", elms[1].Name); Assert.Equal(1, elms[1].Nodes().Count()); Assert.IsType <HText>(elms[1].FirstNode); Assert.Equal("Content & more &entity;.", ((HText)elms[1].FirstNode).Value); // Test from stream using (var ms = new MemoryStream(Encoding.ASCII.GetBytes("<html><body><h1>Document</h1><p>Content & more &entity;.</p></body></html>"))) { serializer.RemoveUnknownOrInvalidEntities = true; hdoc = serializer.DeserializeDocument(new StreamReader(ms, Encoding.ASCII)); Assert.Same(Encoding.ASCII, hdoc.Encoding); Assert.Equal(6, hdoc.DescendantNodes().Count()); Assert.Equal(0, hdoc.ParseErrors.Length); HElement elm = hdoc.Root.Elements("body").First().Elements().Last(); Assert.Equal("p", elm.Name); Assert.Equal(1, elm.Nodes().Count()); Assert.IsType <HText>(elm.FirstNode); Assert.Equal("Content & more .", ((HText)elm.FirstNode).Value); } Assert.Throws <ArgumentNullException>(() => serializer.DeserializeDocument(null)); }
public void TestAppendElement() { var element = new HElement("div", new HElement("p")); Assert.Same(element, element.Append(new HElement("span"), "test", null, new HAttribute("attr", "value"))); Assert.Equal(3, element.Nodes().Count()); Assert.Equal("value", element.Attr("attr")); element = null; Assert.Null(element.Append(new HElement("span"), "test", null, new HAttribute("attr", "value"))); }
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"))); }
public void TestRemove() { var node = new HText("node"); // Create parent var elm = new HElement("test", node); Assert.Same(node, elm.FirstNode); Assert.Same(node, elm.LastNode); Assert.Same(elm, node.Parent); Assert.Equal(1, elm.Nodes().Count()); // Remove attribute node.Remove(); Assert.Null(elm.FirstNode); Assert.Null(elm.LastNode); Assert.Null(node.Parent); Assert.Equal(0, elm.Nodes().Count()); // Fail to remove a detached node var ioe = Assert.Throws <InvalidOperationException>(() => node.Remove()); Assert.Equal("No parent found.", ioe.Message); }
public void TestNodes() { HContainer container = new HElement("Test"); Assert.False(container.HasNodes); HNode[] nodes = container.Nodes().ToArray(); Assert.Equal(0, nodes.Length); container.Add("Content 1"); Assert.True(container.HasNodes); container.Add(new HElement("test")); container.Add("Content 2"); container.Add(new HElement("element")); container.Add("Content 3"); nodes = container.Nodes().ToArray(); Assert.Equal(5, nodes.Length); Assert.IsType <HText>(nodes[0]); Assert.IsType <HElement>(nodes[1]); Assert.IsType <HText>(nodes[2]); Assert.IsType <HElement>(nodes[3]); Assert.IsType <HText>(nodes[4]); }
public void TestClone() { var container = new HElement("parent"); container.Add(new HElement("child1")); container.Add(new HElement("child2", "Content")); container.Add(new HElement("child3", new HText("Content 1"), new HText("Content 2"))); // Adding this container itself to a clone container.Add(container); var nodes = container.Nodes().ToArray(); Assert.Equal(4, nodes.Length); }
public void TestPrependElements() { var element1 = new HElement("div", new HElement("p")); var element2 = new HElement("div", new HElement("p"), new HElement("div"), new HAttribute("attr1", "val1")); var elements = new HElement[] { element1, null, element2 }; Assert.Same(elements, elements.Prepend(new HElement("span"), "test", null, new HAttribute("attr", "value"))); Assert.Equal(3, element1.Nodes().Count()); Assert.Equal("value", element1.Attr("attr")); Assert.Equal(4, element2.Nodes().Count()); Assert.Equal("value", element2.Attr("attr")); elements = null; Assert.Null(elements.Prepend(new HElement("span"), "test", null, new HAttribute("attr", "value"))); }
public void TestAddString() { HContainer container = new HElement("test"); container.Add("String 1"); container.Add(" - String 2"); Assert.IsType <HText>(container.FirstNode); Assert.Equal("String 1 - String 2", ((HText)container.FirstNode).Value); container.Add(" - String 3"); Assert.IsType <HText>(container.FirstNode); Assert.Equal("String 1 - String 2 - String 3", ((HText)container.FirstNode).Value); container = new HElement("test"); container.Add("Content"); container.Add(new HText("Other content")); Assert.Equal(2, container.Nodes().Count()); }
public void TestDeserializeDocument_WithErrors() { var serializer = new HSerializer(); var hdoc = serializer.DeserializeDocument(new StringReader("<html><body><h1>Document</h1><p class=>Content & more.</p></body></html>")); Assert.Same(Encoding.UTF8, hdoc.Encoding); // Document with one root Assert.Equal(1, hdoc.Nodes().Count()); Assert.Equal("html", hdoc.Root.Name); // Root with one body Assert.Equal(1, hdoc.Root.Nodes().Count()); HElement body = hdoc.Root.FirstNode as HElement; Assert.NotNull(body); Assert.Equal("body", body.Name); // Body contains two elements Assert.Equal(2, body.Nodes().Count()); var elms = body.Elements().ToArray(); Assert.Equal(2, elms.Length); // First h1 Assert.Equal("h1", elms[0].Name); Assert.Equal(1, elms[0].Nodes().Count()); Assert.IsType <HText>(elms[0].FirstNode); Assert.Equal("Document", ((HText)elms[0].FirstNode).Value); // Second p Assert.Equal("p", elms[1].Name); Assert.Equal(1, elms[1].Nodes().Count()); Assert.IsType <HText>(elms[1].FirstNode); Assert.Equal("Content & more.", ((HText)elms[1].FirstNode).Value); // Check errors Assert.Equal(1, hdoc.ParseErrors.Length); Assert.Equal("Attribute value expected.", hdoc.ParseErrors[0].Message); }
public void TestRemoveNode() { HNode node1 = new HText("node 1"); HNode node2 = new HElement("node2", "value2"); HNode node3 = new HText("node 3"); HNode node4 = new HElement("node4", "value4"); // Create parent var elm = new HElement("test", node1, node2, node3, node4); Assert.Same(node1, elm.FirstNode); Assert.Same(node4, elm.LastNode); Assert.Same(elm, node1.Parent); Assert.Null(node1.PreviousNode); Assert.Same(node2, node1.NextNode); Assert.Same(elm, node2.Parent); Assert.Same(node1, node2.PreviousNode); Assert.Same(node3, node2.NextNode); Assert.Same(elm, node3.Parent); Assert.Same(node2, node3.PreviousNode); Assert.Same(node4, node3.NextNode); Assert.Same(elm, node4.Parent); Assert.Same(node3, node4.PreviousNode); Assert.Null(node4.NextNode); // Remove node2 node2.Remove(); Assert.Same(node1, elm.FirstNode); Assert.Same(node4, elm.LastNode); Assert.Same(elm, node1.Parent); Assert.Null(node1.PreviousNode); Assert.Same(node3, node1.NextNode); Assert.Null(node2.Parent); Assert.Null(node2.PreviousNode); Assert.Null(node2.NextNode); Assert.Same(elm, node3.Parent); Assert.Same(node1, node3.PreviousNode); Assert.Same(node4, node3.NextNode); Assert.Same(elm, node4.Parent); Assert.Same(node3, node4.PreviousNode); Assert.Null(node4.NextNode); var attrs = elm.Nodes().ToArray(); Assert.Equal(new HNode[] { node1, node3, node4 }, attrs); // Remove node4 node4.Remove(); Assert.Same(node1, elm.FirstNode); Assert.Same(node3, elm.LastNode); Assert.Same(elm, node1.Parent); Assert.Null(node1.PreviousNode); Assert.Same(node3, node1.NextNode); Assert.Same(elm, node3.Parent); Assert.Same(node1, node3.PreviousNode); Assert.Null(node3.NextNode); Assert.Null(node4.Parent); Assert.Null(node4.PreviousNode); Assert.Null(node4.NextNode); attrs = elm.Nodes().ToArray(); Assert.Equal(new HNode[] { node1, node3 }, attrs); // Remove node1 node1.Remove(); Assert.Same(node3, elm.FirstNode); Assert.Same(node3, elm.LastNode); Assert.Null(node1.Parent); Assert.Null(node1.PreviousNode); Assert.Null(node1.NextNode); Assert.Same(elm, node3.Parent); Assert.Null(node3.PreviousNode); Assert.Null(node3.NextNode); attrs = elm.Nodes().ToArray(); Assert.Equal(new HNode[] { node3 }, attrs); // Remove node3 node3.Remove(); Assert.Null(elm.FirstNode); Assert.Null(elm.LastNode); Assert.Null(node3.Parent); Assert.Null(node3.PreviousNode); Assert.Null(node3.NextNode); attrs = elm.Nodes().ToArray(); Assert.Equal(new HNode[] { }, attrs); // Fail to remove a detached attribute var ioe = Assert.Throws <InvalidOperationException>(() => node1.Remove()); Assert.Equal("No parent found.", ioe.Message); }
public void TestRemoveNodes() { HNode node1 = new HText("node 1"); HNode node2 = new HElement("node2", "value2"); HNode node3 = new HText("node 3"); HNode node4 = new HElement("node4", "value4"); // Create parent var elm = new HElement("test", node1, node2, node3, node4); Assert.Same(node1, elm.FirstNode); Assert.Same(node4, elm.LastNode); Assert.Same(elm, node1.Parent); Assert.Null(node1.PreviousNode); Assert.Same(node2, node1.NextNode); Assert.Same(elm, node2.Parent); Assert.Same(node1, node2.PreviousNode); Assert.Same(node3, node2.NextNode); Assert.Same(elm, node3.Parent); Assert.Same(node2, node3.PreviousNode); Assert.Same(node4, node3.NextNode); Assert.Same(elm, node4.Parent); Assert.Same(node3, node4.PreviousNode); Assert.Null(node4.NextNode); // Remove all nodes elm.RemoveNodes(); Assert.Null(elm.FirstNode); Assert.Null(elm.LastNode); Assert.Null(node1.Parent); Assert.Null(node1.PreviousNode); Assert.Null(node1.NextNode); Assert.Null(node2.Parent); Assert.Null(node2.PreviousNode); Assert.Null(node2.NextNode); Assert.Null(node3.Parent); Assert.Null(node3.PreviousNode); Assert.Null(node3.NextNode); Assert.Null(node4.Parent); Assert.Null(node4.PreviousNode); Assert.Null(node4.NextNode); Assert.Equal(0, elm.Nodes().Count()); // Create new element with string content elm = new HElement("test", "content"); elm.RemoveNodes(); Assert.Null(elm.FirstNode); Assert.Null(elm.LastNode); Assert.Equal(0, elm.Nodes().Count()); // Check no exception elm.RemoveNodes(); Assert.Equal(0, elm.Nodes().Count()); }