-
Notifications
You must be signed in to change notification settings - Fork 214
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New methods for excluding elements with specific missing or empty attributes #45
base: master
Are you sure you want to change the base?
Changes from 3 commits
73b68bc
e75d980
ea02e71
2537933
e6dd2ea
f3f56d4
fd6b2dd
eb6ef02
25c3d64
ffe5cfa
c7db2d4
ca40697
af0ca83
acaf3f2
33d319f
020d5d0
ad287c3
be33ec6
374ea2f
7d76ba9
e2b29e8
14f84fd
62a0715
06b299c
5372c74
c2c74fc
e35ef4f
3756979
0372f4f
ccb4c18
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -288,7 +288,28 @@ public HtmlPolicyBuilder allowWithoutAttributes(String... elementNames) { | |
} | ||
return this; | ||
} | ||
|
||
|
||
/** | ||
* Disallows the given element from appearing without the given attribute. | ||
*/ | ||
public HtmlPolicyBuilder disallowWithoutAttribute(String elementName, final String attributeName) { | ||
invalidateCompiledState(); | ||
elementName = HtmlLexer.canonicalName(elementName); | ||
ElementPolicy policy = new ElementPolicy() { | ||
public @Nullable String apply(String elementName, List<String> attrs) { | ||
for(int i=0; i<attrs.size(); i+=2) { | ||
if(attrs.get(i).equals(attributeName)){ | ||
return elementName; | ||
} | ||
} | ||
return null; | ||
} | ||
}; | ||
ElementPolicy newPolicy = ElementPolicy.Util.join(elPolicies.get(elementName), policy); | ||
elPolicies.put(elementName, newPolicy); | ||
return this; | ||
} | ||
|
||
/** | ||
* Disallows the given elements from appearing without attributes. | ||
* | ||
|
@@ -714,6 +735,32 @@ public AttributeBuilder matching( | |
}); | ||
} | ||
|
||
/** | ||
* Restrict the values allowed by later {@code allow*} calls to those | ||
* NOT matching the pattern. This is a convenience method, as inverting | ||
* certain patterns can otherwise be quite complex. | ||
*/ | ||
public AttributeBuilder notMatching(final Pattern pattern) { | ||
return matching(new AttributePolicy() { | ||
public @Nullable String apply(String elementName, String attributeName, String value) { | ||
return !pattern.matcher(value).matches() ? value : null; | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* Restrict the values allowed by later {@code allow*} calls to those | ||
* that are not only whitespace or an empty string. Can be combined | ||
* with calls to {@code matching} or {@code notMatching}. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use Please fix indentation. Maybe add a caveat along the lines of
|
||
*/ | ||
public AttributeBuilder notEmptyOrWhitespace() { | ||
return matching(new AttributePolicy() { | ||
public @Nullable String apply(String elementName, String attributeName, String value) { | ||
return value.trim().length() > 0 ? value : null; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe that the definition of whitespace in HTML is not the same as the whitespace matched by http://www.w3.org/html/wg/drafts/html/master/single-page.html#space-character says
while javadoc says
|
||
} | ||
}); | ||
} | ||
|
||
/** | ||
* Allows the given attributes on any elements but filters the | ||
* attributes' values based on previous calls to {@code matching(...)}. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right now, we can say
and if I understand your goal, that is problematic because it allows but does not require
src="..."
on<img ...>
.I'd prefer
which allows mixing
required()
into the existing flow by which elements are associated with attributes instead of creating a new API.