def merge_identical_selectors(sheet):
 selector_map = defaultdict(list)
 for rule in sheet.cssRules.rulesOfType(CSSRule.STYLE_RULE):
  selector_map[rule.selectorText].append(rule)
 remove = []
 for rule_group in selector_map.itervalues():
  if (len(rule_group) > 1):
   for i in range(1, len(rule_group)):
    merge_declarations(rule_group[0].style, rule_group[i].style)
    remove.append(rule_group[i])
 for rule in remove:
  sheet.cssRules.remove(rule)
 return len(remove)

def merge_identical_selectors ( sheet ) : selector_map = defaultdict ( list ) for rule in sheet . cssRules . rulesOfType ( CSSRule . STYLE_RULE ) : selector_map [ rule . selectorText ] . append ( rule ) remove = [ ] for rule_group in selector_map . itervalues ( ) : if ( len ( rule_group ) > 1 ) : for i in range ( 1 , len ( rule_group ) ) : merge_declarations ( rule_group [ 0 ] . style , rule_group [ i ] . style ) remove . append ( rule_group [ i ] ) for rule in remove : sheet . cssRules . remove ( rule ) return len ( remove )
merge rules that have identical selectors .