Ejemplo n.º 1
0
        public void TestRemoveAll()
        {
            HNode node1 = new HText("node 1");
            HNode node2 = new HElement("node2", "value2");
            HNode node3 = new HText("node 3");
            HNode node4 = new HElement("node4", "value4");

            var attr1 = new HAttribute("attr1", "value1");
            var attr2 = new HAttribute("attr2", "value2");
            var attr3 = new HAttribute("attr3", "value3");
            var attr4 = new HAttribute("attr4", "value4");

            // Create parent
            var elm = new HElement("test", attr1, node1, attr2, node2, node3, node4, attr3, attr4);

            Assert.Equal(4, elm.Attributes().Count());
            Assert.Equal(4, elm.Nodes().Count());

            // Remove all
            elm.RemoveAll();
            Assert.Equal(0, elm.Attributes().Count());
            Assert.Equal(0, elm.Nodes().Count());

            // Check no exception
            elm.RemoveAll();
            Assert.Equal(0, elm.Attributes().Count());
            Assert.Equal(0, elm.Nodes().Count());
        }
Ejemplo n.º 2
0
        public void TestRemoveAttributes()
        {
            var attr1 = new HAttribute("attr1", "value1");
            var attr2 = new HAttribute("attr2", "value2");
            var attr3 = new HAttribute("attr3", "value3");
            var attr4 = new HAttribute("attr4", "value4");

            // Create parent
            var elm = new HElement("test", attr1, attr2, attr3, attr4);

            Assert.Same(attr1, elm.FirstAttribute);
            Assert.Same(attr4, elm.LastAttribute);

            Assert.Same(elm, attr1.Parent);
            Assert.Null(attr1.PreviousAttribute);
            Assert.Same(attr2, attr1.NextAttribute);

            Assert.Same(elm, attr2.Parent);
            Assert.Same(attr1, attr2.PreviousAttribute);
            Assert.Same(attr3, attr2.NextAttribute);

            Assert.Same(elm, attr3.Parent);
            Assert.Same(attr2, attr3.PreviousAttribute);
            Assert.Same(attr4, attr3.NextAttribute);

            Assert.Same(elm, attr4.Parent);
            Assert.Same(attr3, attr4.PreviousAttribute);
            Assert.Null(attr4.NextAttribute);

            // Remove all attributes
            elm.RemoveAttributes();

            Assert.Null(elm.FirstAttribute);
            Assert.Null(elm.LastAttribute);

            Assert.Null(attr1.Parent);
            Assert.Null(attr1.PreviousAttribute);
            Assert.Null(attr1.NextAttribute);

            Assert.Null(attr2.Parent);
            Assert.Null(attr2.PreviousAttribute);
            Assert.Null(attr2.NextAttribute);

            Assert.Null(attr3.Parent);
            Assert.Null(attr3.PreviousAttribute);
            Assert.Null(attr3.NextAttribute);

            Assert.Null(attr4.Parent);
            Assert.Null(attr4.PreviousAttribute);
            Assert.Null(attr4.NextAttribute);

            Assert.Equal(0, elm.Attributes().Count());

            // Check no exception
            elm.RemoveAttributes();
            Assert.Equal(0, elm.Attributes().Count());
        }
Ejemplo n.º 3
0
        public void TestAttributes()
        {
            // Attributes are empty
            var elm = new HElement("test");

            Assert.Equal(0, elm.Attributes().Count());
            Assert.Null(elm.Attribute(null));
            Assert.Null(elm.Attribute(""));
            Assert.Null(elm.Attribute(" "));
            Assert.Null(elm.Attribute("attr2"));

            // Adding two attributes
            elm.Add(
                new HAttribute("attr1", "val1"),
                new HAttribute("attr3", "val3")
                );
            Assert.Equal(2, elm.Attributes().Count());
            Assert.Equal(0, elm.Attributes("Attr2").Count());
            Assert.Null(elm.Attribute(null));
            Assert.Null(elm.Attribute(""));
            Assert.Null(elm.Attribute(" "));
            Assert.Null(elm.Attribute("attr2"));

            // Adding a third attribute
            elm.Add(new HAttribute("attr2", "val2"));

            Assert.Equal(3, elm.Attributes().Count());
            Assert.Equal(1, elm.Attributes("Attr2").Count());
            Assert.Null(elm.Attribute(null));
            Assert.Null(elm.Attribute(""));
            Assert.Null(elm.Attribute(" "));
            Assert.NotNull(elm.Attribute("attr2"));
            Assert.NotNull(elm.Attribute("Attr2"));

            // Can't adding an attribute with a name already defined
            var ioex = Assert.Throws <InvalidOperationException>(() => elm.Add(new HAttribute("attr2", "value")));

            Assert.Equal("The attribute 'attr2' already defined", ioex.Message);

            // Test adding an attribute already defined in another element
            var otherElement = new HElement("OtherTest");
            var a1           = elm.Attribute("Attr2");

            otherElement.Add(a1);
            var a2 = otherElement.Attribute("attr2");

            Assert.NotSame(a1, a2);
            Assert.Equal(a1.Name, a2.Name);
            Assert.Equal(a1.Value, a2.Value);
        }
Ejemplo n.º 4
0
        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());
        }
Ejemplo n.º 5
0
        public void TestRemoveDataElement()
        {
            HElement element = new HElement("div",
                                            new HAttribute("data-value1", "value1"),
                                            new HAttribute("data-value2", "value2"),
                                            new HAttribute("data-value3", "value3")
                                            );

            element = element.RemoveData("value1", "value4");

            Assert.Equal(new String[] { "data-value2", "data-value3" }, element.Attributes().Select(a => a.Name).ToArray());

            element = element.RemoveData();

            Assert.Equal(new String[] { }, element.Attributes().Select(a => a.Name).ToArray());

            element = null;
            element = element.RemoveData();
            Assert.Null(element);
        }
Ejemplo n.º 6
0
        public void TestRemove()
        {
            var attr1 = new HAttribute("attr1", "value1");

            // Create parent
            var elm = new HElement("test", attr1);

            Assert.Same(attr1, elm.FirstAttribute);
            Assert.Same(attr1, elm.LastAttribute);
            Assert.Same(elm, attr1.Parent);
            Assert.Equal(1, elm.Attributes().Count());

            // Remove attribute
            attr1.Remove();
            Assert.Null(elm.FirstAttribute);
            Assert.Null(elm.LastAttribute);
            Assert.Null(attr1.Parent);
            Assert.Equal(0, elm.Attributes().Count());

            // Fail to remove a detached attribute
            var ioe = Assert.Throws <InvalidOperationException>(() => attr1.Remove());

            Assert.Equal("No parent found.", ioe.Message);
        }
Ejemplo n.º 7
0
        public void TestRemoveAttribute()
        {
            var attr1 = new HAttribute("attr1", "value1");
            var attr2 = new HAttribute("attr2", "value2");
            var attr3 = new HAttribute("attr3", "value3");
            var attr4 = new HAttribute("attr4", "value4");

            // Create parent
            var elm = new HElement("test", attr1, attr2, attr3, attr4);

            Assert.Same(attr1, elm.FirstAttribute);
            Assert.Same(attr4, elm.LastAttribute);

            Assert.Same(elm, attr1.Parent);
            Assert.Null(attr1.PreviousAttribute);
            Assert.Same(attr2, attr1.NextAttribute);

            Assert.Same(elm, attr2.Parent);
            Assert.Same(attr1, attr2.PreviousAttribute);
            Assert.Same(attr3, attr2.NextAttribute);

            Assert.Same(elm, attr3.Parent);
            Assert.Same(attr2, attr3.PreviousAttribute);
            Assert.Same(attr4, attr3.NextAttribute);

            Assert.Same(elm, attr4.Parent);
            Assert.Same(attr3, attr4.PreviousAttribute);
            Assert.Null(attr4.NextAttribute);

            // Remove attr2
            attr2.Remove();
            Assert.Same(attr1, elm.FirstAttribute);
            Assert.Same(attr4, elm.LastAttribute);

            Assert.Same(elm, attr1.Parent);
            Assert.Null(attr1.PreviousAttribute);
            Assert.Same(attr3, attr1.NextAttribute);

            Assert.Null(attr2.Parent);
            Assert.Null(attr2.PreviousAttribute);
            Assert.Null(attr2.NextAttribute);

            Assert.Same(elm, attr3.Parent);
            Assert.Same(attr1, attr3.PreviousAttribute);
            Assert.Same(attr4, attr3.NextAttribute);

            Assert.Same(elm, attr4.Parent);
            Assert.Same(attr3, attr4.PreviousAttribute);
            Assert.Null(attr4.NextAttribute);

            var attrs = elm.Attributes().ToArray();

            Assert.Equal(new HAttribute[] { attr1, attr3, attr4 }, attrs);

            // Remove attr4
            attr4.Remove();
            Assert.Same(attr1, elm.FirstAttribute);
            Assert.Same(attr3, elm.LastAttribute);

            Assert.Same(elm, attr1.Parent);
            Assert.Null(attr1.PreviousAttribute);
            Assert.Same(attr3, attr1.NextAttribute);

            Assert.Same(elm, attr3.Parent);
            Assert.Same(attr1, attr3.PreviousAttribute);
            Assert.Null(attr3.NextAttribute);

            Assert.Null(attr4.Parent);
            Assert.Null(attr4.PreviousAttribute);
            Assert.Null(attr4.NextAttribute);

            attrs = elm.Attributes().ToArray();
            Assert.Equal(new HAttribute[] { attr1, attr3 }, attrs);

            // Remove attr1
            attr1.Remove();
            Assert.Same(attr3, elm.FirstAttribute);
            Assert.Same(attr3, elm.LastAttribute);

            Assert.Null(attr1.Parent);
            Assert.Null(attr1.PreviousAttribute);
            Assert.Null(attr1.NextAttribute);

            Assert.Same(elm, attr3.Parent);
            Assert.Null(attr3.PreviousAttribute);
            Assert.Null(attr3.NextAttribute);

            attrs = elm.Attributes().ToArray();
            Assert.Equal(new HAttribute[] { attr3 }, attrs);

            // Remove attr3
            attr3.Remove();
            Assert.Null(elm.FirstAttribute);
            Assert.Null(elm.LastAttribute);

            Assert.Null(attr3.Parent);
            Assert.Null(attr3.PreviousAttribute);
            Assert.Null(attr3.NextAttribute);

            attrs = elm.Attributes().ToArray();
            Assert.Equal(new HAttribute[] { }, attrs);

            // Fail to remove a detached attribute
            var ioe = Assert.Throws <InvalidOperationException>(() => attr1.Remove());

            Assert.Equal("No parent found.", ioe.Message);
        }