Utilities for HTML-specific logic.
| 12 | * |
| 13 | */ |
| 14 | public final class HTMLUtils |
| 15 | { |
| 16 | |
| 17 | private static final Set<String> KNOWN_XHTML_NAMESPACES = ImmutableSet.of(Namespaces.XHTML, |
| 18 | Namespaces.XML, Namespaces.OPS, Namespaces.SVG, Namespaces.MATHML, Namespaces.SSML, |
| 19 | Namespaces.XMLEVENTS, Namespaces.XLINK); |
| 20 | |
| 21 | private static final Set<String> CASE_INSENSITIVE_ATTRIBUTES = ImmutableSet.<String> builder() |
| 22 | .add("align").add("allowfullscreen").add("allowpaymentrequest").add("allowusermedia") |
| 23 | .add("async").add("autocapitalize").add("autocomplete").add("autofocus").add("autoplay") |
| 24 | .add("checked").add("contenteditable").add("controls").add("crossorigin").add("default") |
| 25 | .add("defer").add("dir").add("disabled").add("draggable").add("formnovalidate").add("hidden") |
| 26 | .add("http-equiv").add("ismap").add("itemscope").add("kind").add("loop").add("multiple") |
| 27 | .add("muted").add("nomodule").add("novalidate").add("open").add("playsinline").add("preload") |
| 28 | .add("readonly").add("required").add("reversed").add("scope").add("selected").add("shape") |
| 29 | .add("sizes").add("spellcheck").add("step").add("translate").add("type").add("typemustmatch") |
| 30 | .add("valign").add("value").add("wrap").build(); |
| 31 | |
| 32 | public static boolean isCustomElement(String namespace, String name) |
| 33 | { |
| 34 | return Namespaces.XHTML.equals(namespace) && Preconditions.checkNotNull(name).contains("-"); |
| 35 | } |
| 36 | |
| 37 | public static boolean isCustomNamespace(String namespace) |
| 38 | { |
| 39 | return !(namespace == null || namespace.trim().isEmpty() |
| 40 | || KNOWN_XHTML_NAMESPACES.contains(namespace.trim())); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Returns whether an attribute is defined as having a case-insensitive value |
| 45 | * in HTML. This is notably the case of boolean attributes and enumerated |
| 46 | * attributes. |
| 47 | * |
| 48 | * @param name |
| 49 | * the name of an attribute defined in the HTML specification |
| 50 | * @return <code>true</code> iff the attribute value is case-insensitive |
| 51 | */ |
| 52 | public static boolean isCaseInsensitiveAttribute(String namespace, String name) |
| 53 | { |
| 54 | return namespace.isEmpty() && CASE_INSENSITIVE_ATTRIBUTES.contains(name); |
| 55 | } |
| 56 | |
| 57 | public static boolean isDataAttribute(String namespace, String name) |
| 58 | { |
| 59 | return namespace.isEmpty() && name.startsWith("data-"); |
| 60 | } |
| 61 | |
| 62 | public static boolean isITSAttribute(String namespace, String name) |
| 63 | { |
| 64 | return namespace.isEmpty() && name.startsWith("its-"); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Tells if a string is a valid <a href= |
| 69 | * "https://html.spec.whatwg.org/multipage/dom.html#custom-data-attribute"> |
| 70 | * custom data attribute</a>, as |
| 71 | * defined in HTML. |