'Sanitize a token either by HTML-encoding or dropping.
Unlike HTMLSanitizerMixin.sanitize_token, allowed_attributes can be
a dict of {\'tag\': [\'attribute\', \'pairs\'], \'tag\': callable}.
Here callable is a function with two arguments of attribute name
and value. It should return true of false.
Also gives the option to strip tags instead of encoding.'

def sanitize_token(self, token):

    if ((getattr(self, u'wildcard_attributes', None) is None) and isinstance(self.allowed_attributes, dict)):
        self.wildcard_attributes = self.allowed_attributes.get(u'*', [])
    if (token[u'type'] in (tokenTypes[u'StartTag'], tokenTypes[u'EndTag'], tokenTypes[u'EmptyTag'])):
        if (token[u'name'] in self.allowed_elements):
            if (u'data' in token):
                if isinstance(self.allowed_attributes, dict):
                    allowed_attributes = self.allowed_attributes.get(token[u'name'], [])
                    print callable(allowed_attributes)
                    if (not callable(allowed_attributes)):
                        allowed_attributes += self.wildcard_attributes
                else:
                    allowed_attributes = self.allowed_attributes
                attrs = dict([(name, val) for (name, val) in token[u'data'][::(-1)] if (allowed_attributes(name, val) if callable(allowed_attributes) else (name in allowed_attributes))])
                for attr in self.attr_val_is_uri:
                    if (attr not in attrs):
                        continue
                    val_unescaped = re.sub(u'[`\x00-    \x7f-\xa0\\s]+', u'', unescape(attrs[attr])).lower()
                    val_unescaped = val_unescaped.replace(u'\ufffd', u'')
                    if (re.match(u'^[a-z0-9][-+.a-z0-9]*:', val_unescaped) and (val_unescaped.split(u':')[0] not in self.allowed_protocols)):
                        del attrs[attr]
                for attr in self.svg_attr_val_allows_ref:
                    if (attr in attrs):
                        attrs[attr] = re.sub(u'url\\s*\\(\\s*[^#\\s][^)]+?\\)', u'    ', unescape(attrs[attr]))
                if ((token[u'name'] in self.svg_allow_local_href) and (u'xlink:href' in attrs) and re.search(u'^\\s*[^#\\s].*', attrs[u'xlink:href'])):
                    del attrs[u'xlink:href']
                if (u'style' in attrs):
                    attrs[u'style'] = self.sanitize_css(attrs[u'style'])
                token[u'data'] = [(name, val) for (name, val) in attrs.items()]
            return token
        elif self.strip_disallowed_elements:
            pass
        else:
            if (token[u'type'] == tokenTypes[u'EndTag']):
                token[u'data'] = u'</{0!s}>'.format(token[u'name'])
            elif token[u'data']:
                attr = u'    {0!s}="{1!s}"'
                attrs = u''.join([attr.format(k, escape(v)) for (k, v) in token[u'data']])
                token[u'data'] = u'<{0!s}{1!s}>'.format(token[u'name'], attrs)
            else:
                token[u'data'] = u'<{0!s}>'.format(token[u'name'])
            if token[u'selfClosing']:
                token[u'data'] = (token[u'data'][:(-1)] + u'/>')
            token[u'type'] = tokenTypes[u'Characters']
            del token[u'name']
            return token
    elif (token[u'type'] == tokenTypes[u'Comment']):
        if (not self.strip_html_comments):
            return token
    else:
        return token
