def __init__(self, parent, simplify_output, output_format):
   SkidmarkHierarchy.__init__(self, parent)
   self.properties = []
   self.simplify_output = simplify_output
   self.output_format = output_format
   self.requires_shorthand_check = False
   
   ShorthandHandler.set_output_format(output_format)
  def add_property(self, property, bypass_expand=False, position=-1):
    """Add a property to the declaration block"""
    
    # Verify if this property already exists, if it does we need to pop it out
    # before appending the new one. This property will essentially crush the
    # previous one and keep the output CSS as clean as possible.
    
    prop_name, prop_value = n_DeclarationBlock.get_property_parts(property)
    
    if self.simplify_output and not bypass_expand and ShorthandHandler.is_shorthand(prop_name):
      properties_to_add = ShorthandHandler.expand_shorthand(prop_name, prop_value)
      
      if properties_to_add:
        for p_name, p_value in properties_to_add:
          if p_value is None:
            self.remove_property(p_name)
          else:
            sep = skidmarkoutputs.OUTPUT_TEMPLATE_PROPERTY_VALUE_SEPARATOR[self.output_format]
            new_property = "%s%s%s" % ( p_name, sep, p_value )
            self.add_property(new_property)
        
        return
    
    expanded_property_names = n_DeclarationBlock._expand_property(self, prop_name)
    property_names = [ n_DeclarationBlock.get_property_parts(prop)[0] for prop in self.properties ]
    
    props_available_for_shorthand = ShorthandHandler.get_properties_available_for_shorthand()
    # If it already exists in the list, remove the original
    for property_name in expanded_property_names:
      if not self.requires_shorthand_check and property_name in props_available_for_shorthand:
        self.requires_shorthand_check = True

      if property_name in property_names:
        # TODO: Right now, we allow duplicating the "background" and "background-image" tag for
        # the gradient support. Is there a better way to to this?
        if not property_name in ("background", "background-image"):
          property_position = property_names.index(property_name)
          self.properties.pop(property_position)
        
      if callable(property_name):
        new_property = property_name(prop_value)
        
        # We need to format this property according to the user's desired output format
        p_name, p_value = n_DeclarationBlock.get_property_parts(new_property)
        sep = skidmarkoutputs.OUTPUT_TEMPLATE_PROPERTY_VALUE_SEPARATOR[self.output_format]
        new_property = "%s%s%s" % ( p_name, sep, p_value )
      else:
        new_property = property.replace(prop_name, property_name)
      
      if position >= 0:
        self.properties.insert(position, new_property)
      else:
        self.properties.append(new_property)
    
    return
 def simplify_shorthandables(self):
   """Scan through the properties to determine if it is possible to regroup
   the entries into shorthand syntax.  This method will take care of updating
   the properties itself and does not return anything.
   
   I decided that 'shorthandables' was a word.  Use it at will."""
   
   # Scan through the properties. Everytime we find a shorthand version, verify
   # all properties defined beforehand to see if they contain property names that
   # this shorthand would overwrite. If this is the case, keep track of these
   # properties so that we can remove them after.
   to_remove = set()
   processed = []
   for property in self.properties:
     prop_name, prop_value = n_DeclarationBlock.get_property_parts(property)
     if ShorthandHandler.is_shorthand(prop_name):
       props_to_nuke = ShorthandHandler.get_all_expand_properties(prop_name)
       for p_nuke in props_to_nuke:
         if p_nuke in processed:
           to_remove.add(processed.index(p_nuke))
     
     processed.append(prop_name)
   
   # Remove the properties that would get overwritten.
   if to_remove:
     to_remove = list(to_remove)
     to_remove.sort()
     
     for idx in to_remove[::-1]:
       self.properties.pop(idx)
       processed.pop(idx)
   
   # Create the shorthand if it's possible (removing the originals)
   for shorthand, shorthand_blocks in PROPERTY_SHORTHANDS.iteritems():
     for blk in shorthand_blocks:
       style = blk[0]
       block_values = [ self.has_property(property_name) for property_name in blk[1:] ]
       shorthand_property = ShorthandHandler.process(style, shorthand, block_values)
       if shorthand_property:
         properties_to_check = [ p_name for p_name in ShorthandHandler.get_all_expand_properties(shorthand) if p_name not in blk[1:] ]
         positions = [ processed.index(p_name) for p_name in properties_to_check if p_name in processed ] + [-1]
         
         self.add_property(shorthand_property, bypass_expand=True, position=positions[0])
         self.remove_property(blk)
         break
   
   return