Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Some common regular expression definitions.
- // The 16 colors defined by the HTML Spec (also used by the CSS Spec)
- private static final Pattern COLOR_NAME = Pattern
- .compile("(?:aqua|black|blue|fuchsia|gray|grey|green|lime|maroon|navy|olive|purple"
- + "|red|silver|teal|white|yellow)");
- // HTML/CSS Spec allows 3 or 6 digit hex to specify color
- private static final Pattern COLOR_CODE = Pattern.compile("(?:#(?:[0-9a-fA-F]{3}(?:[0-9a-fA-F]{3})?))");
- private static final Pattern NUMBER_OR_PERCENT = Pattern.compile("[0-9]+%?");
- private static final Pattern PARAGRAPH = Pattern.compile("(?:[\\p{L}\\p{N},'\\.\\s\\-_\\(\\)]|&[0-9]{2};)*");
- private static final Pattern ONSITE_URL = Pattern
- .compile("(?:[\\p{L}\\p{N}\\\\\\.\\#@\\$%\\+&;\\-_~,\\?=/!]+|\\#(\\w)+)");
- private static final Pattern NUMBER = Pattern.compile("[+-]?(?:(?:[0-9]+(?:\\.[0-9]*)?)|\\.[0-9]+)");
- private static final Pattern NAME = Pattern.compile("[a-zA-Z0-9\\-_\\$]+");
- private static final Pattern ALIGN = Pattern.compile("(?i)center|left|right|justify|char");
- private static final Pattern VALIGN = Pattern.compile("(?i)baseline|bottom|middle|top");
- private static final Predicate<String> COLOR_NAME_OR_COLOR_CODE = new Predicate<String>()
- {
- @Override
- public boolean apply(String s)
- {
- return COLOR_NAME.matcher(s).matches() || COLOR_CODE.matcher(s).matches();
- }
- };
- private static final Pattern ONE_CHAR = Pattern.compile(".?", Pattern.DOTALL);
- /**
- * Allows table elements...
- */
- public static final PolicyFactory TABLES = new HtmlPolicyBuilder()
- .allowAttributes("border", "cellpadding", "cellspacing").matching(NUMBER).onElements("table")
- .allowAttributes("bgcolor").matching(COLOR_NAME_OR_COLOR_CODE).onElements("table")
- .allowAttributes("background").matching(ONSITE_URL).onElements("table").allowAttributes("align")
- .matching(ALIGN).onElements("table").allowAttributes("noresize").matching(Pattern.compile("(?i)noresize"))
- .onElements("table").allowAttributes("background").matching(ONSITE_URL).onElements("td", "th", "tr")
- .allowAttributes("bgcolor").matching(COLOR_NAME_OR_COLOR_CODE).onElements("td", "th")
- .allowAttributes("abbr").matching(PARAGRAPH).onElements("td", "th").allowAttributes("axis", "headers")
- .matching(NAME).onElements("td", "th").allowAttributes("scope")
- .matching(Pattern.compile("(?i)(?:row|col)(?:group)?")).onElements("td", "th").allowAttributes("nowrap")
- .onElements("td", "th").allowAttributes("height", "width").matching(NUMBER_OR_PERCENT)
- .onElements("table", "td", "th", "tr", "img").allowAttributes("align").matching(ALIGN)
- .onElements("thead", "tbody", "tfoot", "img", "td", "th", "tr", "colgroup", "col")
- .allowAttributes("valign").matching(VALIGN)
- .onElements("thead", "tbody", "tfoot", "td", "th", "tr", "colgroup", "col").allowAttributes("charoff")
- .matching(NUMBER_OR_PERCENT).onElements("td", "th", "tr", "colgroup", "col", "thead", "tbody", "tfoot")
- .allowAttributes("char").matching(ONE_CHAR)
- .onElements("td", "th", "tr", "colgroup", "col", "thead", "tbody", "tfoot")
- .allowAttributes("colspan", "rowspan").matching(NUMBER).onElements("td", "th")
- .allowAttributes("span", "width").matching(NUMBER_OR_PERCENT).onElements("colgroup", "col")
- .allowElements("table, th, tr, td, thead, tbody, tfoot").toFactory();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement