Class Pre<T extends Pre<T>>


public class Pre<T extends Pre<T>> extends PhrasingContentElement<T>
Represents an HTML <pre> (preformatted text) element that preserves whitespace and line breaks.

The <pre> element displays text exactly as it is written in the HTML source code, preserving spaces, tabs, and line breaks. It is typically displayed in a monospace font and is commonly used for displaying code snippets, ASCII art, or any content where whitespace formatting is significant.

Features:

  • Preserves all whitespace including spaces, tabs, and line breaks
  • Displays text in monospace font by default
  • No automatic text wrapping (unless specified with CSS)
  • Ideal for displaying code, terminal output, or formatted text
  • Supports text content and nested HTML elements
  • Commonly used with <code> element for code blocks

Usage:


 // Code snippet display
 Pre codeBlock = new Pre();
 codeBlock.addText("public class Hello {\n");
 codeBlock.addText("    public static void main(String[] args) {\n");
 codeBlock.addText("        System.out.println(\"Hello, World!\");\n");
 codeBlock.addText("    }\n");
 codeBlock.addText("}\n");

 // Constructor with text
 Pre asciiArt = new Pre(
     "  /\\_/\\\n" +
     " ( o.o )\n" +
     "  > ^ <"
 );

 // Terminal output
 Pre terminal = new Pre();
 terminal.addText("$ ls -la\n");
 terminal.addText("total 48\n");
 terminal.addText("drwxr-xr-x  12 user  staff   384 Jan 15 10:30 .\n");
 terminal.addText("drwxr-xr-x   8 user  staff   256 Jan 15 09:15 ..\n");

 // Code with syntax highlighting container
 Pre styledCode = new Pre();
 Code javaCode = new Code();
 javaCode.addText("int x = 42;");
 styledCode.addElement(javaCode);
 
Since:
2007
Version:
1.0
Author:
Marvin P. Warble Jr.
See Also:
  • Constructor Details

    • Pre

      public Pre()
      Constructs a new empty Pre element.

      Creates an HTML <pre> element with no initial content. Text can be added later, and all whitespace will be preserved.

    • Pre

      public Pre(String text)
      Constructs a new Pre element with the specified preformatted text.

      Creates an HTML <pre> element containing the specified text. All whitespace, including spaces, tabs, and line breaks in the text will be preserved exactly as provided.

      Parameters:
      text - the preformatted text content