Class HtmlTool
The methods utilise CSS selectors to refer to specific elements for manipulation.
- Since:
- 1.0
- Author:
- Bertrand Martin (originally inspired by Andrius Velykis)
- See Also:
-
Field Summary
Fields inherited from class org.apache.velocity.tools.generic.SafeConfig
LOCK_CONFIG_KEY, LOGGER_NAME_KEY, SAFE_MODE_KEY, USE_CLASS_LOGGER_KEY
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionorg.jsoup.nodes.Element
Adds given class to the elements in HTML.org.jsoup.nodes.Element
Adds given class names to the elements in HTML.org.jsoup.nodes.Element
Adds given class names to the elements in HTML.org.jsoup.nodes.Element
Append HTML elements to specified elements in the given HTML.Utility method to concatenate a String to a list of Strings.org.jsoup.nodes.Element
ensureHeadingIds
(org.jsoup.nodes.Element body) Transforms the given HTML content by adding IDs to all heading elements (h1-6
) that do not have one.org.jsoup.nodes.Element
fixIds
(org.jsoup.nodes.Element body) Transforms the given HTML content to replace IDs that have symbols not allowed in CSS selectors, e.g.org.jsoup.nodes.Element
fixProtocolRelativeUrls
(org.jsoup.nodes.Element body) Replace all<a href="//...">
links with protocol-relative URLs with proper HTTPS URLsorg.jsoup.nodes.Element
fixTableHeads
(org.jsoup.nodes.Element body) Fixes table heads: wraps rows with<th>
(table heading) elements into<thead>
element if they are currently in<tbody>
.Retrieves attribute value on elements in HTML.org.jsoup.nodes.Element
headingAnchorToId
(org.jsoup.nodes.Element body) Transforms the given HTML content by moving anchor (<a name="myheading">
) names to IDs for heading elements.org.jsoup.nodes.Element
parseContent
(String content) Parses HTML fragmentorg.jsoup.nodes.Element
Prepend HTML elements to specified elements in the given HTML.org.jsoup.nodes.Element
Removes elements from HTML.org.jsoup.nodes.Element
Replace selected elements with specified elements in the given HTML.org.jsoup.nodes.Element
Sets attribute to the given value on elements in HTML.static String
Creates a slug (latin text with no whitespace or other symbols) for a longer text (i.e.Retrieves text content of the selected elements in HTML.org.jsoup.nodes.Element
Wraps elements in HTML with the given HTML.Methods inherited from class org.apache.velocity.tools.generic.SafeConfig
configure, isConfigLocked, isSafeMode
-
Constructor Details
-
HtmlTool
public HtmlTool()Create a new instance
-
-
Method Details
-
setAttr
public org.jsoup.nodes.Element setAttr(org.jsoup.nodes.Element body, String selector, String attributeKey, String value) Sets attribute to the given value on elements in HTML.- Parameters:
body
- HTML content to set attributes onselector
- CSS selector for elements to modifyattributeKey
- Attribute namevalue
- Attribute value- Returns:
- HTML content with modified elements. If no elements are found, the original content is returned.
- Since:
- 1.0
-
parseContent
Parses HTML fragment- Parameters:
content
- HTML fragment to parse- Returns:
- Element of the specified HTML fragment
-
getAttr
Retrieves attribute value on elements in HTML. Will return all attribute values for the selector, since there can be more than one element.- Parameters:
body
- HTML content to read attributes fromselector
- CSS selector for elements to findattributeKey
- Attribute name- Returns:
- Attribute values for all matching elements. If no elements are found, empty list is returned.
- Since:
- 1.0
-
addClass
public org.jsoup.nodes.Element addClass(org.jsoup.nodes.Element body, String selector, List<String> classNames, int amount) Adds given class names to the elements in HTML.- Parameters:
body
- HTML content to modifyselector
- CSS selector for elements to add classes toclassNames
- Names of classes to add to the selected elementsamount
- Maximum number of elements to modify- Returns:
- HTML content with modified elements. If no elements are found, the original content is returned.
- Since:
- 1.0
-
addClass
public org.jsoup.nodes.Element addClass(org.jsoup.nodes.Element body, String selector, List<String> classNames) Adds given class names to the elements in HTML.- Parameters:
body
- HTML content to modifyselector
- CSS selector for elements to add classes toclassNames
- Names of classes to add to the selected elements- Returns:
- HTML content with modified elements. If no elements are found, the original content is returned.
- Since:
- 1.0
-
addClass
public org.jsoup.nodes.Element addClass(org.jsoup.nodes.Element body, String selector, String className) Adds given class to the elements in HTML.- Parameters:
body
- HTML content to modifyselector
- CSS selector for elements to add the class toclassName
- Name of class to add to the selected elements- Returns:
- HTML content with modified elements. If no elements are found, the original content is returned.
- Since:
- 1.0
-
wrap
public org.jsoup.nodes.Element wrap(org.jsoup.nodes.Element body, String selector, String wrapHtml, int amount) Wraps elements in HTML with the given HTML.- Parameters:
body
- HTML content to modifyselector
- CSS selector for elements to wrapwrapHtml
- HTML to use for wrapping the selected elementsamount
- Maximum number of elements to modify- Returns:
- HTML content with modified elements. If no elements are found, the original content is returned.
- Since:
- 1.0
-
append
public org.jsoup.nodes.Element append(org.jsoup.nodes.Element body, String selector, String appendHtml, int amount) Append HTML elements to specified elements in the given HTML.- Parameters:
body
- HTML content to modifyselector
- CSS selector for elements that will get the appendiceappendHtml
- HTML to append to the selected elementsamount
- Maximum number of elements to modify- Returns:
- HTML content with modified elements. If no elements are found, the original content is returned.
-
prepend
public org.jsoup.nodes.Element prepend(org.jsoup.nodes.Element body, String selector, String prependHtml, int amount) Prepend HTML elements to specified elements in the given HTML.- Parameters:
body
- HTML content to modifyselector
- CSS selector for elements that will get the "pre-pendice"prependHtml
- HTML to prepend to the selected elementsamount
- Maximum number of elements to modify- Returns:
- HTML content with modified elements. If no elements are found, the original content is returned.
-
remove
Removes elements from HTML.- Parameters:
body
- HTML content to modifyselector
- CSS selector for elements to remove- Returns:
- HTML content with removed elements. If no elements are found, the original content is returned.
- Since:
- 1.0
-
replace
public org.jsoup.nodes.Element replace(org.jsoup.nodes.Element body, String selector, String replaceHtml, int amount) Replace selected elements with specified elements in the given HTML.- Parameters:
body
- HTML content to modifyselector
- CSS selector for elements that will get the appendicereplaceHtml
- HTML to append to the selected elementsamount
- Maximum number of elements to modify (-1 for unlimited)- Returns:
- HTML content with modified elements. If no elements are found, the original content is returned.
-
text
Retrieves text content of the selected elements in HTML. Renders the element's text as it would be displayed on the web page (including its children).- Parameters:
body
- HTML content with the elementsselector
- CSS selector for elements to extract contents- Returns:
- A list of element texts as rendered to display. Empty list if no elements are found.
- Since:
- 1.0
-
headingAnchorToId
public org.jsoup.nodes.Element headingAnchorToId(org.jsoup.nodes.Element body) Transforms the given HTML content by moving anchor (<a name="myheading">
) names to IDs for heading elements.The anchors are used to indicate positions within a HTML page. In HTML5, however, the
name
attribute is no longer supported on<a>
) tag. The positions within pages are indicated usingid
attribute instead, e.g.<h1 id="myheading">
.The method finds anchors inside, immediately before or after the heading tags and uses their name as heading
id
instead. The anchors themselves are removed.- Parameters:
body
- HTML content to modify- Returns:
- HTML content with modified elements. Anchor names are used for adjacent headings, and anchor tags are removed. If no elements are found, the original content is returned.
- Since:
- 1.0
-
concat
Utility method to concatenate a String to a list of Strings. The text can be either appended or prepended.- Parameters:
elements
- list of elements to append/prepend the text totext
- the given text to append/prependappend
- iftrue
, text will be appended to the elements. Iffalse
, it will be prepended- Returns:
- list of elements with the text appended/prepended
- Since:
- 1.0
-
ensureHeadingIds
public org.jsoup.nodes.Element ensureHeadingIds(org.jsoup.nodes.Element body) Transforms the given HTML content by adding IDs to all heading elements (h1-6
) that do not have one.IDs on heading elements are used to indicate positions within a HTML page in HTML5. If a heading tag without an
id
is found, its "slug" is generated automatically based on the heading contents and used as the ID.- Parameters:
body
- HTML content to modify- Returns:
- HTML content with all heading elements having
id
attributes. If all headings were with IDs already, the original content is returned. - Since:
- 1.0
-
fixIds
public org.jsoup.nodes.Element fixIds(org.jsoup.nodes.Element body) Transforms the given HTML content to replace IDs that have symbols not allowed in CSS selectors, e.g. ":", ".", etc. The symbols are removed.Naturally, the references to these IDs (in
<a href="#my_id">
) are also modified.- Parameters:
body
- HTML content to modify- Returns:
- HTML content fixed IDs.
- Since:
- 1.0
-
fixTableHeads
public org.jsoup.nodes.Element fixTableHeads(org.jsoup.nodes.Element body) Fixes table heads: wraps rows with<th>
(table heading) elements into<thead>
element if they are currently in<tbody>
.- Parameters:
body
- HTML content to modify- Returns:
- HTML content with all table heads fixed. If all heads were correct, the original content is returned.
- Since:
- 1.0
-
slug
Creates a slug (latin text with no whitespace or other symbols) for a longer text (i.e. to use in URLs). Uses "-" as a word separator.- Parameters:
input
- The string (free) to be transformed into a valid element ID- Returns:
- the proper slug
-
fixProtocolRelativeUrls
public org.jsoup.nodes.Element fixProtocolRelativeUrls(org.jsoup.nodes.Element body) Replace all<a href="//...">
links with protocol-relative URLs with proper HTTPS URLs- Parameters:
body
- HTML content to modify- Returns:
- HTML content fixed linkss
-