def test_build_media_query_invalid_limit_key(self): css_class = 'padding-100-small-up' name = 'padding' value = px_to_em('100') expected = '' css_property = Property(name=name, value=value, priority='') breakpoint_parser = BreakpointParser(css_class=css_class, css_property=css_property) breakpoint_parser.limit_key = 'invalid_key' css = breakpoint_parser.build_media_query() self.assertEqual(css, expected)
def test_css_for_up_wrong_limit_key(self): css_class = 'padding-100-small-up' name = 'padding' value = px_to_em('100') expected = '' css_property = Property(name=name, value=value, priority='') breakpoint_parser = BreakpointParser(css_class=css_class, css_property=css_property) breakpoint_parser.limit_key = '-only' # Change to WRONG LIMIT KEY css = breakpoint_parser.css_for_up() self.assertEqual(css, expected)
def test_build_media_query_up_general_usage(self): css_class = 'padding-100-small-up' name = 'padding' value = px_to_em('100') expected = ( '@media only screen and (min-width: 15.0625em) {\n' + '\t.padding-100-small-up {\n' + '\t\tpadding: 6.25em;\n' + '\t}\n' + '}\n\n' ) css_property = Property(name=name, value=value, priority='') breakpoint_parser = BreakpointParser(css_class=css_class, css_property=css_property) css = breakpoint_parser.build_media_query() self.assertEqual(css, expected)
def test_css_for_down_general_usage(self): css_class = 'padding-100-medium-down' name = 'padding' value = px_to_em('100') expected = ( '@media only screen and (max-width: 45.0em) {\n' + '\t.padding-100-medium-down {\n' + '\t\tpadding: 6.25em;\n' + '\t}\n' + '}\n\n' ) css_property = Property(name=name, value=value, priority='') breakpoint_parser = BreakpointParser(css_class=css_class, css_property=css_property) css = breakpoint_parser.css_for_down() self.assertEqual(css, expected)
def test_css_for_only_general_usage_important(self): css_class = 'padding-100-large-only-i' name = 'padding' value = px_to_em('100') priority = 'important' expected = ( '@media only screen and (min-width: 45.0625em) and (max-width: 64.0em) {\n' + '\t.padding-100-large-only-i {\n' + '\t\tpadding: 6.25em !important;\n' + '\t}\n' + '}\n\n' ) css_property = Property(name=name, value=value, priority=priority) breakpoint_parser = BreakpointParser(css_class=css_class, css_property=css_property) css = breakpoint_parser.css_for_only() self.assertEqual(css, expected)
def add_units(self, property_value=''): """ If the property_name requires units, then apply the default units defined in default_property_units_dict. **Rules:** - If use_em is False apply the default units for the property name by looking it up in default_property_units_dict. - Unit that have default units of ``px`` are converted to ``em`` if use_em is True. - If ``property_value`` has multiple property values, then split it apart. - If the value already has units, then pass it through unchanged. - The value provided shall possess negative signs and decimal points. - Mixed units are allowed, but **not recommended**. - Values shall only contain [] e.g. -1.25 can be processed, but n1_25 cannot be processed. :type property_value: str :param property_value: A string containing one or more space delimited alphanumeric characters. :return: (str) -- Returns the property value with the default or converted units added. >>> # Convert 'px' to 'em' >>> unit_parser = UnitParser(property_name='padding', use_em=True) >>> unit_parser.add_units('1 2 1 2') 0.0625em 0.125em 0.0625em 0.125em >>> # Use default units >>> unit_parser.use_em = False >>> unit_parser.add_units('1 2 1 2') 1px 2px 1px 2px >>> # Values already have units or are not parsable pass through >>> # True produces the same output. >>> unit_parser.use_em = False >>> unit_parser.add_units('55zp') 55zp >>> unit_parser.add_units('17rem') 17rem >>> # Unitless ``property_name`` >>> # causes ``property_value`` to pass through. >>> unit_parser.property_name = 'font-weight' >>> unit_parser.add_units('200') 200 >>> # Mixed units cases - Not a Recommended Practice, >>> # but represent valid CSS. Be careful. >>> unit_parser.use_em = False >>> unit_parser.add_units('5em 6 5em 6') 5em 6px 5em 6px >>> unit_parser.use_em = True >>> unit_parser.add_units('1em 100 4cm 9rem') 1em 6.25em 4cm 9rem """ new_value = [] try: default_units = self.default_property_units_dict[self.property_name] # See if property_name has units. for val in property_value.split(): # single, double and quadruple if set(val) <= self.allowed: val = val.replace('px', '') # Handle 'px' units case. if settings.use_em and default_units == 'px': # Convert units if required. new_value.append(settings.px_to_em(pixels=val)) else: new_value.append(val + default_units) # Use default units. else: new_value.append(val) # Pass through and ignore value. property_value = ' '.join(new_value) # Put the new values back together. except KeyError: pass # Property is unitless. return property_value