public void construct_generic_dom_document_fluent_appenders()
 {
     DomDocument d = new DomDocument();
     d.AppendElement("html")
         .AppendElement("body")
         .AppendElement("p")
         .AppendText("Hello, World");
 }
 public void test_append_element_implies_parent()
 {
     DomDocument doc = new DomDocument();
     var ele = doc.AppendElement("html");
     Assert.That(ele.ParentNode, Is.SameAs(doc));
     Assert.That(doc.DocumentElement, Is.SameAs(ele));
     Assert.That(doc.DocumentElement.ChildNodes, Is.Empty);
     Assert.False(doc.UnlinkedNodes.Contains(ele));
 }
Beispiel #3
0
 public void test_add_child_implies_parent_and_owner()
 {
     DomDocument doc = new DomDocument();
     var html = doc.AppendElement("html");
     var text = html.AppendText("ws");
     Assert.That(text.ChildNodes.Count, Is.EqualTo(0));
     Assert.That(text.OwnerDocument, Is.SameAs(doc));
     Assert.That(text.ParentNode, Is.SameAs(html));
     Assert.That(text.ParentElement, Is.SameAs(html));
 }
        public void test_append_attribute_implies_parent_and_owner()
        {
            DomDocument doc = new DomDocument();
            var html = doc.AppendElement("html");
            var attr = html.AppendAttribute("lang", "en");

            Assert.That(attr.OwnerDocument, Is.SameAs(doc));
            Assert.That(attr.OwnerElement, Is.SameAs(html));
            Assert.That(attr.ParentNode, Is.Null); // per spec
        }
 public void test_create_attribute_implies_owner_document()
 {
     DomDocument doc = new DomDocument();
     var html = doc.AppendElement("html");
     var attr = doc.CreateAttribute("class");
     html.Attributes.Add(attr);
     Assert.That(attr.OwnerElement, Is.SameAs(html));
     Assert.That(attr.ParentNode, Is.Null);
     Assert.That(attr.OwnerDocument, Is.SameAs(doc));
 }
        public void test_add_attribute_implies_parent_and_owner()
        {
            DomDocument doc = new DomDocument();
            var html = doc.AppendElement("html");
            html.Attribute("lang", "en");

            DomAttribute attr = html.Attributes[0];
            Assert.That(attr.OwnerDocument, Is.SameAs(doc));
            Assert.That(attr.OwnerElement, Is.SameAs(html));
            Assert.That(attr.ParentNode, Is.Null); // per spec
            Assert.False(doc.UnlinkedNodes.Contains(attr));
        }
        public void test_append_attribute_implies_add()
        {
            DomDocument doc = new DomDocument();
            var html = doc.AppendElement("html");
            var attr = doc.CreateAttribute("lang", "en");

            html.Append(attr);

            Assert.That(html.Attributes.Count, Is.EqualTo(1));
            Assert.That(html.Attribute("lang"), Is.EqualTo("en"));
            Assert.That(attr.OwnerDocument, Is.SameAs(doc));
        }
        public void test_attribute_adjacent_nominal()
        {
            DomDocument doc = new DomDocument();
            var html = doc.AppendElement("html");
            html.Attribute("lang", "en");
            html.Attribute("dir", "ltr");
            html.Attribute("data-e", "e");

            DomAttribute attr = html.Attributes[1];
            Assert.That(attr.NodePosition, Is.EqualTo(1));
            Assert.That(attr.NextAttribute.Name, Is.EqualTo("data-e"));
            Assert.That(attr.PreviousAttribute.Name, Is.EqualTo("lang"));
        }
 public void test_outer_text_domelement()
 {
     DomDocument doc = new DomDocument();
     var html = doc.AppendElement("html");
     var body = html.AppendElement("body");
     html.Attribute("lang", "en");
     html.Attribute("data-cast", "true");
     body.Attribute("dir", "ltr");
     body.Attribute("class", "hl");
     body.AppendText("Hello, world!");
     string htmlText = "<html lang=\"en\" data-cast=\"true\"><body dir=\"ltr\" class=\"hl\">Hello, world!</body></html>";
     Assert.That(html.OuterText, Is.EqualTo(htmlText));
     Assert.That(html.ToString(), Is.EqualTo(htmlText));
 }
        public void test_descendents_and_self()
        {
            DomDocument doc = new DomDocument();
            var html = doc.AppendElement("html");
            var body = html.AppendElement("body");
            var para1 = body.AppendElement("p");
            var para2 = body.AppendElement("div");
            para1.AppendText("Hello, world!");
            para2.AppendText("Hello, world!");

            Assert.That(string.Join(" ", html.DescendantsAndSelf.Select(t => t.NodeName)),
                        Is.EquivalentTo("html body p div"));
            Assert.That(string.Join(" ", html.Descendants.Select(t => t.NodeName)),
                        Is.EquivalentTo("body p div"));
        }
        public void test_elements_list_nominal()
        {
            DomDocument doc = new DomDocument();
            var svg = doc.AppendElement("svg");
            var g1 = svg.AppendElement("g");
            var g2 = svg.AppendElement("g");
            var g3 = svg.AppendElement("g");

            Assert.That(svg.Elements[1], Is.SameAs(g2));
            Assert.That(svg.Elements[2], Is.SameAs(g3));

            Assert.That(svg.Element(1), Is.SameAs(g2));
            Assert.That(svg.Element(2), Is.SameAs(g3));

            Assert.That(svg.Element("g"), Is.SameAs(g1));
        }
        public void test_clear_collection_implies_no_parent()
        {
            DomDocument doc = new DomDocument();
            var html = doc.AppendElement("html");
            var attr1 = html.AppendAttribute("lang", "en");
            var attr2 = html.AppendAttribute("dir", "ltr");
            var attr3 = html.AppendAttribute("class", "y");

            Assert.That(attr1.OwnerDocument, Is.SameAs(doc));
            Assert.That(attr1.OwnerElement, Is.SameAs(html));
            Assert.That(attr1.ParentElement, Is.Null); // spec
            html.Attributes.Clear();

            Assert.That(html.Attributes.Count, Is.EqualTo(0));
            Assert.That(attr1.OwnerDocument, Is.SameAs(doc));
            Assert.That(attr1.OwnerElement, Is.Null); // spec
            Assert.That(attr1.ParentElement, Is.Null);
        }
        public void test_remove_from_collection_implies_no_parent()
        {
            DomDocument doc = new DomDocument();
            var html = doc.AppendElement("html");
            var attr1 = html.AppendAttribute("lang", "en");

            Assert.That(attr1.OwnerDocument, Is.SameAs(doc));
            Assert.That(attr1.OwnerElement, Is.SameAs(html));
            Assert.That(attr1.ParentElement, Is.Null); // spec
            Assert.That(attr1.NodePosition, Is.EqualTo(0));
            Assert.That(html.Attributes.Count, Is.EqualTo(1));
            Assert.That(html.Attributes.IndexOf(attr1), Is.EqualTo(0));
            Assert.True(html.Attributes.Remove(attr1));
            Assert.That(html.Attributes.Count, Is.EqualTo(0));
            Assert.That(html.Attributes.IndexOf(attr1), Is.EqualTo(-1));

            Assert.That(attr1.OwnerDocument, Is.SameAs(doc));
            Assert.That(attr1.OwnerElement, Is.Null); // spec
            Assert.That(attr1.ParentElement, Is.Null);
        }
Beispiel #14
0
        public void Xmlns_should_allow_binding_prefix_names_from_AppendAttribute(Func <DomElement, DomAttribute> op)
        {
            var doc = new DomDocument();

            doc.NameContext = DomNameContext.Xml;
            var root = doc.AppendElement("root");

            root.Attribute("xmlns:a", "https://example.com/");

            var newAttr = op(root);

            Assert.Equal(
                "https://example.com/",
                newAttr.NamespaceUri
                );
            Assert.Equal(
                "hello",
                newAttr.LocalName,
                "Local name should re-map without the prefix because of namespace binding"
                );
        }
Beispiel #15
0
        public void Xmlns_should_move_attributes_to_replacement_on_SetName()
        {
            var doc = new DomDocument {
                NameContext = DomNameContext.Xml
            };
            var root = doc.AppendElement("root");

            root.Attribute("xmlns:a", "https://example.com/");

            var newElement = root.SetName("a:ugust");

            // The XML name context moves to the wrapped element
            Assert.IsInstanceOf(typeof(XmlNameContext), newElement.ActualNameContext);
            Assert.Same(newElement, ((XmlNameContext)newElement.ActualNameContext).Container);

            Assert.Equal(
                new [] { "xmlns:a" },
                newElement.Attributes.Select(a => a.Name.ToString("P"))
                );
            Assert.Equal("ugust", newElement.LocalName);
        }
        public void test_attribute_adjacent_singleton()
        {
            DomDocument doc = new DomDocument();
            var html = doc.AppendElement("html");
            html.Attribute("lang", "en");

            DomAttribute attr = html.Attributes[0];
            Assert.That(attr.NextAttribute, Is.Null);
            Assert.That(attr.PreviousAttribute, Is.Null);
        }
Beispiel #17
0
 public void Closest_should_return_self_if_it_matches() {
     var doc = new DomDocument();
     var self = doc.AppendElement("a").Attribute("class", "clear").AppendElement("span").AppendElement("span");
     Assert.Same(self, self.Closest("span"));
 }
Beispiel #18
0
 public void NameContext_set_should_use_Value() {
     var doc = new DomDocument();
     var e = doc.AppendElement("e");
     e.NameContext = DomNameContext.Html;
     Assert.Same(DomNameContext.Html, e.NameContext);
 }
        public void test_attribute_duplicate_name_same_instance()
        {
            DomDocument doc = new DomDocument();
            var html = doc.AppendElement("html");
            var attr = html.AppendAttribute("lang", "en");
            Assert.That(html.Attributes.Count, Is.EqualTo(1));

            // TODO Should this move attribute to end of collection?

            html.Append(attr); // legal
            Assert.That(html.Attributes.Count, Is.EqualTo(1));
        }
 public void test_common_element_properties()
 {
     DomDocument doc = new DomDocument();
     var html = doc.AppendElement("html");
     Assert.That(html.NodeValue, Is.Null);
 }
 public void test_set_dom_value_setter()
 {
     DomDocument doc = new DomDocument();
     var html = doc.AppendElement("html");
     var classList = new DomStringTokenList();
     html.Attribute("class", classList);
     html.Attribute("class", "heat up");
     Assert.That(classList, Contains.Item("heat"));
     Assert.That(classList, Contains.Item("up"));
 }
        public void test_set_dom_value_getter()
        {
            DomDocument doc = new DomDocument();
            var html = doc.AppendElement("html");
            var classList = new DomStringTokenList();
            html.Attribute("class", classList);

            Assert.That(html.Attribute("class"), Is.EqualTo(string.Empty));
            classList.Add("cool");
            classList.Add("down");
            Assert.That(html.Attribute("class"), Is.EqualTo("cool down"));
        }
        public void test_to_xml_string()
        {
            DomDocument doc = new DomDocument();
            // doc.AppendDocumentType("html");
            var html = doc.AppendElement("html");
            var body = html.AppendElement("body");
            body.AppendElement("h1").AppendText("Hello, world");
            html.Attribute("lang", "en");

            // N.B. By default, xml decl is omitted
            Assert.That(doc.ToXml(), Is.EqualTo("<html lang=\"en\"><body><h1>Hello, world</h1></body></html>"));
        }
Beispiel #24
0
        public void AppendElement_should_throw_on_ws()
        {
            var doc = new DomDocument();

            Assert.Throws <ArgumentException>(() => doc.AppendElement(" s"));
        }
        public void test_attribute_remove_self()
        {
            DomDocument doc = new DomDocument();
            var html = doc.AppendElement("html");
            var attr = html.AppendAttribute("lang", "en");

            Assert.That(html.Attributes.Count, Is.EqualTo(1));
            attr.RemoveSelf();

            Assert.That(html.Attributes.Count, Is.EqualTo(0));
        }
        public void test_attribute_implies_new_instance()
        {
            DomDocument doc = new DomDocument();
            var html = doc.AppendElement("html");
            html.Attribute("lang", "en");
            DomAttribute attr = html.Attributes[0];
            Assert.That(html.Attributes.Count, Is.EqualTo(1));
            Assert.That(html.ChildNodes.Count, Is.EqualTo(0));

            // Excludes attributes
            Assert.That(attr.Value, Is.EqualTo("en"));
            Assert.That(attr.Name, Is.EqualTo("lang"));
            Assert.False(doc.UnlinkedNodes.Contains(attr));
        }
Beispiel #27
0
 public void Closest_should_return_closest_element() {
     var doc = new DomDocument();
     var self = doc.AppendElement("a").Attribute("class", "clear").AppendElement("a").AppendElement("span");
     Assert.Same(doc.DocumentElement, self.Closest("a.clear"));
 }
        public void test_replace_with_many_nominal()
        {
            DomDocument doc = new DomDocument();
            var html = doc.AppendElement("html");
            var body = html.AppendElement("body");
            var h1 = body.AppendElement("h1");
            var h3 = doc.CreateElement("h3");

            var result = h1.ReplaceWith(h3, doc.CreateElement("h4"), doc.CreateElement("h5"));
            Assert.That(doc.ToXml(), Is.EqualTo("<html><body><h3 /><h4 /><h5 /></body></html>"));
            Assert.That(result, Is.SameAs(h3));
        }
Beispiel #29
0
 public void NodeDepth_should_be_correct_value_nominal_case() {
     DomDocument doc = new DomDocument();
     var e = doc.AppendElement("a").AppendElement("b").AppendElement("c");
     Assert.Equal(3, e.NodeDepth);
 }
        public void test_attribute_replace_with_attribute()
        {
            DomDocument doc = new DomDocument();
            var html = doc.AppendElement("html");
            var attr = html.AppendAttribute("lang", "en");
            var attr2 = doc.CreateAttribute("dir", "ltr");

            Assert.That(attr.ReplaceWith(attr2), Is.SameAs(attr2));
            Assert.That(html.Attributes.Count, Is.EqualTo(1));
            Assert.That(html.Attributes[0], Is.SameAs(attr2));
            Assert.True(doc.UnlinkedNodes.Contains(attr));
            Assert.False(doc.UnlinkedNodes.Contains(attr2));
        }
        public void test_attribute_duplicate_name_error()
        {
            DomDocument doc = new DomDocument();
            var html = doc.AppendElement("html");
            var attr = html.AppendAttribute("lang", "en");

            Assert.That(() => {
                            html.AppendAttribute("lang", "fr");
                        }, Throws.ArgumentException);

            Assert.That(() => {
                            html.Append(doc.CreateAttribute("lang", "fr"));
                        }, Throws.ArgumentException);

            Assert.That(() => {
                            html.Attributes.Add(doc.CreateAttribute("lang", "fr"));
                        }, Throws.ArgumentException);
        }
        public void test_remove_self_implies_parent_and_owner()
        {
            DomDocument doc = new DomDocument();
            var html = doc.AppendElement("html");
            var body = html.AppendElement("head");

            DomElement e = body.RemoveSelf();
            Assert.That(html.ChildNodes.Count, Is.EqualTo(0));
            Assert.That(e, Is.SameAs(body));
            Assert.That(e.OwnerDocument, Is.SameAs(doc));
            Assert.That(e.ParentNode, Is.Null);
            Assert.That(e.ParentElement, Is.Null);
        }
        public void test_attribute_replace_with_non_attribute()
        {
            DomDocument doc = new DomDocument();
            var html = doc.AppendElement("html");
            var attr = html.AppendAttribute("lang", "en");

            Assert.That(() => {
                            attr.ReplaceWith(doc.CreateCData());
                        }, Throws.ArgumentException);
        }
        public void test_remove_self_implies_parent_and_owner()
        {
            DomDocument doc = new DomDocument();
            var html = doc.AppendElement("html");
            html.Attribute("lang", "en");

            DomAttribute attr = html.Attributes[0].RemoveSelf();
            Assert.That(html.Attributes.Count, Is.EqualTo(0));
            Assert.That(attr.OwnerDocument, Is.SameAs(doc));
            Assert.That(attr.ParentNode, Is.Null);
        }
 public void test_replace_element_with_element()
 {
     DomDocument doc = new DomDocument();
     var html = doc.AppendElement("html");
     var body = html.AppendElement("body");
     var head = (DomElement) body.ReplaceWith(doc.CreateElement("head"));
     Assert.That(((DomElement) html.ChildNode(0)).Name, Is.EqualTo("head"));
     Assert.That(head.Name, Is.EqualTo("head"));
 }