AbstractHTMLFilter implements HTMLFilter, HTMLConstants { protected boolean hasAttribute(List<Element> elements, String attributeName, boolean checkValue) { boolean hasAttribute = true; if (!checkValue) { for (Element e : elements) { hasAttribute = e.hasAttribute(attributeName) && hasAttribute; } } else { String attributeValue = null; for (Element e : elements) { attributeValue = attributeValue == null ? e.getAttribute(attributeName) : attributeValue; hasAttribute = e.getAttribute(attributeName).equals(attributeValue) && hasAttribute; } } return hasAttribute; }    }
@Test void hasAttribute() { Document document = this.cleaner.clean(new StringReader("<html><head><body><p>test1</p><p class=\"myClass\">test2</p></body>" + "</html>")); List<Element> paragraphs = htmlFilter.filterDescendants(document.getDocumentElement(), new String[]{ "p" }); assertEquals(2, paragraphs.size()); assertFalse(htmlFilter.hasAttribute(paragraphs, "class", false)); document = this.cleaner.clean(new StringReader("<html><head><body><p class=\"anotherClass\">test1</p>" + "<p class=\"myClass\">test2</p></body></html>")); paragraphs = htmlFilter.filterDescendants(document.getDocumentElement(), new String[]{ "p" }); assertEquals(2, paragraphs.size()); assertTrue(htmlFilter.hasAttribute(paragraphs, "class", false)); assertFalse(htmlFilter.hasAttribute(paragraphs, "class", true)); document = this.cleaner.clean(new StringReader("<html><head><body><p class=\"myClass\">test1</p>" + "<p class=\"myClass\">test2</p></body></html>")); paragraphs = htmlFilter.filterDescendants(document.getDocumentElement(), new String[]{ "p" }); assertEquals(2, paragraphs.size()); assertTrue(htmlFilter.hasAttribute(paragraphs, "class", true)); }