Search in sources :

Example 1 with Attribute

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);
}
Also used : Attribute(org.jsoup.nodes.Attribute) Element(org.jsoup.nodes.Element) Attributes(org.jsoup.nodes.Attributes)

Example 2 with Attribute

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++;
    }
}
Also used : Attribute(org.jsoup.nodes.Attribute) Node(org.jsoup.nodes.Node)

Example 3 with Attribute

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());
}
Also used : BooleanAttribute(org.jsoup.nodes.BooleanAttribute) Attribute(org.jsoup.nodes.Attribute) BooleanAttribute(org.jsoup.nodes.BooleanAttribute) Element(org.jsoup.nodes.Element) Test(org.junit.Test)

Aggregations

Attribute (org.jsoup.nodes.Attribute)3 Element (org.jsoup.nodes.Element)2 Attributes (org.jsoup.nodes.Attributes)1 BooleanAttribute (org.jsoup.nodes.BooleanAttribute)1 Node (org.jsoup.nodes.Node)1 Test (org.junit.Test)1