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"));
}
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);
}
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);
}
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;
}
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"));
}