public void Select_attribute_exists()
        {
            DomDocument doc = new DomDocument();

            doc.LoadXml("<section> <div style='a v' /> <div /> </section>");
            Assert.Equal(1, doc.DocumentElement.Select("[style]").Count());
        }
Example #2
0
        public void InnerText_gets_contents_of_child_nodes()
        {
            var doc = new DomDocument();

            doc.LoadXml("<?xml version=\"1.0\" ?><!-- comment--><hello>world</hello>");
            Assert.Equal("world", doc.InnerText);
        }
        public void Select_classes()
        {
            DomDocument doc = new DomDocument();

            doc.LoadXml("<section> <div class='a v' /> <div /> </section>");
            Assert.Equal(1, doc.DocumentElement.Select(".a.v").Count());
        }
Example #4
0
        public void LoadXml_parses_leading_and_or_trailing_ws(string input)
        {
            var d = new DomDocument();

            d.LoadXml(input);
            Assert.Equal(input, d.ToXmlString());
        }
        public void Select_id()
        {
            DomDocument doc = new DomDocument();

            doc.LoadXml("<section> <div id='m' /> <div /> </section>");
            Assert.Equal(1, doc.DocumentElement.Select("#m").Count());
        }
Example #6
0
        public void OuterXml_should_be_equal_to_xml()
        {
            var          d   = new DomDocument();
            const string xml = "<html> <head> </head> <body a=\"false\"> </body> </html>";

            d.LoadXml(xml);
            Assert.Equal(xml, d.OuterXml);
        }
Example #7
0
        public void InnerText_set_will_replace_inner_element_with_text()
        {
            var doc = new DomDocument();

            doc.LoadXml("<!-- comment--><hello></hello>");
            doc.InnerText = "world";
            Assert.Equal("<!-- comment--><hello>world</hello>", doc.OuterXml);
        }
Example #8
0
        public void LoadXml_from_xml_should_roundtrip()
        {
            var doc = new DomDocument();
            var xml = "<html> <body a=\"true\" b=\"false\"> Text <p a=\"b\" /> <span> <time /> </span>  </body> </html>";

            doc.LoadXml(xml);
            Assert.Equal(xml, doc.ToXmlString());
        }
        public void ReplaceWith_returns_the_node_set()
        {
            var xml = "<html><s /><s /></html>";
            var doc = new DomDocument();

            doc.LoadXml(xml);
            var result = doc.Select("s").ReplaceWith("<a /><b /><c />");

            Assert.HasCount(6, result);
        }
Example #10
0
        public void SplitText_should_split_on_index_keep_original_node()
        {
            const string xml = @"<e>this is some text</e>";
            DomDocument  doc = new DomDocument();

            doc.LoadXml(xml);
            var text = ((DomText)doc.DocumentElement.ChildNodes[0]);

            Assert.Same(text, text.SplitText(4));
        }
        public void SetName_should_set_names()
        {
            var xml = "<html> <body a=\"false\"> <s /> <b /> </body> </html>";
            var doc = new DomDocument();

            doc.LoadXml(xml);
            doc.Select("body").SetName("head");

            Assert.Equal("<html> <head a=\"false\"> <s /> <b /> </head> </html>",
                         doc.ToXmlString());
        }
        public void Remove_should_delete_nodes()
        {
            var xml = "<html> <body><s /><s /><s /></body> </html>";
            var doc = new DomDocument();

            doc.LoadXml(xml);
            doc.Select("s").Remove();

            Assert.Equal("<html> <body /> </html>",
                         doc.ToXmlString());
        }
Example #13
0
        public void SplitText_should_split_on_index_follower_is_rest_of_text()
        {
            const string xml = @"<e><!--this is some text--></e>";
            DomDocument  doc = new DomDocument();

            doc.LoadXml(xml);
            var text = ((DomComment)doc.DocumentElement.ChildNodes[0]);

            text.SplitText(4);
            Assert.Equal(text.NodeValue, "this");
            Assert.Equal(text.NextSiblingNode.NodeValue, " is some text");
        }
Example #14
0
        public void Wrap_with_fragment_should_remove_children_from_document()
        {
            var doc = new DomDocument();

            doc.LoadXml(@"<html> <a /> <b /> <c /> </html>");
            var frag = doc.CreateDocumentFragment();

            doc.QuerySelectorAll("html > *").Wrap(frag);

            Assert.Equal("<html>    </html>", doc.ToXmlString());
            Assert.Equal("<a /><b /><c />", frag.ToXmlString());
        }
Example #15
0
        public void Append_should_append_fragment_contents_not_fragment_itself()
        {
            var frag = new DomDocument().CreateDocumentFragment();

            frag.LoadXml(@"<a /> <b /> <c />");

            var doc = new DomDocument();

            doc.AppendElement("html").Append(frag);
            Assert.Equal("<html><a /> <b /> <c /></html>", doc.ToXmlString());
            Assert.Equal("", frag.ToXmlString());
        }
Example #16
0
        public void Wrap_should_apply_specified_element_and_return_it()
        {
            DomDocument doc  = new DomDocument();
            var         head = doc.CreateElement("head").Attribute("profile", "s");

            doc.LoadXml("<html> <title> <s /> </title> </html>");

            var elem = doc.DocumentElement.FirstChild.Wrap(head);

            Assert.Equal("<html> <head profile=\"s\"><title> <s /> </title></head> </html>", doc.ToXmlString());
            Assert.Same(elem, head);
        }
Example #17
0
        public void Wrap_should_apply_element_name_and_return_new_element()
        {
            DomDocument doc = new DomDocument();

            doc.LoadXml("<html> <div> <s /> </div> </html>");

            var elem = doc.DocumentElement.FirstChild.Wrap("body");

            Assert.Equal("<html> <body><div> <s /> </div></body> </html>", doc.ToXmlString());
            Assert.Equal("body", elem.LocalName);
            Assert.Equal(doc.DocumentElement.FirstChild, elem);
        }
        public void ReplaceWith_with_multiple_nodes_does_clone()
        {
            var xml = "<html> <s /><s /><s /> </html>";
            var doc = new DomDocument();

            doc.LoadXml(xml);
            var replacement = doc.CreateElement("t");

            doc.Select("s").ReplaceWith(replacement);

            Assert.Equal("<html> <t /><t /><t /> </html>", doc.ToXmlString());
        }
Example #19
0
        internal DomContainer LoadDocOrFragment(DomDocument doc, string xml)
        {
            if (ParseAs == ParseAsType.Document)
            {
                doc.LoadXml(xml);
                return(doc);
            }

            var frag = doc.CreateDocumentFragment();

            frag.LoadXml(xml);
            return(frag);
        }
        public void ReplaceWith_with_multiple_nodes_keeps_the_replacement()
        {
            var xml = "<html> <s /><s /><s /> </html>";
            var doc = new DomDocument();

            doc.LoadXml(xml);
            var replacement = doc.CreateElement("t");
            var result      = doc.Select("s").ReplaceWith(replacement);

            // We use the actual instance for the first replacement, then clone the rest
            Assert.Same(doc.DocumentElement.FirstChild, replacement);
            Assert.HasCount(3, result);
        }
Example #21
0
 public void FollowingSiblings_should_obtain_inorder_following_elements() {
     string xml = @"<root>
                       <m>
                        <a />
                        <b>
                         <c>
                           <d />
                         </c>
                        </b><e />
                    </m><f />
                    </root>";
     DomDocument doc = new DomDocument();
     doc.LoadXml(xml);
     var a = doc.DocumentElement.FirstChild.FirstChild;
     Assert.Equal("bcdef", string.Concat(a.FollowingSiblings.Select(t => t.Name)));
 }
        public void ImmediateParent_should_consider_depth()
        {
            DomDocument d = new DomDocument();

            d.LoadXml(@"<html>
                           <a id='1'>
                                <a id='2'>
                                    <a id='3'> </a>
                                </a>
                           </a>
                        </html>");
            var match = d.QuerySelectorAll("html > a");

            Assert.HasCount(1, match);
            Assert.Equal("1", match.First().Attribute("id"));
        }
Example #23
0
 public void FollowingNodes_should_obtain_inorder_following_nodes() {
     string xml = @"<root>
                       <m>
                        <a />
                        <b>
                         <c>
                           <d />
                         </c>
                        </b><e />
                    </m><f />
                    </root>";
     DomDocument doc = new DomDocument();
     doc.LoadXml(xml);
     var a = doc.DocumentElement.FirstChild.FirstChild;
     Assert.Equal("#text b #text c #text #text d #text e #text f #text", string.Join(" ", a.FollowingNodes.Select(t => t.NodeName)));
 }
        public void Unwrap_should_unwrap_nodes()
        {
            const string xml = @"<section>
  <dl>
  <dd class='f'><dl></dl>
  </dd>
  <dd class='e'><dl></dl></dd>
  </dl>
  </section>";
            var          doc = new DomDocument();

            doc.LoadXml(xml);
            doc.QuerySelectorAll("dl").QuerySelectorAll("> dd").Unwrap();
            Assert.Equal("<section>\n<dl>\n<dl />\n<dl />\n</dl>\n</section>",
                         doc.CompressWhitespace().ToXmlString());
        }
Example #25
0
 public void PrecedingNodes_should_obtain_inorder_preceding_nodes() {
     string xml = @"<root>
                       <m>
                        <a />
                        <b>
                         <c>
                           <d />
                         </c>
                        </b><e />
                    </m><f />
                    </root>";
     DomDocument doc = new DomDocument();
     doc.LoadXml(xml);
     var e = doc.QuerySelector("e");
     Assert.Equal("#document root #text m #text a #text b #text c #text #text d #text", string.Join(" ", e.PrecedingNodes.Select(t => t.NodeName)));
 }
Example #26
0
        public void SplitText_should_split_on_regular_expression_remove_zero_length_texts()
        {
            const string xml = @"<e><!--this null false and true splits--></e>";
            DomDocument  doc = new DomDocument();

            doc.LoadXml(xml);
            var text = ((DomComment)doc.DocumentElement.ChildNodes[0]);

            text.SplitText(new Regex("(this|null|false|true)"));
            var items = new [] { text.NodeValue }.Concat(
                text.FollowingSiblingNodes.Select(t => t.NodeValue).ToArray()
                );

            // N.B.: that regex split would usually lead to "" at beginning and end, but
            // we drop that
            Assert.Equal <string>(new string[] { "this", " ", "null", " ", "false", " and ", "true", " splits" }, items);
        }