Search in sources :

Example 1 with Attributes

use of org.jsoup.nodes.Attributes in project jsoup by jhy.

the class AttributeParseTest method parsesRoughAttributeString.

@Test
public void parsesRoughAttributeString() {
    String html = "<a id=\"123\" class=\"baz = 'bar'\" style = 'border: 2px'qux zim foo = 12 mux=18 />";
    // should be: <id=123>, <class=baz = 'bar'>, <qux=>, <zim=>, <foo=12>, <mux.=18>
    Element el = Jsoup.parse(html).getElementsByTag("a").get(0);
    Attributes attr = el.attributes();
    assertEquals(7, attr.size());
    assertEquals("123", attr.get("id"));
    assertEquals("baz = 'bar'", attr.get("class"));
    assertEquals("border: 2px", attr.get("style"));
    assertEquals("", attr.get("qux"));
    assertEquals("", attr.get("zim"));
    assertEquals("12", attr.get("foo"));
    assertEquals("18", attr.get("mux"));
}
Also used : Element(org.jsoup.nodes.Element) Attributes(org.jsoup.nodes.Attributes) Test(org.junit.Test)

Example 2 with Attributes

use of org.jsoup.nodes.Attributes 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 3 with Attributes

use of org.jsoup.nodes.Attributes in project jsoup by jhy.

the class Whitelist method isSafeAttribute.

/**
     * Test if the supplied attribute is allowed by this whitelist for this tag
     * @param tagName tag to consider allowing the attribute in
     * @param el element under test, to confirm protocol
     * @param attr attribute under test
     * @return true if allowed
     */
protected boolean isSafeAttribute(String tagName, Element el, Attribute attr) {
    TagName tag = TagName.valueOf(tagName);
    AttributeKey key = AttributeKey.valueOf(attr.getKey());
    Set<AttributeKey> okSet = attributes.get(tag);
    if (okSet != null && okSet.contains(key)) {
        if (protocols.containsKey(tag)) {
            Map<AttributeKey, Set<Protocol>> attrProts = protocols.get(tag);
            // ok if not defined protocol; otherwise test
            return !attrProts.containsKey(key) || testValidProtocol(el, attr, attrProts.get(key));
        } else {
            // attribute found, no protocols defined, so OK
            return true;
        }
    }
    // might be an enforced attribute?
    Map<AttributeKey, AttributeValue> enforcedSet = enforcedAttributes.get(tag);
    if (enforcedSet != null) {
        Attributes expect = getEnforcedAttributes(tagName);
        String attrKey = attr.getKey();
        if (expect.hasKeyIgnoreCase(attrKey)) {
            return expect.getIgnoreCase(attrKey).equals(attr.getValue());
        }
    }
    // no attributes defined for tag, try :all tag
    return !tagName.equals(":all") && isSafeAttribute(":all", el, attr);
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) Attributes(org.jsoup.nodes.Attributes)

Example 4 with Attributes

use of org.jsoup.nodes.Attributes in project jsoup by jhy.

the class Whitelist method getEnforcedAttributes.

Attributes getEnforcedAttributes(String tagName) {
    Attributes attrs = new Attributes();
    TagName tag = TagName.valueOf(tagName);
    if (enforcedAttributes.containsKey(tag)) {
        Map<AttributeKey, AttributeValue> keyVals = enforcedAttributes.get(tag);
        for (Map.Entry<AttributeKey, AttributeValue> entry : keyVals.entrySet()) {
            attrs.put(entry.getKey().toString(), entry.getValue().toString());
        }
    }
    return attrs;
}
Also used : Attributes(org.jsoup.nodes.Attributes) Map(java.util.Map) HashMap(java.util.HashMap)

Example 5 with Attributes

use of org.jsoup.nodes.Attributes in project jsoup by jhy.

the class AttributeParseTest method canStartWithEq.

@Test
public void canStartWithEq() {
    String html = "<a =empty />";
    Element el = Jsoup.parse(html).getElementsByTag("a").get(0);
    Attributes attr = el.attributes();
    assertEquals(1, attr.size());
    assertTrue(attr.hasKey("=empty"));
    assertEquals("", attr.get("=empty"));
}
Also used : Element(org.jsoup.nodes.Element) Attributes(org.jsoup.nodes.Attributes) Test(org.junit.Test)

Aggregations

Attributes (org.jsoup.nodes.Attributes)9 Element (org.jsoup.nodes.Element)6 Attribute (org.jsoup.nodes.Attribute)3 Test (org.junit.Test)3 Element (com.vaadin.flow.dom.Element)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Set (java.util.Set)1