Beispiel #1
0
        public void WhenBaseUrlIsNullItHandlesAbsoluteAnchorLinks()
        {
            // Arrange
            string       html = @"
<html>
  <body>
    <p>Hello World: <a href=""http://link1"" rel=""next"">Link no. 1</a></p>
  </body>
</html>";
            HtmlDocument doc  = new HtmlDocument();

            doc.LoadHtml(html);

            // Act
            ILink l1 = doc.Anchors((Uri)null).Select("next");
            ILink l2 = doc.Anchors((Response)null).Select("next");
            ILink l3 = doc.DocumentNode.Anchors((Uri)null).Select("next");
            ILink l4 = doc.DocumentNode.Anchors((Response)null).Select("next");
            ILink l5 = doc.DocumentNode.SelectNodes("//*").Anchors((Uri)null).Select("next");
            ILink l6 = doc.DocumentNode.SelectNodes("//*").Anchors((Response)null).Select("next");
            ILink l7 = doc.DocumentNode.SelectNodes("//a").First().Anchor((Uri)null);
            ILink l8 = doc.DocumentNode.SelectNodes("//a").First().Anchor((Response)null);

            // Assert
            Assert.AreEqual("http://link1/", l1.HRef.AbsoluteUri);
            Assert.AreEqual("http://link1/", l2.HRef.AbsoluteUri);
            Assert.AreEqual("http://link1/", l3.HRef.AbsoluteUri);
            Assert.AreEqual("http://link1/", l4.HRef.AbsoluteUri);
            Assert.AreEqual("http://link1/", l5.HRef.AbsoluteUri);
            Assert.AreEqual("http://link1/", l6.HRef.AbsoluteUri);
            Assert.AreEqual("http://link1/", l7.HRef.AbsoluteUri);
            Assert.AreEqual("http://link1/", l8.HRef.AbsoluteUri);
        }
Beispiel #2
0
        public void CanExtractAnchorLinksFromHtmlDocument()
        {
            // Act
            List <Anchor> links = HtmlDoc.Anchors(BaseUrl).ToList();

            // Assert
            Assert.AreEqual(5, links.Count);

            ILink l1 = links[0];

            Assert.AreEqual("http://link1/", l1.HRef.AbsoluteUri);
            Assert.AreEqual("Link no. 1", l1.Title);
            Assert.Contains("next", l1.RelationTypes.ToList());

            ILink l2 = links[2];

            Assert.AreEqual("http://link2/", l2.HRef.AbsoluteUri);
            Assert.AreEqual("Link no. 2", l2.Title);
            Assert.Contains("up", l2.RelationTypes.ToList());
        }