Interface Element
- All Superinterfaces:
DomQueryable
,EventTarget
,Node
- All Known Subinterfaces:
BodyElement
,ButtonElement
,CanvasElement
,ComponentElement
,DelphiElement
,HeadElement
,InputElement
,ItemElement
,JavaObjectElement
,OptionElement
,ScriptElement
,StyleElement
Elements are not guaranteed to have children. An element can only support children if
canHaveChildren()
returns true
.
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionvoid
appendChild
(@NotNull Node node) Adds a child node to the end of this element's child nodes.default @NotNull Element
appendElement
(@NotNull String tagName) Appends an element and returns it.default @NotNull TextNode
appendText
(@Nullable String text) Creates a text node and appends it to this elementboolean
Tests if this element is capable of having child nodes.void
Remove all child elements.@Nullable Node
Gets the first child of this element.void
forEachDescendant
(@NotNull Consumer<Node> consumer) Applies a consumer to all descendant nodes of this element.@Nullable String
getAttribute
(String key) Gets the value of an attribute.Gets a set of attribute entries that exist on this element.Get a set of attribute keys that exist on this element.getChild
(int index) Gets the nth child of this element.int
Gets the amount of children the element hasGets an immutable list of child nodesGets the element's class array list.default @Nullable String
Gets the element'sAttributes.CLASS
attribute value.Get the element's current style.default String
getId()
Shorthand for getting the id attribute's valueGet the modifiable inline style properties.Gets the element's tag name.Get the element's text content.@Nullable Node
Gets the element's title node.@Nullable Node
Gets the element's tooltip.boolean
hasAttribute
(@Nullable String key) Test if the element has an attribute with a mapped value.boolean
Tests if the specifiednode
is a direct child of this elementboolean
Tests if this element has children.int
Gets the index of a node that is the direct child of this element.void
insertAfter
(@NotNull Node node, @NotNull Node after) Inserts an element after the specified nodevoid
insertBefore
(@NotNull Node node, @NotNull Node before) Inserts an element before the specified nodeboolean
isDescendant
(@Nullable Node node) Test if a specifiednode
is a descendant of the element.@Nullable Node
Gets the last child of this element.boolean
Test if the element matches a CSS selector.void
prependChild
(@NotNull Node node) Adds a child node to start of this element's child nodes.@Nullable String
removeAttribute
(String attributeName) Remove an attribute.void
removeChild
(int childIndex) Removes a child element by its index.boolean
removeChild
(@NotNull Node node) Removes a specified child from this element.void
removeMatchingChildren
(@NotNull Predicate<Node> filter) Remove all child nodes that match the specifiedfilter
predicate.void
replaceChild
(int idx, @NotNull Node node) Replace a child element at the specified index.void
replaceChild
(@NotNull Node child, @NotNull Node node) Replace a child element at the specified index.void
setAttribute
(@NotNull String key, @Nullable String value) Sets an attribute's value.default void
setClassName
(String className) Sets the element'sAttributes.CLASS
attribute valuedefault void
Shorthand for setting the id attributevoid
setTextContent
(String content) Remove all child elements and replace it with the specifiedcontent
void
setTitleNode
(@Nullable Node title) Sets the element's title node.Methods inherited from interface com.juliewoolie.dom.DomQueryable
getElementsByClassName, getElementsByTagName, querySelector, querySelectorAll
Methods inherited from interface com.juliewoolie.dom.event.EventTarget
addEventListener, dispatchEvent, getOnAddAttribute, getOnAddOption, getOnAppendChild, getOnAttributeChange, getOnClick, getOnClosing, getOnInput, getOnLoaded, getOnMouseEnter, getOnMouseExit, getOnMouseMove, getOnOptionChange, getOnRemoveAttribute, getOnRemoveChild, getOnRemoveOption, getOnRightClick, getOnSetAttribute, getOnSetOption, getOnSpawned, onAddAttribute, onAddOption, onAppendChild, onAttributeChange, onClick, onClosing, onInput, onLoaded, onMouseEnter, onMouseExit, onMouseMove, onOptionChange, onRemoveAttribute, onRemoveChild, onRemoveOption, onRightClick, onSetAttribute, onSetOption, onSpawned, removeEventListener
Methods inherited from interface com.juliewoolie.dom.Node
enterVisitor, exitVisitor, getDepth, getOwningDocument, getParent, getRenderingBounds, getSiblingIndex, hasFlag, nextSibling, previousSibling
-
Method Details
-
getInlineStyle
StyleProperties getInlineStyle()Get the modifiable inline style properties.Any changes made to the returned properties will be reflected in the
Attributes.STYLE
attribute, and any changes to theAttributes.STYLE
attribute will be reflected in the returned properties.- Returns:
- Inline style
-
getCurrentStyle
StylePropertiesReadonly getCurrentStyle()Get the element's current style.The returned result's properties will be the computed style values of all applicable stylesheet rules and the inline style for the element.
- Returns:
- Unmodifiable style properties
-
getAttribute
-
hasAttribute
Test if the element has an attribute with a mapped value.- Parameters:
key
- Attribute name- Returns:
true
, if the element has a value mapped for the specified attribute,false
otherwise.
-
setAttribute
Sets an attribute's value.If the specified
value
is already set as the value for this attribute, nothing will change.
Otherwise, anEventTypes.MODIFY_ATTR
event will be triggered after the value is set.- Parameters:
key
- Attribute keyvalue
- Attribute value, ornull
to remove.- Throws:
NullPointerException
- ifkey
isnull
or empty
-
removeAttribute
Remove an attribute.If the specified attribute isn't set for this attribute, nothing will change.
Otherwise, anEventTypes.MODIFY_ATTR
event will be triggered after the attribute is removed- Parameters:
attributeName
- Attribute name- Returns:
- The attribute's value before being removed
- Throws:
NullPointerException
- ifattributeName
isnull
or empty
-
getAttributeNames
-
getAttributeEntries
-
getId
-
setId
Shorthand for setting the id attribute- Parameters:
elementId
- Element ID- See Also:
-
getClassName
Gets the element'sAttributes.CLASS
attribute value.- Returns:
- Class name
-
setClassName
Sets the element'sAttributes.CLASS
attribute value- Parameters:
className
- Class name
-
getClassList
Gets the element's class array list.Any modifications made to the returned array will be reflected in the element's
class
attribute andgetCurrentStyle()
- Returns:
- An array list containing all the classes of this element.
-
getTagName
-
getTooltip
-
getTitleNode
Gets the element's title node.The title node is used as the element's hover tooltip.
- Returns:
- Title node
-
setTitleNode
Sets the element's title node.The title node is used as the element's hover tooltip.
- Parameters:
title
- Title node
-
appendChild
Adds a child node to the end of this element's child nodes.If the specified
node
belongs to a different document, it will be adopted to the current document. if thenode
already has a parent, it will be orphaned before addition.Will trigger an
EventTypes.APPEND_CHILD
event. Can additionally trigger aEventTypes.REMOVE_CHILD
event on its previous parent, if it has one.- Parameters:
node
- Node- Throws:
NullPointerException
- ifnode
isnull
-
prependChild
Adds a child node to start of this element's child nodes.If the specified
node
belongs to a different document, it will be adopted to the current document. if thenode
already has a parent, it will be orphaned before addition.Will trigger an
EventTypes.APPEND_CHILD
event. Can additionally trigger aEventTypes.REMOVE_CHILD
event on its previous parent, if it has one.- Parameters:
node
- Prepended node- Throws:
NullPointerException
- ifnode
isnull
-
insertBefore
Inserts an element before the specified nodeIf the specified
node
belongs to a different document, it will be adopted to the current document. if thenode
already has a parent, it will be orphaned before addition.If the specified
before
node does not belong to the children of this element, then nothing happens.Will trigger an
EventTypes.APPEND_CHILD
event. Can additionally trigger aEventTypes.REMOVE_CHILD
event on its previous parent, if it has one.- Parameters:
node
- Node to insertbefore
- Node to insert before- Throws:
NullPointerException
- If eithernode
orbefore
arenull
-
insertAfter
Inserts an element after the specified nodeIf the specified
node
belongs to a different document, it will be adopted to the current document. if thenode
already has a parent, it will be orphaned before addition.If the specified
after
node does not belong to the children of this element, then nothing happens.Will trigger an
EventTypes.APPEND_CHILD
event. Can additionally trigger aEventTypes.REMOVE_CHILD
event on its previous parent, if it has one.- Parameters:
node
- Node to insertafter
- Node to insert after- Throws:
NullPointerException
- If eithernode
orafter
arenull
-
replaceChild
Replace a child element at the specified index.If the specified node belongs to a different element or document, it will be removed from its previous document and element and adopted to this one.
Will trigger an
EventTypes.REMOVE_CHILD
on the child node that's being replaced and then aEventTypes.APPEND_CHILD
when the node is inserted into the child node list. Can additionally trigger aEventTypes.REMOVE_CHILD
event on the specified node's parent element, if there is one.- Parameters:
idx
- Index of the child to replacenode
- Node to replace with- Throws:
IndexOutOfBoundsException
- If the specified index is less than 0 or greater than or equal togetChildCount()
.NullPointerException
- Ifnode
is null.
-
replaceChild
Replace a child element at the specified index.If the specified node belongs to a different element or document, it will be removed from its previous document and element and adopted to this one.
If the specified
child
is not a child of this element, then nothing happens.Will trigger an
EventTypes.REMOVE_CHILD
on the child node that's being replaced and then aEventTypes.APPEND_CHILD
when the node is inserted into the child node list. Can additionally trigger aEventTypes.REMOVE_CHILD
event on the specified node's parent element, if there is one.- Parameters:
child
- Child to replacenode
- Node to replace with- Throws:
NullPointerException
- Ifnode
orchild
is null.
-
removeChild
Removes a specified child from this element.If the specified
node
is not a child of this element, thenfalse
will be returned.Will trigger a
EventTypes.REMOVE_CHILD
event. The removed node will be orphaned after removal.- Parameters:
node
- Node to remove- Returns:
true
, if the node was removed,false
if the node was not a child of this element.- Throws:
NullPointerException
- ifnode
isnull
- See Also:
-
removeChild
Removes a child element by its index.Will trigger a
EventTypes.REMOVE_CHILD
event. The removed node will be orphaned after removal.- Parameters:
childIndex
- Index of the child element to remove- Throws:
IndexOutOfBoundsException
- If the specifiedchildIndex
is invalid
-
removeMatchingChildren
Remove all child nodes that match the specifiedfilter
predicate.Each child that is removed triggers a
EventTypes.REMOVE_CHILD
event. And each removed node will be orphaned after removal.- Parameters:
filter
- Removal filter- Throws:
NullPointerException
- Iffilter
isnull
-
clearChildren
void clearChildren()Remove all child elements.Children are removed 1 by 1 using the
removeChild(Node)
method -
getChildren
-
hasChildren
boolean hasChildren()Tests if this element has children.- Returns:
true
, if the element has children,false
otherwise
-
hasChild
Tests if the specifiednode
is a direct child of this element- Parameters:
node
- Node to test- Returns:
true
, ifnode
is a direct child of this element,false
otherwise.
-
indexOf
Gets the index of a node that is the direct child of this element.- Parameters:
node
- Direct child node- Returns:
- The node's index, or
-1
, if the node is not a direct child of this element
-
getChildCount
int getChildCount()Gets the amount of children the element has- Returns:
- Child count
-
canHaveChildren
boolean canHaveChildren()Tests if this element is capable of having child nodes.This returns
true
in most cases. It returns false if the element's tag is<item>
.- Returns:
true
, if this element can support child noes,false
otherwise.
-
getChild
Gets the nth child of this element.- Parameters:
index
- Child index from 0 (inclusive) togetChildCount()
(exclusive)- Returns:
- Child node.
- Throws:
IndexOutOfBoundsException
- Ifindex
is less than 0 or greater/equal togetChildCount()
.
-
firstChild
Gets the first child of this element.- Returns:
- First child, or
null
, if this element has no children
-
lastChild
Gets the last child of this element.- Returns:
- Last child, or
null
, if this element has no children
-
appendElement
Appends an element and returns it.- Parameters:
tagName
- Element tag name- Returns:
- Created element
- Throws:
NullPointerException
- iftagName
isnull
- See Also:
-
appendText
-
getTextContent
String getTextContent()Get the element's text content.Returns the joined the text content of all descendants.
- Returns:
- Text content
-
setTextContent
Remove all child elements and replace it with the specifiedcontent
- Parameters:
content
- New text content
-
forEachDescendant
Applies a consumer to all descendant nodes of this element.The specified
consumer
is applied to elements in a depth-first traversal.- Parameters:
consumer
- Node consumer- Throws:
NullPointerException
- Ifconsumer
isnull
-
isDescendant
Test if a specifiednode
is a descendant of the element.- Parameters:
node
- Node to test- Returns:
true
, if thenode
is a descendant of the element,false
otherwise
-
matches
Test if the element matches a CSS selector.- Parameters:
selector
- Selector string- Returns:
true
, if the element matches a selector,false
otherwise
-