Skip to content
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

Support ARIA role property #1025

Open
wants to merge 2 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion src/freenet/client/filter/HTMLFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.nio.charset.MalformedInputException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
Expand Down Expand Up @@ -2111,14 +2112,19 @@ static class TagVerifier {
private final HashSet<String> uriAttrs;
private final HashSet<String> inlineURIAttrs;
final HashSet<String> booleanAttrs;
private final HashSet<String> allowedRole;

TagVerifier(String tag, String[] allowedAttrs) {
this(tag, allowedAttrs, null, null, null);
}

TagVerifier(String tag, String[] allowedAttrs, String[] uriAttrs, String[] inlineURIAttrs, String[] booleanAttrs) {
this.tag = tag;
this.allowedAttrs = new HashSet<String>();
this.allowedAttrs = new HashSet<String>(Arrays.asList("aria-braillelabel", "aria-brailleroledescription",
"aria-describedby", "aria-description", "aria-details", "aria-disabled", "aria-errormessage",
"aria-expanded", "aria-flowto","aria-hidden","aria-invalid","aria-label","aria-labelledby","aria-level",
"aria-multiline","aria-multiselectable","aria-orientation","aria-owns","aria-placeholder","aria-required",
"aria-roledescription","aria-selected","aria-setsize","aria-sort","aria-valuemax","aria-valuemin","aria-valuenow","aria-valuetext"));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These allow different types of content — boolean, free text, id, ... — so they need different validation.

this.parsedAttrs = new HashSet<String>();
if (allowedAttrs != null) {
for (String allowedAttr: allowedAttrs)
Expand All @@ -2140,6 +2146,25 @@ static class TagVerifier {
this.booleanAttrs.add(booleanAttrs[x]);
}
}
// https://w3c.github.io/aria/
this.allowedRole = new HashSet<String>(Arrays.asList("alert","alertdialog","application","article",
"banner","blockquote","button",
"caption","cell","checkbox","code","columnheader","combobox","command","comment","complementary","composite","contentinfo",
"definition","deletion","dialog","directory","document",
"emphasis",
"feed","figure","form",
"generic","grid","gridcell","group",
"heading",
"image","img","input","insertion",
"landmark","link","list","listbox","listitem","log",
"main","mark","marquee","math","menu","menubar","menuitem","menuitemcheckbox","menuitemradio","meter",
"navigation","none","note",
"option",
"paragraph","presentation","progressbar",
"radio","radiogroup","range","region","roletype","row","rowgroup","rowheader",
"scrollbar","search","searchbox","section","sectionhead","select","separator","slider","spinbutton","status","strong","structure","subscript","suggestion","superscript","switch",
"tab","table","tablist","tabpanel","term","textbox","time","timer","toolbar","tooltip","tree","treegrid","treeitem",
"widget","window"));
}

ParsedTag sanitize(ParsedTag t, HTMLParseContext pc) throws DataFilterException {
Expand Down Expand Up @@ -2286,6 +2311,13 @@ Map<String, Object> sanitizeHash(Map<String, Object> h,
if(logDEBUG) Logger.debug(this, "HTML Filter is putting attribute: "+x+" = "+o);
hn.put(x, o);
}
// ARIA properties
// role can be set on any element
if (x.equals("role") && o instanceof String) {
if(allowedRole.contains((String)o)) {
hn.put(x, o);
}
}
}
return hn;
}
Expand Down
7 changes: 7 additions & 0 deletions test/freenet/client/filter/ContentFilterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -431,4 +431,11 @@ public void testLowerCaseExtensions() {
}
}
}

private static final String ARIA_ROLE_TEST = "<span role=\"caption\" aria-label=\"caption\"/>";

@Test
public void testARIAProperties() throws Exception {
assertEquals(ARIA_ROLE_TEST, htmlFilter(ARIA_ROLE_TEST));
}
}
Loading