Class DomUtil


  • public class DomUtil
    extends Object
    • Method Detail

      • formatHtml

        public static String formatHtml​(Node node)
                                 throws IOException
        Pretty prints the given HTML node. Puts attributes on new lines.
        Parameters:
        node - the node to format to html
        Returns:
        The formatted HTML.
        Throws:
        IOException - Should not happen as ByteArrayOutputStream does not throw.
      • setText

        public static void setText​(Document doc,
                                   Element element,
                                   String text)
        Adds the given text to the node as a child, escaping according to HTML rules.
        Parameters:
        doc - the document for which the node should be added
        element - the element under which the text node should be appended
        text - the text of the textnode
      • setJs

        public static void setJs​(Document doc,
                                 Element element,
                                 String text)
        Similar to setText(Document, Element, String), but assumes the text to be set is JavaScript. The text content of a script tag is CDATA, the string </script (which can only occur in strings or regexps) is escaped by replacing it with \x3c/script.
        Parameters:
        doc - the document for which the node should be added
        element - the element under which the node should be appended
        text - the javascript to set to the node
      • setAttribute

        public static void setAttribute​(Element element,
                                        String name,
                                        String value)
        Assumes the implementation outputs the attribute as a double-quoted attribute. Only & and " need to be escaped for double-quoted attributes. See https://html.spec.whatwg.org/multipage/syntax.html#syntax-attributes Attribute values are a mixture of text and character references, except with the additional restriction that the text cannot contain an ambiguous ampersand. The attribute value, which, in addition to the requirements given above for attribute values, [begins with a single U+0022 QUOTATION MARK character (") and] must not contain any literal U+0022 QUOTATION MARK characters ("), and finally followed by a second single U+0022. QUOTATION MARK character (").
        Parameters:
        element - Element on which to set an attribute.
        name - Attribute name to set.
        value - Attribute value to set.