public abstract class HtmlElement extends Object implements HtmlPiece
An html element has attributes. An attribute is a name-value pair. For example
HtmlElement input = new Html4.INPUT(); input.setAttribute("id", "q"); input.setAttribute("disabled", ""); input.render(0, System.out::print); // <input id="q" disabled>
For
boolean attributes,
use "" as true, null
as false, for example
input.setAttribute("disabled", ""); // <input name="q" disabled> input.setAttribute("disabled", null); // <input name="q"> // not disabled
This is an abstract class; a concrete subclass needs to implement getElementType()
.
See HtmlParentElement
for elements that can contain children.
Modifier | Constructor and Description |
---|---|
protected |
HtmlElement()
Protected constructor for subclasses.
|
Abstract Method | |
---|---|
HtmlElementType |
getElementType()
Get the metadata of this element.
|
Instance Methods | |
boolean |
isBlock()
Whether this is a "block" element.
|
int |
getAttributeCount()
Get the number of attributes.
|
CharSequence |
getAttribute(String name)
Get the value of the attribute; null if the attribute is not present.
|
CharSequence |
setAttribute(String name,
CharSequence value)
Set the value of the attribute.
|
Map<String,CharSequence> |
getAttributeMap()
Get a live Map view of the attributes.
|
void |
render(int indent,
Consumer<CharSequence> out)
Render this element to `out`.
|
public abstract HtmlElementType getElementType()
HtmlElementType
.public boolean isBlock()
This method is equivalent to `getElementType().isBlock`.
public int getAttributeCount()
public CharSequence getAttribute(String name)
public CharSequence setAttribute(String name, CharSequence value)
If `value` is null, the attribute is removed.
The value can contain any chars. It can be empty.
public Map<String,CharSequence> getAttributeMap()
The returned Map supports read and write.
public void render(int indent, Consumer<CharSequence> out)
An example rendering: <input name="q" disabled>
If an attribute value is empty, only the name is rendered. For example.
setAttribute("disabled", "") ==> <input disabled>
Special chars in attribute values will be escaped,
using HtmlPiece.renderEscaped()
.
For example
setAttribute("name", "a&b") ==> <input name="a&b">