001package org.jsoup.nodes;
002
003import org.jsoup.helper.Validate;
004import org.jsoup.internal.StringUtil;
005
006import java.io.IOException;
007
008/**
009 A text node.
010
011 @author Jonathan Hedley, jonathan@hedley.net */
012public class TextNode extends LeafNode {
013    /**
014     Create a new TextNode representing the supplied (unencoded) text).
015
016     @param text raw text
017     @see #createFromEncoded(String)
018     */
019    public TextNode(String text) {
020        super(text);
021    }
022
023        @Override public String nodeName() {
024        return "#text";
025    }
026    
027    /**
028     * Get the text content of this text node.
029     * @return Unencoded, normalised text.
030     * @see TextNode#getWholeText()
031     */
032    public String text() {
033        return StringUtil.normaliseWhitespace(getWholeText());
034    }
035    
036    /**
037     * Set the text content of this text node.
038     * @param text unencoded text
039     * @return this, for chaining
040     */
041    public TextNode text(String text) {
042        coreValue(text);
043        return this;
044    }
045
046    /**
047     Get the (unencoded) text of this text node, including any newlines and spaces present in the original.
048     @return text
049     */
050    public String getWholeText() {
051        return coreValue();
052    }
053
054    /**
055     Test if this text node is blank -- that is, empty or only whitespace (including newlines).
056     @return true if this document is empty or only whitespace, false if it contains any text content.
057     */
058    public boolean isBlank() {
059        return StringUtil.isBlank(coreValue());
060    }
061
062    /**
063     * Split this text node into two nodes at the specified string offset. After splitting, this node will contain the
064     * original text up to the offset, and will have a new text node sibling containing the text after the offset.
065     * @param offset string offset point to split node at.
066     * @return the newly created text node containing the text after the offset.
067     */
068    public TextNode splitText(int offset) {
069        final String text = coreValue();
070        Validate.isTrue(offset >= 0, "Split offset must be not be negative");
071        Validate.isTrue(offset < text.length(), "Split offset must not be greater than current text length");
072
073        String head = text.substring(0, offset);
074        String tail = text.substring(offset);
075        text(head);
076        TextNode tailNode = new TextNode(tail);
077        if (parentNode != null)
078            parentNode.addChildren(siblingIndex()+1, tailNode);
079
080        return tailNode;
081    }
082
083    @Override
084    void outerHtmlHead(Appendable accum, int depth, Document.OutputSettings out) throws IOException {
085        final boolean prettyPrint = out.prettyPrint();
086        final boolean normaliseWhite = prettyPrint && !Element.preserveWhitespace(parentNode);
087        int escape = Entities.ForText;
088
089        if (normaliseWhite) {
090            escape |= Entities.Normalise;
091            final Element parent = parentNode instanceof Element ? ((Element) parentNode) : null;
092            final boolean trimLikeBlock = parent != null && (parent.tag().isBlock() || parent.tag().formatAsBlock());
093            if ((trimLikeBlock && siblingIndex == 0) || parentNode instanceof Document)
094                escape |= Entities.TrimLeading;
095            if (trimLikeBlock && nextSibling() == null)
096                escape |= Entities.TrimTrailing;
097
098            // if this text is just whitespace, and the next node will cause an indent, skip this text:
099            Node next = nextSibling();
100            Node prev = previousSibling();
101            boolean isBlank = isBlank();
102            boolean couldSkip = (next instanceof Element && ((Element) next).shouldIndent(out)) // next will indent
103                || (next instanceof TextNode && (((TextNode) next).isBlank())) // next is blank text, from re-parenting
104                || (prev instanceof Element && (((Element) prev).isBlock() || prev.nameIs("br"))) // br is a bit special - make sure we don't get a dangling blank line, but not a block otherwise wraps in head
105                ;
106            if (couldSkip && isBlank) return;
107
108            if (
109                (prev == null && parent != null && parent.tag().formatAsBlock() && !isBlank) ||
110                (out.outline() && siblingNodes().size() > 0 && !isBlank) ||
111                (prev != null && prev.nameIs("br")) // special case wrap on inline <br> - doesn't make sense as a block tag
112            )
113                indent(accum, depth, out);
114        }
115
116        Entities.escape(accum, coreValue(), out, escape);
117    }
118
119    @Override
120    void outerHtmlTail(Appendable accum, int depth, Document.OutputSettings out) throws IOException {}
121
122    @Override
123    public String toString() {
124        return outerHtml();
125    }
126
127    @Override
128    public TextNode clone() {
129        return (TextNode) super.clone();
130    }
131
132    /**
133     * Create a new TextNode from HTML encoded (aka escaped) data.
134     * @param encodedText Text containing encoded HTML (e.g. {@code &lt;})
135     * @return TextNode containing unencoded data (e.g. {@code <})
136     */
137    public static TextNode createFromEncoded(String encodedText) {
138        String text = Entities.unescape(encodedText);
139        return new TextNode(text);
140    }
141
142    static String normaliseWhitespace(String text) {
143        text = StringUtil.normaliseWhitespace(text);
144        return text;
145    }
146
147    static String stripLeadingWhitespace(String text) {
148        return text.replaceFirst("^\\s+", "");
149    }
150
151    static boolean lastCharIsWhitespace(StringBuilder sb) {
152        return sb.length() != 0 && sb.charAt(sb.length() - 1) == ' ';
153    }
154}