def get_property_priority(self, css_class=''): """ Returns the keyword ``'important'`` or ``''``. These are in the form that ``cssutils`` understands. :raises ValueError: If the css_class is empty or only contain whitespace values. :type css_class: str :param css_class: An encoded css class selector that may contain property name, value, and priority designator. :return: (str) -- Returns the keyword ``important`` if the property priority is set to important. Otherwise, it returns ''. >>> property_parser = ClassPropertyParser() >>> property_parser.get_property_priority('text-align-center-i') 'important' >>> property_parser.get_property_priority('text-align-center') '' >>> property_parser.get_property_priority('') ValueError >>> property_parser.get_property_priority(' ') ValueError """ deny_empty_or_whitespace(string=css_class, variable_name='css_class') return 'important' if self.is_important(css_class=css_class) else ''
def strip_priority_designator(self, css_class=''): """ Removes the priority designator, if necessary. :raises ValueError: If the css_class is empty or only contain whitespace values. :type css_class: str :param css_class: A css_class that may or may not contain a priority designator. :return: (str) -- If necessary, strip priority designator from the end of css_class and return the string. If the importance_designator is not found, then it returns the unchanged css_class. >>> property_parser = ClassPropertyParser() >>> property_parser.strip_priority_designator('blink-i') 'blink' >>> property_parser.strip_priority_designator('blink') 'blink' >>> property_parser.strip_priority_designator('') ValueError >>> property_parser.strip_priority_designator(' ') ValueError """ deny_empty_or_whitespace(string=css_class, variable_name='css_class') if self.is_important(css_class=css_class): return css_class[:-len(self.importance_designator)] else: return css_class
def is_important(self, css_class=''): """ Tests whether the css_class ends with the importance_designator. :raises ValueError: If the css_class is empty or only contain whitespace values. :type css_class: str :param css_class: An encoded css class selector that may contain property name, value, and priority designator. :return: (bool) -- Returns True if the css_class ends with the importance_designator. Otherwise, returns False. **Examples:** >>> property_parser = ClassPropertyParser() >>> property_parser.is_important('line-through-i') True >>> property_parser.is_important('line-through') False >>> property_parser.is_important('') ValueError >>> property_parser.is_important(' ') ValueError """ deny_empty_or_whitespace(string=css_class, variable_name='css_class') return css_class.endswith(self.importance_designator)
def strip_property_abbreviation(self, property_name='', css_class=''): """ Strip property abbreviation from css_class if applicable and return the result. :raises ValueError: If either property_name or css_class are empty or only contain whitespace values. :type property_name: str :type css_class: str :param property_name: Presumed to match a key or value in the ``property_alias_dict`` :param css_class: Initially this value may or may not contain the property_name. :return: (str) -- Returns the encoded property value portion of the css_class. **Examples:** >>> property_parser = ClassPropertyParser() >>> property_parser.strip_property_abbreviation('color', 'c-lime') 'lime' >>> property_parser.strip_property_abbreviation('', 'c-lime') ValueError >>> property_parser.strip_property_abbreviation('color', ' ') ValueError """ deny_empty_or_whitespace(string=css_class, variable_name='css_class') deny_empty_or_whitespace(string=property_name, variable_name='property_name') property_abbreviations = self.get_property_abbreviations(property_name=property_name) for property_abbreviation in property_abbreviations: if css_class.startswith(property_abbreviation): return css_class[len(property_abbreviation):] return css_class
def strip_property_name(property_name='', css_class=''): """ Strip property name from css_class if applicable and return the css_class. :raises ValueError: If either property_name or css_class are empty or only contain whitespace values. :type property_name: str :type css_class: str :param property_name: Presumed to match a key or value in the ``property_alias_dict``. :param css_class: This value may or may not be identical to the property_value. :return: (str) -- Returns the encoded property value portion of the css_class. **Examples:** >>> ClassPropertyParser.strip_property_name('padding', 'padding-1-2-1-2') '1-2-1-2' >>> ClassPropertyParser.strip_property_name('font-weight', 'bold') 'bold' >>> ClassPropertyParser.strip_property_name('', 'padding-1-2-1-2') ValueError >>> ClassPropertyParser.strip_property_name('font-weight', ' ') ValueError """ deny_empty_or_whitespace(string=css_class, variable_name='css_class') deny_empty_or_whitespace(string=property_name, variable_name='property_name') property_name += '-' # Append '-' to property to match the class format. if css_class.startswith(property_name): # Strip property name return css_class[len(property_name):] else: # If it doesn't have a property name ignore it. return css_class
def __init__(self, css_class='', css_property=Property()): deny_empty_or_whitespace(css_class, variable_name='css_class') deny_empty_or_whitespace(css_property.cssText, variable_name='css_property') self.css_class = css_class self.css_property = css_property self.scale_dict = { 'medium': 1.125, 'small': 1.25, } self.scaling_flag = '-s' self.is_scaling = self._is_scaling()
def get_property_value(self, property_name='', encoded_property_value=''): """ Accepts an encoded_property_value that's been stripped of it's property named and priority Uses CSSPropertyValueParser, and returns a valid css property value or ''. Usage Note: Invalid CSS values can be returned by this method. CSS validation occurs at a later step. :raises ValueError: If either property_name or css_class are empty or only contain whitespace values. :type property_name: str :type encoded_property_value: str :param property_name: Name of CSS property that matches a key in ``property_alias_dict``. :param encoded_property_value: A property value that may or may not contain dashes and underscores. :return: (str) -- An unvalidated / potential CSS property value. **Examples:** >>> property_parser = ClassPropertyParser(set(), False) # Turn OFF unit conversion. >>> property_parser.get_property_value('font-weight', 'bold') # Special case: alias == property value 'bold' >>> property_parser.get_property_value('padding', '1-10-10-5') # Multi-value encoding contains dashes. '1px 10px 10px 5px' >>> property_parser.get_property_value('width', '7_25rem') # Single value contains underscores. '7.25rem' >>> property_parser.get_property_value('margin', '1ap-10xp-3qp-1mp3') # Invalid CSS returned. '1a% 10x% 3q% 1mp3' >>> property_parser.get_property_value('', 'c-lime') ValueError >>> property_parser.get_property_value('color', ' ') ValueError """ deny_empty_or_whitespace(string=property_name, variable_name='property_name') deny_empty_or_whitespace(string=encoded_property_value, variable_name='encoded_property_value') value_parser = CSSPropertyValueParser(property_name=property_name) value = value_parser.decode_property_value(value=encoded_property_value) return value
def build_media_query(self): """ Returns CSS media queries that scales pixel / em values in response to screen size changes. **Generated CSS for ``font-size-24-s`` minus the inline comments**:: .font-size-24-s { // Default size above medium font-size: 24px; // medium screen font size reduction @media only screen and (max-width: 45.0em) { font-size: 21.3px; } // small screen font size reduction @media only screen and (max-width: 30.0em) { font-size: 19.2px; } } **Priority !important -- Generated CSS for ``font-size-24-s-i`` minus the inline comments**:: // Default size above the maximum 'medium' width breakpoint. .font-size-24-s-i { font-size: 24px !important; } // Apply 'medium' screen font size reduction. @media only screen and (max-width: 45.0em) { .font-size-24-s-i { font-size: 21.3px !important; } } // Apply 'small' screen font size reduction. @media only screen and (max-width: 30.0em) { .font-size-24-s-i { font-size: 19.2px !important; } } :return: (*str*) -- Returns CSS media queries that scales pixel / em values in response to screen size changes. """ if not self.is_scaling: return '' name = self.css_property.name value = self.css_property.value units = ''.join(filter(lambda x: x.isalpha(), value)) # Only keep letters. priority = self.css_property.priority deny_empty_or_whitespace(str(value), variable_name='value') float_value = float(value.replace(units, '')) # Remove units. _max = 1 small_max = small[_max] medium_max = medium[_max] medium_property = Property(name=name, value=value, priority=priority) small_property = Property(name=name, value=value, priority=priority) medium_value = round(float_value / self.scale_dict['medium'], 4) # Scale to medium screen medium_value = str(medium_value) + units # Add units small_value = round(float_value / self.scale_dict['small'], 4) # Scale to small screen small_value = str(small_value) + units # Add units medium_property.value = medium_value small_property.value = small_value return ( '.' + self.css_class + ' { ' + self.css_property.cssText + '; }\n\n' + '@media only screen and (max-width: ' + medium_max + ') {\n' + '\t.' + self.css_class + ' { ' + medium_property.cssText + '; }\n' + '}\n\n' + '@media only screen and (max-width: ' + small_max + ') {\n' + '\t.' + self.css_class + ' { ' + small_property.cssText + '; }\n' + '}\n\n' )
def test_deny_empty_or_whitespace_valid(self): self.assertEqual(deny_empty_or_whitespace(string='valid_string', variable_name='valid_variable'), None)