Package com.pd4ml.dom
Class Element
- java.lang.Object
-
- com.pd4ml.dom.Node
-
- com.pd4ml.dom.Element
-
public class Element extends Node
An HTML element. Tag and attribute names are lower case, as the HTML parser makes them.An inline
<svg>and a custom tag (<math>,<qrcode>and those added withPD4ML.addCustomTagHandler(java.lang.String, com.pd4ml.CustomTag)) are drawn from their source markup, not from child nodes: they have no child nodes here, and their content is read and changed as HTML withgetInnerHTML()/setInnerHTML(String)orsetOuterHTML(String). Their attributes can be read; to change how they are drawn, change their markup.- Since:
- v4.2.0
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddClass(java.lang.String name)Elementclosest(java.lang.String selectors)This element or its nearest ancestor that matches the CSS selector (list), or null.java.lang.StringgetAttribute(java.lang.String name)The attribute value, or null if the element does not have the attribute.java.util.Map<java.lang.String,java.lang.String>getAttributes()The attributes in document order (a read-only snapshot).java.util.List<Element>getChildren()The child elements (a snapshot).java.util.Set<java.lang.String>getClassList()The class names (a read-only snapshot).java.lang.StringgetClassName()The class attribute, or an empty string.java.util.List<Element>getElementsByClassName(java.lang.String names)The descendant elements that have all the (space separated) class names, in document order.java.util.List<Element>getElementsByTagName(java.lang.String name)The descendant elements with the tag name, in document order (*: all).ElementgetFirstElementChild()java.lang.StringgetId()The id attribute, or an empty string.java.lang.StringgetInnerHTML()The HTML of the element's content.ElementgetLastElementChild()ElementgetNextElementSibling()java.lang.StringgetOuterHTML()The HTML of the element itself.ElementgetPreviousElementSibling()java.lang.StringgetTagName()The tag name, lower case (e.g.booleanhasAttribute(java.lang.String name)booleanhasClass(java.lang.String name)booleanmatches(java.lang.String selectors)True if this element matches the CSS selector (list).ElementquerySelector(java.lang.String selectors)The first descendant element that matches the CSS selector (list), or null.java.util.List<Element>querySelectorAll(java.lang.String selectors)The descendant elements that match the CSS selector (list), in document order.voidremoveAttribute(java.lang.String name)voidremoveClass(java.lang.String name)voidsetAttribute(java.lang.String name, java.lang.String value)Sets an attribute (the name is made lower case).voidsetClassName(java.lang.String className)voidsetId(java.lang.String id)voidsetInnerHTML(java.lang.String html)Replaces the element's content with the HTML, parsed as the browser'sinnerHTMLparses it: as the content of an element of this tag (table rows in a<tr>, options in a<select>).voidsetOuterHTML(java.lang.String html)Replaces the element (in its parent) with the nodes the HTML parses into, as the content of the parent.voidsetTextContent(java.lang.String text)Replaces the content of this node with the text: an element's children are replaced by one text node (none for an empty text); a text or comment gets the text as its data.booleantoggleClass(java.lang.String name)Adds the class if the element does not have it, removes it otherwise.-
Methods inherited from class com.pd4ml.dom.Node
appendChild, cloneNode, contains, equals, getChildNodes, getFirstChild, getLastChild, getNextSibling, getNodeName, getOwnerDocument, getParentElement, getParentNode, getPreviousSibling, getTextContent, hasChildNodes, hashCode, insertBefore, remove, removeChild, replaceChild, toHtml, toString
-
-
-
-
Method Detail
-
getTagName
public java.lang.String getTagName()
The tag name, lower case (e.g.div,pd4ml:page.break).
-
getAttribute
public java.lang.String getAttribute(java.lang.String name)
The attribute value, or null if the element does not have the attribute.
-
hasAttribute
public boolean hasAttribute(java.lang.String name)
-
setAttribute
public void setAttribute(java.lang.String name, java.lang.String value)Sets an attribute (the name is made lower case). A null value is an empty one.
-
removeAttribute
public void removeAttribute(java.lang.String name)
-
getAttributes
public java.util.Map<java.lang.String,java.lang.String> getAttributes()
The attributes in document order (a read-only snapshot).
-
getId
public java.lang.String getId()
The id attribute, or an empty string.
-
setId
public void setId(java.lang.String id)
-
getClassName
public java.lang.String getClassName()
The class attribute, or an empty string.
-
setClassName
public void setClassName(java.lang.String className)
-
getClassList
public java.util.Set<java.lang.String> getClassList()
The class names (a read-only snapshot).
-
hasClass
public boolean hasClass(java.lang.String name)
-
addClass
public void addClass(java.lang.String name)
-
removeClass
public void removeClass(java.lang.String name)
-
toggleClass
public boolean toggleClass(java.lang.String name)
Adds the class if the element does not have it, removes it otherwise. @return true if added
-
getChildren
public java.util.List<Element> getChildren()
The child elements (a snapshot).
-
getFirstElementChild
public Element getFirstElementChild()
-
getLastElementChild
public Element getLastElementChild()
-
getNextElementSibling
public Element getNextElementSibling()
-
getPreviousElementSibling
public Element getPreviousElementSibling()
-
getElementsByTagName
public java.util.List<Element> getElementsByTagName(java.lang.String name)
The descendant elements with the tag name, in document order (*: all).
-
getElementsByClassName
public java.util.List<Element> getElementsByClassName(java.lang.String names)
The descendant elements that have all the (space separated) class names, in document order.
-
querySelector
public Element querySelector(java.lang.String selectors)
The first descendant element that matches the CSS selector (list), or null.- Throws:
java.lang.IllegalArgumentException- if the selector is not valid
-
querySelectorAll
public java.util.List<Element> querySelectorAll(java.lang.String selectors)
The descendant elements that match the CSS selector (list), in document order.- Throws:
java.lang.IllegalArgumentException- if the selector is not valid
-
matches
public boolean matches(java.lang.String selectors)
True if this element matches the CSS selector (list).- Throws:
java.lang.IllegalArgumentException- if the selector is not valid
-
closest
public Element closest(java.lang.String selectors)
This element or its nearest ancestor that matches the CSS selector (list), or null.- Throws:
java.lang.IllegalArgumentException- if the selector is not valid
-
getInnerHTML
public java.lang.String getInnerHTML()
The HTML of the element's content.
-
setInnerHTML
public void setInnerHTML(java.lang.String html)
Replaces the element's content with the HTML, parsed as the browser'sinnerHTMLparses it: as the content of an element of this tag (table rows in a<tr>, options in a<select>). The text content of<script>,<style>,<textarea>and<title>is set as is.
-
getOuterHTML
public java.lang.String getOuterHTML()
The HTML of the element itself.
-
setOuterHTML
public void setOuterHTML(java.lang.String html)
Replaces the element (in its parent) with the nodes the HTML parses into, as the content of the parent. This element is then detached.- Throws:
java.lang.IllegalStateException- if the element has no parent element
-
setTextContent
public void setTextContent(java.lang.String text)
Description copied from class:NodeReplaces the content of this node with the text: an element's children are replaced by one text node (none for an empty text); a text or comment gets the text as its data.- Overrides:
setTextContentin classNode
-
-