Open lampo1024 opened 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);
}
}
}
For example,a document as follows:
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:
Anybody can tell me how to do this?