use of org.jsoup.nodes.Attribute in project jsoup by jhy.
the class Cleaner method createSafeElement.
private ElementMeta createSafeElement(Element sourceEl) {
String sourceTag = sourceEl.tagName();
Attributes destAttrs = new Attributes();
Element dest = new Element(Tag.valueOf(sourceTag), sourceEl.baseUri(), destAttrs);
int numDiscarded = 0;
Attributes sourceAttrs = sourceEl.attributes();
for (Attribute sourceAttr : sourceAttrs) {
if (whitelist.isSafeAttribute(sourceTag, sourceEl, sourceAttr))
destAttrs.put(sourceAttr);
else
numDiscarded++;
}
Attributes enforcedAttrs = whitelist.getEnforcedAttributes(sourceTag);
destAttrs.addAll(enforcedAttrs);
return new ElementMeta(dest, numDiscarded);
}
use of org.jsoup.nodes.Attribute in project Asqatasun by Asqatasun.
the class HTMLJsoupCleanerImpl method removeMalformedAttributes.
/**
* Remove the comments of the page
*
* @param node
*/
private void removeMalformedAttributes(Node node) {
// as we are removing child nodes while iterating, we cannot use a normal foreach over children,
// or will get a concurrent list modification error.
int i = 0;
while (i < node.childNodes().size()) {
Node child = node.childNode(i);
for (Attribute attr : child.attributes()) {
if (attr.getKey().startsWith("\"") && attr.getKey().endsWith("\"")) {
child.removeAttr(attr.getKey());
}
}
removeMalformedAttributes(child);
i++;
}
}
use of org.jsoup.nodes.Attribute in project jsoup by jhy.
the class AttributeParseTest method parsesBooleanAttributes.
@Test
public void parsesBooleanAttributes() {
String html = "<a normal=\"123\" boolean empty=\"\"></a>";
Element el = Jsoup.parse(html).select("a").first();
assertEquals("123", el.attr("normal"));
assertEquals("", el.attr("boolean"));
assertEquals("", el.attr("empty"));
List<Attribute> attributes = el.attributes().asList();
assertEquals("There should be 3 attribute present", 3, attributes.size());
// Assuming the list order always follows the parsed html
assertFalse("'normal' attribute should not be boolean", attributes.get(0) instanceof BooleanAttribute);
assertTrue("'boolean' attribute should be boolean", attributes.get(1) instanceof BooleanAttribute);
assertFalse("'empty' attribute should not be boolean", attributes.get(2) instanceof BooleanAttribute);
assertEquals(html, el.outerHtml());
}
Aggregations