コード例 #1
0
        public void TestComments()
        {
            XmlLightDocument doc = new HtmlLightDocument(document);
            XmlLightElement  e   = doc.SelectSingleNode("/html/head");

            e = e.NextSibling;
            Assert.IsTrue(e.IsComment);
            Assert.AreEqual("<!-- comments included -->", e.InnerXml);
        }
コード例 #2
0
        public void TestInnerText()
        {
            XmlLightDocument doc = new HtmlLightDocument(document);
            XmlLightElement  e   = doc.SelectSingleNode("/html/body");

            Assert.AreEqual("this is > cdata! Hi, this is content.", Normalize(e.InnerText));
            Assert.AreEqual("Hi", e.SelectSingleNode(".//div[@id='two']").InnerText);
            Assert.AreEqual("this is > cdata!", Normalize(e.SelectSingleNode("text()").InnerText));
        }
コード例 #3
0
        public void TestXPath()
        {
            XmlDocument      xdoc     = new XmlDocument();
            XmlLightDocument doc      = new HtmlLightDocument(document);
            string           testpath = "/html/body[@id='one' and @class='cls']/../body/div[@id='two' and text() = 'Hi']/@id";

            xdoc.LoadXml(doc.CreateNavigator().InnerXml);
            Assert.IsNotNull(xdoc.SelectSingleNode(testpath));
            XPathNavigator nav = doc.CreateNavigator().SelectSingleNode(testpath);

            Assert.IsNotNull(nav);
            Assert.IsTrue(nav.NodeType == XPathNodeType.Attribute);
            Assert.AreEqual("id", nav.Name);
            Assert.AreEqual("two", nav.Value);

            XmlLightElement e = doc.SelectSingleNode("/html/Head");

            Assert.IsNull(e);
            e = doc.SelectSingleNode("/html/head");
            Assert.IsNotNull(e);
        }
コード例 #4
0
        public bool TryGetPingbackFromHtml(out Uri pingbackApi)
        {
            HttpRequestUtil http = new HttpRequestUtil(_targetLink);

            if (http.Get(_targetLink.PathAndQuery) != System.Net.HttpStatusCode.OK)
            {
                LogError(String.Format("GET {0}: {1}/{2}", _targetLink, (int)http.StatusCode, http.StatusCode));
            }
            else if (!http.ContentType.StartsWith("text/html", StringComparison.OrdinalIgnoreCase))
            {
                LogError("Invalid content-type, expected text/html, found: " + http.ContentType);
            }
            else
            {
                try
                {
                    HtmlLightDocument htmlDoc = new HtmlLightDocument(Encoding.UTF8.GetString(http.Content));
                    XmlLightElement   link    = htmlDoc.SelectSingleNode("/html/head/link[@rel='pingback']");
                    if (link == null)
                    {
                        LogError("Unable to locate <link rel=\"pingback\" ... in header.");
                    }
                    else
                    {
                        string pingback;
                        if (!link.Attributes.TryGetValue("href", out pingback))
                        {
                            LogError("Link for rel=pingback is missing the href attribute.");
                        }
                        else
                        {
                            LogInfo("Found rel=pingback: " + pingback);
                            return(Uri.TryCreate(pingback, UriKind.Absolute, out pingbackApi));
                        }
                    }
                }
                catch (Exception e)
                {
                    LogError(e.Message);
                }
            }
            pingbackApi = null;
            return(false);
        }
コード例 #5
0
        public void TestXmlElement()
        {
            XmlLightDocument doc = new HtmlLightDocument(document);

            Assert.IsNull(doc.PrevSibling);
            Assert.IsNull(doc.Children[0].PrevSibling);
            Assert.IsNull(doc.NextSibling);
            Assert.IsNull(doc.Children[doc.Children.Count - 1].NextSibling);

            XmlLightElement e = doc.SelectSingleNode("/html/body//*[@class='2']");

            Assert.IsNotNull(e);
            Assert.AreEqual("p", e.TagName);
            Assert.IsNotNull(e.PrevSibling);
            Assert.AreEqual("p", e.PrevSibling.TagName);

            Assert.AreEqual("", e.Namespace);
            Assert.AreEqual("p", e.LocalName);

            e = new XmlLightElement(null, "a:b");
            Assert.AreEqual("a", e.Namespace);
            Assert.AreEqual("b", e.LocalName);
        }
コード例 #6
0
        public bool TryGetPingbackFromHtml(out Uri pingbackApi)
        {
            HttpRequestUtil http = new HttpRequestUtil(_targetLink);

            if (http.Get(_targetLink.PathAndQuery) != System.Net.HttpStatusCode.OK)
                LogError(String.Format("GET {0}: {1}/{2}", _targetLink, (int)http.StatusCode, http.StatusCode));
            else if (!http.ContentType.StartsWith("text/html", StringComparison.OrdinalIgnoreCase))
                LogError("Invalid content-type, expected text/html, found: " + http.ContentType);
            else
            {
                try
                {
                    HtmlLightDocument htmlDoc = new HtmlLightDocument(Encoding.UTF8.GetString(http.Content));
                    XmlLightElement link = htmlDoc.SelectSingleNode("/html/head/link[@rel='pingback']");
                    if (link == null)
                        LogError("Unable to locate <link rel=\"pingback\" ... in header.");
                    else
                    {
                        string pingback;
                        if (!link.Attributes.TryGetValue("href", out pingback))
                            LogError("Link for rel=pingback is missing the href attribute.");
                        else
                        {
                            LogInfo("Found rel=pingback: " + pingback);
                            return Uri.TryCreate(pingback, UriKind.Absolute, out pingbackApi);
                        }
                    }
                }
                catch (Exception e)
                {
                    LogError(e.Message);
                }
            }
            pingbackApi = null;
            return false;
        }
コード例 #7
0
		public void TestComments()
		{
			XmlLightDocument doc = new HtmlLightDocument(document);
			XmlLightElement e = doc.SelectSingleNode("/html/head");
			e = e.NextSibling;
			Assert.IsTrue(e.IsComment);
			Assert.AreEqual("<!-- comments included -->", e.InnerXml);
		}
コード例 #8
0
		public void TestInnerText()
		{
			XmlLightDocument doc = new HtmlLightDocument(document);
			XmlLightElement e = doc.SelectSingleNode("/html/body");
			Assert.AreEqual("this is > cdata! Hi, this is content.", Normalize(e.InnerText));
			Assert.AreEqual("Hi", e.SelectSingleNode(".//div[@id='two']").InnerText);
			Assert.AreEqual("this is > cdata!", Normalize(e.SelectSingleNode("text()").InnerText));
		}
コード例 #9
0
		public void TestXmlElement()
		{
			XmlLightDocument doc = new HtmlLightDocument(document);
			Assert.IsNull(doc.PrevSibling);
			Assert.IsNull(doc.Children[0].PrevSibling);
			Assert.IsNull(doc.NextSibling);
			Assert.IsNull(doc.Children[doc.Children.Count - 1].NextSibling);

			XmlLightElement e = doc.SelectSingleNode("/html/body//*[@class='2']");
			Assert.IsNotNull(e);
			Assert.AreEqual("p", e.TagName);
			Assert.IsNotNull(e.PrevSibling);
			Assert.AreEqual("p", e.PrevSibling.TagName);

			Assert.AreEqual("", e.Namespace);
			Assert.AreEqual("p", e.LocalName);

			e = new XmlLightElement(null, "a:b");
			Assert.AreEqual("a", e.Namespace);
			Assert.AreEqual("b", e.LocalName);
		}
コード例 #10
0
		public void TestXPath()
		{
			XmlDocument xdoc = new XmlDocument();
			XmlLightDocument doc = new HtmlLightDocument(document);
			string testpath = "/html/body[@id='one' and @class='cls']/../body/div[@id='two' and text() = 'Hi']/@id";

			xdoc.LoadXml(doc.CreateNavigator().InnerXml);
			Assert.IsNotNull(xdoc.SelectSingleNode(testpath));
			XPathNavigator nav = doc.CreateNavigator().SelectSingleNode(testpath);

			Assert.IsNotNull(nav);
			Assert.IsTrue(nav.NodeType == XPathNodeType.Attribute);
			Assert.AreEqual("id", nav.Name);
			Assert.AreEqual("two", nav.Value);

			XmlLightElement e = doc.SelectSingleNode("/html/Head");
			Assert.IsNull(e);
			e = doc.SelectSingleNode("/html/head");
			Assert.IsNotNull(e);
		}