CsQuery icon indicating copy to clipboard operation
CsQuery copied to clipboard

How do I get html content by using prevall method?

Open lampo1024 opened this issue 10 years ago • 1 comments

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?

lampo1024 avatar Apr 05 '15 15:04 lampo1024

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);
        }
    }
}

rufanov avatar May 21 '15 03:05 rufanov