Skip to content

Commit

Permalink
Simplified TextNode.outerHtml a little
Browse files Browse the repository at this point in the history
  • Loading branch information
jhy committed Jan 2, 2022
1 parent 3d136d7 commit f4d00c2
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
13 changes: 12 additions & 1 deletion src/main/java/org/jsoup/internal/StringUtil.java
Expand Up @@ -147,7 +147,7 @@ public static String padding(int width, int maxPaddingWidth) {
* @param string string to test
* @return if string is blank
*/
public static boolean isBlank(String string) {
public static boolean isBlank(final String string) {
if (string == null || string.length() == 0)
return true;

Expand All @@ -159,6 +159,17 @@ public static boolean isBlank(String string) {
return true;
}

/**
Tests if a string starts with a newline character
@param string string to test
@return if its first character is a newline
*/
public static boolean startsWithNewline(final String string) {
if (string == null || string.length() == 0)
return false;
return string.charAt(0) == '\n';
}

/**
* Tests if a string is numeric, i.e. contains only digit characters
* @param string string to test
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/org/jsoup/nodes/LeafNode.java
Expand Up @@ -38,9 +38,8 @@ void coreValue(String value) {

@Override
public String attr(String key) {
Validate.notNull(key);
if (!hasAttributes()) {
return key.equals(nodeName()) ? (String) value : EmptyString;
return nodeName().equals(key) ? (String) value : EmptyString;
}
return super.attr(key);
}
Expand Down
14 changes: 8 additions & 6 deletions src/main/java/org/jsoup/nodes/TextNode.java
Expand Up @@ -74,20 +74,22 @@ public TextNode splitText(int offset) {
String tail = text.substring(offset);
text(head);
TextNode tailNode = new TextNode(tail);
if (parent() != null)
parent().addChildren(siblingIndex()+1, tailNode);
if (parentNode != null)
parentNode.addChildren(siblingIndex()+1, tailNode);

return tailNode;
}

void outerHtmlHead(Appendable accum, int depth, Document.OutputSettings out) throws IOException {
final boolean prettyPrint = out.prettyPrint();
Element parent = parentNode instanceof Element ? ((Element) parentNode) : null;
boolean parentIndent = parent != null && parent.shouldIndent(out);
if (parentIndent && getWholeText().startsWith("\n") && isBlank()) // we are skippable whitespace
final Element parent = parentNode instanceof Element ? ((Element) parentNode) : null;
final boolean parentIndent = parent != null && parent.shouldIndent(out);
final boolean blank = isBlank();

if (parentIndent && StringUtil.startsWithNewline(coreValue()) && blank) // we are skippable whitespace
return;

if (prettyPrint && ((siblingIndex() == 0 && parent != null && parent.tag().formatAsBlock() && !isBlank()) || (out.outline() && siblingNodes().size()>0 && !isBlank()) ))
if (prettyPrint && ((siblingIndex == 0 && parent != null && parent.tag().formatAsBlock() && !blank) || (out.outline() && siblingNodes().size()>0 && !blank) ))
indent(accum, depth, out);

final boolean normaliseWhite = prettyPrint && !Element.preserveWhitespace(parentNode);
Expand Down

0 comments on commit f4d00c2

Please sign in to comment.