jamietre / CsQuery

CsQuery is a complete CSS selector engine, HTML parser, and jQuery port for C# and .NET 4.
Other
1.16k stars 249 forks source link

How do I get html content by using prevall method? #185

Open lampo1024 opened 9 years ago

lampo1024 commented 9 years ago

For example,a document as follows: firefox_screenshot_2015-04-05t15-16-50 050z

Now I want to get all content within the 'article' div,include the innerhtml(such as p and b),but except the last "b".The result would like this: firefox_screenshot_2015-04-05t15-17-02 120z

Anybody can tell me how to do this?

rufanov commented 9 years ago

You can't use prevAll(), because it don't work with text nodes - just like when using jQuery.

To accomplish this, you can select last bold element, then exclude it from Select(".article").Contents() using Not() method:

using Microsoft.VisualStudio.TestTools.UnitTesting;
using NUnit.Framework;
using Assert = NUnit.Framework.Assert;

namespace CsQuery.Tests.Issues
{
    [TestFixture, TestClass]
    public class Issue185 : CsQueryTest
    {
        private const string html =
           "<div class='article'>"+
                "Hello World!"+
                "<p>I am using CsQuery library.</p>"+
                "<b>I am feeling bold!</b>"+
                "<b>I am the second bold!</b>"+
                "What about"+
                "<b>you?</b>"+
            "</div>";

        private const string expected =
            "Hello World!" +
            "<p>I am using CsQuery library.</p>" +
            "<b>I am feeling bold!</b>" +
            "<b>I am the second bold!</b>" +
            "What about";

        [Test, TestMethod]
        public void Issue185Test()
        {
            CQ document = CQ.Create(html);

            var lastBold = document.Select(".article b").Last();
            var contents = document.Select(".article").Contents().Not(lastBold);
            var actual = contents.RenderSelection();

            Assert.AreEqual(expected, actual);
        }
    }
}