Exemplo n.º 1
0
        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());
        }
Exemplo n.º 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);
        }
Exemplo n.º 3
0
        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());
        }
Exemplo n.º 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());
        }
Exemplo n.º 5
0
        public void Select_id()
        {
            DomDocument doc = new DomDocument();

            doc.LoadXml("<section> <div id='m' /> <div /> </section>");
            Assert.Equal(1, doc.DocumentElement.Select("#m").Count());
        }
Exemplo n.º 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);
        }
Exemplo n.º 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);
        }
Exemplo n.º 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());
        }
Exemplo n.º 9
0
        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);
        }
Exemplo n.º 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));
        }
Exemplo n.º 11
0
        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());
        }
Exemplo n.º 12
0
        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());
        }
Exemplo n.º 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");
        }
Exemplo n.º 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());
        }
Exemplo n.º 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());
        }
Exemplo n.º 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);
        }
Exemplo n.º 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);
        }
Exemplo n.º 18
0
        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());
        }
Exemplo n.º 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);
        }
Exemplo n.º 20
0
        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);
        }
Exemplo n.º 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"));
        }
Exemplo n.º 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)));
 }
Exemplo n.º 24
0
        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());
        }
Exemplo n.º 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)));
 }
Exemplo n.º 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);
        }