Permalink
Browse files

[#93] Add Match.matchAttr(String, String)

  • Loading branch information...
lukaseder committed Jun 25, 2014
1 parent 5f5d54f commit e64a439e3143f0cc4518f124cb44e883d24c367b
@@ -0,0 +1,2 @@
/bin
/.idea
@@ -1,2 +1,4 @@
/target
/release
/.idea
/*.iml
@@ -1072,6 +1072,21 @@ public final Impl matchTag(String regex, boolean keepMatches) {
}
}

@Override
public final Match matchAttr(String name, String valueRegex) {
return matchAttr(name, valueRegex, true);
}

@Override
public final Match matchAttr(String name, String valueRegex, boolean keepMatches) {
if (keepMatches) {
return filter(JOOX.matchAttr(name, valueRegex));
}
else {
return not(JOOX.matchAttr(name, valueRegex));
}
}

@Override
public final Impl leaf() {
return filter(JOOX.leaf());
@@ -595,6 +595,33 @@ public boolean filter(Context context) {
}
}

/**
* A filter that returns all elements whose text content matches a given
* regex
*
* @see Pattern#matches(String, CharSequence)
*/
public static FastFilter matchAttr(final String name, final String valueRegex) {
if (name == null || name.equals("") || valueRegex == null || valueRegex.equals("")) {
return none();
}
else {
return new FastFilter() {
private final Pattern pattern = Pattern.compile(valueRegex);

@Override
public boolean filter(Context context) {
String value = $(context).attr(name);

if (value == null)
return false;

return pattern.matcher(value).matches();
}
};
}
}

/**
* A filter that returns all elements whose tag name matches a given regex
* <p>
@@ -1451,6 +1451,26 @@
*/
Match matchText(String regex, boolean keepMatches);

/**
* Reduce the set of matched elements by filtering out those whose attribute
* content doesn't match a given regex
* <p>
* This is the same as calling <code>matchAttr(name, valueRegex, true)</code>
*
* @see JOOX#matchAttr(String, String)
*/
Match matchAttr(String name, String valueRegex);

/**
* Reduce the set of matched elements by filtering out those whose attribute
* content doesn't match a given regex <code>(keepMatches = true)</code>, or
* those whose text content matches a given regex
* <code>(keepMatches = false)</code>
*
* @see JOOX#matchAttr(String, String)
*/
Match matchAttr(String name, String valueRegex, boolean keepMatches);

/**
* Reduce the set of matched elements by filtering out those whose tag name
* doesn't match a given regex
@@ -1799,6 +1799,11 @@ public void testRegex() throws Exception {
assertEquals(2, $.find("name, actor").matchText(".*in.*", true).size());
assertEquals($.find("name, actor").size() - 2,
$.find("name, actor").matchText(".*in.*", false).size());

assertEquals(2, $.find("library").matchAttr("name", ".*i.*").size());
assertEquals(1, $.find("library").matchAttr("name", ".*i.*", false).size());
assertEquals(asList("Roesslitor", "Orell Fuessli"), $.find("library").matchAttr("name", ".*i.*").attrs("name"));
assertEquals(asList("Amazon"), $.find("library").matchAttr("name", ".*i.*", false).attrs("name"));
}

@Test

0 comments on commit e64a439

Please sign in to comment.