Beispiel #1
0
        public void TestHtml1()
        {
            string html = @"<body><h1>title</h1>message</body>";
            HtmlDocument doc = new HtmlDocument(html);

            Assert.AreEqual(html, doc.Html);
        }
Beispiel #2
0
 public void TestParse1()
 {
     string html = @"
     <body>
       <h1><h2>title</h1></h2>
     </body>";
     HtmlDocument doc = new HtmlDocument(html);
     Assert.AreEqual(@"<body><h1><h2>title</h2></h1></body>", doc.Html);
 }
Beispiel #3
0
        public void TestLoad()
        {
            string html = @"<body>test</body>";
            HtmlDocument doc = new HtmlDocument(html);

            Assert.IsNotNull(doc.documentElement);
            Assert.AreEqual("body", doc.documentElement.TagName);
            Assert.AreEqual("test", doc.documentElement.Value);
        }
Beispiel #4
0
        public void TestTagName()
        {
            string html = @"<body><h1>title</h1>message</body>";
            HtmlDocument doc = new HtmlDocument(html);

            var q = new HtmlNavigator( doc )
                .Where( n => n.TagName == "h1" )
                .Select( n => n );
            Assert.AreEqual(1, q.Count());
            Assert.AreEqual("title", q.First().Value );
        }
Beispiel #5
0
        public void TestTagName2()
        {
            string html = @"<body><h2>title1</h2>message<h2>title2</h2></body>";
            HtmlDocument doc = new HtmlDocument(html);

            var q = new HtmlNavigator(doc)
                .Where(n => n.TagName == "h2")
                .Select(n => n);

            Assert.AreEqual(2, q.Count());
            Assert.AreEqual("title1", q.First().Value);
            Assert.AreEqual("title2", q.ToList()[1].Value);
        }
Beispiel #6
0
        public void TestLoad2()
        {
            string html = @"<body><h1>title</h1></body>";
            HtmlDocument doc = new HtmlDocument(html);

            Assert.IsNotNull(doc.documentElement);
            Assert.AreEqual("body", doc.documentElement.TagName);
            Assert.AreEqual("", doc.documentElement.Value);
            HtmlNode root = doc.documentElement;
            HtmlNode el = root.Children;	// auto cast
            Assert.AreEqual("h1", el.TagName);
            Assert.AreEqual("title", el.Value);
        }
Beispiel #7
0
        public void TestInsert1()
        {
            string html = @"
            <body>
            <h2>title1</h2>
            <div id='m1'></div>
            <h2>title2</h2>
            <div id='m2'></div>
            </body>
            ";
            HtmlDocument doc = new HtmlDocument(html);
            var q = new HtmlNavigator(doc)
                .Where(n => n % "id" == "m1")
                .AppendChild(new HtmlNode("p", "new message"));

            Assert.AreEqual("<body><h2>title1</h2><div id=\"m1\"><p>new message</p></div><h2>title2</h2><div id=\"m2\"/></body>", doc.Html );
        }
Beispiel #8
0
        public void TestRemove1()
        {
            string html = @"
            <body>
            <h2>title1</h2>
            <span id='m1'>message</span>
            <h2>title2</h2>
            <span id='m2'>message2</span>
            </body>
            ";
            HtmlDocument doc = new HtmlDocument(html);
            var q = new HtmlNavigator(doc)
                .Where(n => n % "id" == "m2")
                .Remove();

            q = new HtmlNavigator(doc)
                .Where(n => n.TagName == "span")
                .Select(n => n);

            Assert.AreEqual(1, q.Count());
            Assert.AreEqual("message", q.Item().Value);
        }
Beispiel #9
0
 public void TestRemove2()
 {
     string html = @"
     <body>
     <h2>title1</h2>
     <span id='m1'>message</span>
     <h2>title2</h2>
     <span id='m2'>message2</span>
     </body>
     ";
     HtmlDocument doc = new HtmlDocument(html);
     var el = doc.Where(n => n.TagName == "span");
     doc.Remove( el );
     Assert.AreEqual(@"<body><h2>title1</h2><h2>title2</h2></body>", doc.Html );
 }
Beispiel #10
0
        public void TestTagName4()
        {
            string html = @"
            <body>
            <h2>title1</h2>
            <span id='msg1'>message</span>
            <h2>title2</h2>
            <span id='msg2'>message2</span>
            </body>
            ";
            HtmlDocument doc = new HtmlDocument(html);

            var q = new HtmlNavigator(doc)
                .Where(n => n % "id" == "msg2")
                .Select(n => n);

            Assert.AreEqual(1, q.Count());
            Assert.AreEqual("message2", q.First().Value);
        }
Beispiel #11
0
 public void TestParse3()
 {
     // a pattern of no body tag
     string html = @"
     <h1>title</h1>
     <div id='m1'>message</div>";
     HtmlDocument doc = new HtmlDocument(html);
     Assert.AreEqual("<body><h1>title</h1><div id=\"m1\">message</div></body>", doc.Html);
 }
Beispiel #12
0
 public void TestParse2()
 {
     string html = @"
     <body>
       <ul>
     <li>item1
     <li>item2
     <li>item3
       </ul>
     </body>";
     HtmlDocument doc = new HtmlDocument(html);
     Assert.AreEqual(@"<body><ul><li>item1</li><li>item2</li><li>item3</li></ul></body>", doc.Html);
 }
Beispiel #13
0
        public void TestHtml2()
        {
            string html = @"<body><h1>title</h1>message</body>";
            HtmlDocument doc = new HtmlDocument(html);
            Assert.AreEqual(html, doc.Html);
            doc.Where(el => el.TagName == "h1").Update(el => el.Value = "TITLE");
            Assert.AreEqual(@"<body><h1>TITLE</h1>message</body>", doc.Html);

            doc.Where(el => el.Value == "message").Update(el => el.Value = "MESSAGE");
            Assert.AreEqual(@"<body><h1>TITLE</h1>MESSAGE</body>", doc.Html);
        }
Beispiel #14
0
 public void TestParseComment2()
 {
     // test pattern has a comment tag.
     // convert to xml refenrece character
     string html = @"
     <body>
      <h1>title</h1>
      <!-- <div>comment</div> -->
      <div id='m1'>message</div>
     </body>";
     HtmlDocument doc = new HtmlDocument(html);
     Assert.AreEqual("<body><h1>title</h1><comment>&lt;div&gt;comment&lt;/div&gt;</comment><div id=\"m1\">message</div></body>", doc.Html);
 }
Beispiel #15
0
 public void TestParseComment()
 {
     // test pattern has a comment.
     // convert to new comment tag.
     string html = @"
     <body>
      <h1>title</h1>
      <!-- comment -->
      <div id='m1'>message</div>
     </body>";
     HtmlDocument doc = new HtmlDocument(html);
     Assert.AreEqual("<body><h1>title</h1><comment>comment</comment><div id=\"m1\">message</div></body>", doc.Html);
 }