Class Text<T extends Text<T>>

java.lang.Object
com.oorian.html.Element<T>
com.oorian.html.elements.Text<T>
Direct Known Subclasses:
RawHtml

public class Text<T extends Text<T>> extends Element<T>
Represents a text node in an HTML document structure.

This class provides a way to represent plain text content within HTML documents. Unlike typical HTML elements, text nodes don't have opening/closing tags and render as raw text content. This class extends Element but overrides the rendering behavior to output only the text content without any HTML markup. Text elements automatically exclude ID attributes as they are not HTML elements.

Features:

  • Represents plain text content within HTML structure
  • Renders as raw text without HTML tags
  • Automatically excludes ID attributes
  • Trims whitespace-only content during rendering
  • Used internally by many other elements for text content
  • Supports dynamic text updates

Usage:


 // Create text node
 Text text = new Text("Hello, World!");

 // Add text to a paragraph
 P paragraph = new P();
 paragraph.addElement(new Text("This is plain text."));

 // Update text content dynamically
 Text dynamic = new Text();
 dynamic.setText("Initial text");
 // Later...
 dynamic.setText("Updated text");

 // Mix text with other elements
 Div div = new Div();
 div.addElement(new Text("Click "));
 div.addElement(new A("here", "#"));
 div.addElement(new Text(" for more info."));
 // Renders as: Click <a href="#">here</a> for more info.
 
Since:
2007
Version:
1.0
Author:
Marvin P. Warble Jr.
See Also:
  • Element
  • invalid reference
    TextElement
  • Constructor Details

    • Text

      public Text()
      Constructs an empty Text element.

      Creates a new text node with no initial content. Text can be set later using the setText(String) method. The element is configured to exclude ID attributes as text nodes are not HTML elements.

    • Text

      public Text(String text)
      Constructs a Text element with the specified content.

      Creates a new text node containing the provided text content. The element is configured to exclude ID attributes as text nodes are not HTML elements.

      Parameters:
      text - the text content for this text node
  • Method Details

    • setText

      public final void setText(String text)
      Sets the text content of this text node.

      Replaces any existing content with the specified text. This method also removes any child elements that may have been added, ensuring that only the text content is rendered.

      Overrides:
      setText in class Element<T extends Text<T>>
      Parameters:
      text - the new text content for this text node
    • getText

      public String getText()
      Gets the text content of this text node.

      Returns the current text content stored in this text node.

      Returns:
      the text content, or null if no text has been set
    • getHtml

      public void getHtml(StringBuilder sb)
      Appends the HTML-escaped text content to the provided StringBuilder.

      Overrides the default HTML rendering to output only the text content without any HTML tags or attributes. The text content is HTML-escaped to prevent XSS (Cross-Site Scripting) attacks by converting special characters (<, >, &, ", ') to their HTML entity equivalents. Empty or whitespace-only text is not appended to the output.

      Note: If you need to output raw, unescaped HTML content, use the RawHtml class instead.

      Overrides:
      getHtml in class Element<T extends Text<T>>
      Parameters:
      sb - the StringBuilder to append the escaped text content to
      See Also: