def __init__(self, property='color', match_type='*', query='', action='remove', action_data=''): self.property_name = property.lower() self.action, self.action_data = action, action_data if self.action == 'append': decl = safe_parser().parseStyle(self.action_data) self.appended_properties = list(all_properties(decl)) elif self.action in '+-/*': self.action_operator = partial(transform_number, float(self.action_data), getattr(operator, operator_map[self.action])) if match_type == 'is': self.property_matches = lambda x: x.lower() == query elif match_type == 'is_not': self.property_matches = lambda x: x.lower() != query elif match_type == '*': self.property_matches = lambda x: True elif 'matches' in match_type: q = compile_pat(query) if match_type.startswith('not_'): self.property_matches = lambda x: q.match(x) is None else: self.property_matches = lambda x: q.match(x) is not None else: value, unit = parse_css_length_or_number(query) op = getattr(operator, operator_map[match_type]) pts = unit_convert(value, unit) self.property_matches = partial(numeric_match, value, unit, pts, op)
def __init__(self, property='color', match_type='*', query='', action='remove', action_data=''): self.property_name = property.lower() self.action, self.action_data = action, action_data if self.action == 'append': decl = safe_parser().parseStyle(self.action_data) self.appended_properties = list(all_properties(decl)) elif self.action in '+-/*': self.action_operator = partial( transform_number, float(self.action_data), getattr(operator, operator_map[self.action])) if match_type == 'is': self.property_matches = lambda x: x.lower() == query elif match_type == 'is_not': self.property_matches = lambda x: x.lower() != query elif match_type == '*': self.property_matches = lambda x: True elif 'matches' in match_type: q = compile_pat(query) if match_type.startswith('not_'): self.property_matches = lambda x: q.match(x) is None else: self.property_matches = lambda x: q.match(x) is not None else: value, unit = parse_css_length_or_number(query) op = getattr(operator, operator_map[match_type]) pts = unit_convert(value, unit) self.property_matches = partial(numeric_match, value, unit, pts, op)
def apply_rule(style, **rule): r = Rule(**rule) decl = StyleDeclaration(safe_parser().parseStyle(style)) r.process_declaration(decl) return str(decl)