下面列出了org.jsoup.nodes.TextNode#text ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void appendTextSkipHidden(Element e, StringBuilder accum, int indent) {
for (Node child : e.childNodes()) {
if (unlikely(child)) {
continue;
}
if (child instanceof TextNode) {
TextNode textNode = (TextNode) child;
String txt = textNode.text();
accum.append(txt);
} else if (child instanceof Element) {
Element element = (Element) child;
if (accum.length() > 0 && element.isBlock()
&& !lastCharIsWhitespace(accum))
accum.append(' ');
else if (element.tagName().equals("br"))
accum.append(' ');
appendTextSkipHidden(element, accum, indent + 1);
}
}
}
@Override
public String operate(Element element) {
int index = 0;
StringBuilder accum = new StringBuilder();
for (Node node : element.childNodes()) {
if (node instanceof TextNode) {
TextNode textNode = (TextNode) node;
if (group == 0) {
accum.append(textNode.text());
} else if (++index == group) {
return textNode.text();
}
}
}
return accum.toString();
}
private void appendTextSkipHidden(Element e, StringBuilder accum, int indent) {
for (Node child : e.childNodes()) {
if (unlikely(child)) {
continue;
}
if (child instanceof TextNode) {
TextNode textNode = (TextNode) child;
String txt = textNode.text();
accum.append(txt);
} else if (child instanceof Element) {
Element element = (Element) child;
if (accum.length() > 0 && element.isBlock()
&& !lastCharIsWhitespace(accum))
accum.append(' ');
else if (element.tagName().equals("br"))
accum.append(' ');
appendTextSkipHidden(element, accum, indent + 1);
}
}
}
@Override
public void head(Node node, int depth) {
if (node instanceof TextNode) {
TextNode text = (TextNode) node;
String textContent = text.text();
if (textLen >= maxTextLen) {
text.text("");
} else if (textLen + textContent.length() > maxTextLen) {
int ptr = maxTextLen - textLen;
if (!killwords) {
ptr = Functions.movePointerToJustBeforeLastWord(ptr, textContent) - 1;
}
text.text(textContent.substring(0, ptr) + ending);
textLen = maxTextLen;
} else {
textLen += textContent.length();
}
}
}
@Override
public String operate(Element element) {
int index = 0;
StringBuilder accum = new StringBuilder();
for (Node node : element.childNodes()) {
if (node instanceof TextNode) {
TextNode textNode = (TextNode) node;
if (group == 0) {
accum.append(textNode.text());
} else if (++index == group) {
return textNode.text();
}
}
}
return accum.toString();
}
static void cleanup(Document d) {
// https://www.chromestatus.com/feature/5756335865987072
// Some messages contain 100 thousands of Apple spaces
for (Element aspace : d.select(".Apple-converted-space")) {
Node next = aspace.nextSibling();
if (next instanceof TextNode) {
TextNode tnode = (TextNode) next;
tnode.text(" " + tnode.text());
aspace.remove();
} else
aspace.replaceWith(new TextNode(" "));
}
}
static boolean truncate(Document d, boolean reformat) {
int max = (reformat ? MAX_FORMAT_TEXT_SIZE : MAX_FULL_TEXT_SIZE);
int length = 0;
int images = 0;
for (Element elm : d.select("*")) {
if ("img".equals(elm.tagName()))
images++;
boolean skip = false;
for (Node child : elm.childNodes()) {
if (child instanceof TextNode) {
TextNode tnode = ((TextNode) child);
String text = tnode.getWholeText();
if (length < max) {
if (length + text.length() >= max) {
text = text.substring(0, max - length) + " ...";
tnode.text(text);
skip = true;
}
} else {
if (skip)
tnode.text("");
}
length += text.length();
}
}
if (length >= max && !skip)
elm.remove();
}
Log.i("Message size=" + length + " images=" + images);
return (length >= max);
}
/**
* Inserts the text of the given {@link TextNode}.
*
* @param parent
* the parent {@link MList}
* @param context
* the {@link Context}
* @param node
* the {@link TextNode}
*/
private void insertText(MList parent, final Context context, TextNode node) {
final String text = node.text();
if (!text.trim().isEmpty()) {
if (context.linkTargetURI == null) {
final MText mText = new MTextImpl(text, context.style);
parent.add(mText);
} else {
context.style.setForegroundColor(LINK_COLOR);
final MHyperLink mLink = new MHyperLinkImpl(text, context.style, context.linkTargetURI.toString());
parent.add(mLink);
}
}
}
public CountInfo(TextNode tNode) {
this.tNode = tNode;
String text = tNode.text();
this.textCount = TextUtils.countText(text);
this.puncCount = TextUtils.countPunc(text);
}
public CountInfo(TextNode tNode) {
this.tNode = tNode;
String text = tNode.text();
this.textCount = TextUtils.countText(text);
this.puncCount = TextUtils.countPunc(text);
}