public interface HtmlPiece
An HtmlPiece can correspond to an arbitrary sequence of chars in an html document.
The only abstract method of this interface is render(indent, out),
which renders this piece as a sequence of chars.
| Abstract Method | |
|---|---|
void |
render(int indent,
Consumer<CharSequence> out)
Render this piece as a sequence of chars to `out`.
|
| Default Methods | |
CharSequence |
render(int indent)
Render this piece as a sequence of chars.
|
boolean |
isBlock()
Whether this is a "block" piece.
|
| Static Methods | |
void |
renderEscaped(CharSequence csq,
Consumer<CharSequence> out)
Write the char sequence to `out`, escaping special chars.
|
String |
indent(int n)
Return an indentation string of the n-th level.
|
void render(int indent,
Consumer<CharSequence> out)
The implementation of this method may invoke `out.accept()` multiple times.
Indentation
The `indent` parameter indicates the indentation level of this piece.
If `indent<0`, indentation should be suppressed within this piece.
If this is a parent piece, it should not introduce indentations around its children,
and it should pass negative `indent` to the children's render() methods as well.
If `indent>=0`, this piece is indented.
If this is a parent piece, it may introduce proper indentations around each of its children,
and increment `indent` by 1 when invoking the children's render() methods.
The implementation of this method does not need to add indentations
before/after this piece (it's taken care of by the parent).
Use indent(int) method to create an indentation string.
Do not introduce indentation between two inline pieces, see isBlock().
Usually `indent=0` is passed to the root piece.
Pass `indent=-1` to the root piece to suppress all indentations.
The `indent` parameter may also be simply ignored by the implementation, since indentation is not semantically important.
default CharSequence render(int indent)
The default implementation invokes `render(indent, out)` and merges outputs to one char sequence.
default boolean isBlock()
<div>...</div> element is a block piece.
If a piece is not "block", it is "inline".
This property is used by `render()` methods for indentation purposes. It is safe to introduce indentations before/after a block piece.
However, it is generally not safe to introduce spaces between two adjacent inline pieces (whether sibling-sibling or parent-child), therefore it's not safe to introduce indentation between them.
The default implementation returns `false`. When in doubt, use the default `false`, because extra spaces could be undesirable.
static void renderEscaped(CharSequence csq, Consumer<CharSequence> out)
The following chars will be escaped:
static String indent(int n)
If `n<0`, this method returns an empty string "".
If `n>=0`, this method returns a string that starts with '\n', followed by n '\t' chars.
For example, `indent(2)` returns "\n\t\t".