Skip to content

Commit

Permalink
Merge branch 'feature/fix'
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaohuo committed Sep 27, 2020
2 parents 0f3909a + 39b1aa7 commit 94fc9c7
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ maven依赖,全版本请参见[release信息](https://github.com/zhegexiaohuozi/
<dependency>
<groupId>cn.wanghaomiao</groupId>
<artifactId>JsoupXpath</artifactId>
<version>2.4.1</version>
<version>2.4.2</version>
</dependency>
```

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</parent>
<groupId>cn.wanghaomiao</groupId>
<artifactId>JsoupXpath</artifactId>
<version>2.4.1</version>
<version>2.4.2</version>
<modelVersion>4.0.0</modelVersion>
<packaging>jar</packaging>
<name>JsoupXpath</name>
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/org/seimicrawler/xpath/core/node/Text.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -65,6 +67,13 @@ public void head(Node node, int depth) {
}
Element data = new Element(Constants.DEF_TEXT_TAG_NAME);
data.text(textNode.getWholeText());
try {
Method parent = Node.class.getDeclaredMethod("setParentNode",Node.class);
parent.setAccessible(true);
parent.invoke(data,textNode.parent());
} catch (Exception e) {
//ignore
}
CommonUtil.setSameTagIndexInSiblings(data,index);
res.add(data);
}
Expand Down
24 changes: 23 additions & 1 deletion src/test/java/org/seimicrawler/xpath/JXDocumentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,11 @@ public void testA(){
}
}

/**
* fix https://github.com/zhegexiaohuozi/JsoupXpath/issues/52
*/
@Test
public void FixTextBehaviorTest(){
public void fixTextBehaviorTest(){
String html = "<p><span class=\"text-muted\">分类:</span>动漫<span class=\"split-line\"></span><span class=\"text-muted hidden-xs\">地区:</span>日本<span class=\"split-line\"></span><span class=\"text-muted hidden-xs\">年份:</span>2010</p>";
JXDocument jxDocument = JXDocument.create(html);
List<JXNode> jxNodes = jxDocument.selN("//text()[3]");
Expand All @@ -232,7 +235,26 @@ public void FixTextBehaviorTest(){
Assert.assertEquals("2010", actual);
List<JXNode> nodes = jxDocument.selN("//text()");
String allText = StringUtils.join(nodes,"");
Assert.assertEquals("分类:动漫地区:日本年份:2010",allText);
logger.info("all = {}",allText);
}

/**
* fix https://github.com/zhegexiaohuozi/JsoupXpath/issues/44
*/
@Test
public void fixTextElNoParentTest(){
String test="<div class='a'> a <div>need</div> <div class='e'> not need</div> c </div>";
JXDocument j = JXDocument.create(test);
List<JXNode> l = j.selN("//div[@class='a']//text()[not(ancestor::div[@class='e'])]");
Set<String> finalRes = new HashSet<>();
for (JXNode i : l){
logger.info("{}",i.toString());
finalRes.add(i.asString());
}
Assert.assertFalse(finalRes.contains("not need"));
Assert.assertTrue(finalRes.contains("need"));
Assert.assertEquals(4, finalRes.size());
}

}

0 comments on commit 94fc9c7

Please sign in to comment.