def test_generate_fallback_fonts_family_key(self): font_families = {'serif', 'sans-serif', 'monospace', 'fantasy'} font_parser = FontParser() for font_family in font_families: font_parser.font_value = font_family fallback_fonts = font_parser.generate_fallback_fonts() self.assertEqual(font_family, fallback_fonts)
def test_generate_fallback_fonts_invalid(self): input_fonts = ['invalid', 'border', 'padding', 'oasnuth', 'oe2nth', '234'] expected = '' font_parser = FontParser() for font in input_fonts: font_parser.font_value = font fallback_fonts = font_parser.generate_fallback_fonts() self.assertEqual(fallback_fonts, expected)
def test_generate_fallback_fonts_append_family(self): input_fonts = [ 'cambria', 'didot', 'garamond', 'arial', 'helvetica', 'gadget', 'courier', 'monaco', 'consolas', 'copperplate', 'papyrus', ] expected = [ 'cambria, serif', 'didot, serif', 'garamond, serif', 'arial, sans-serif', 'helvetica, sans-serif', 'gadget, sans-serif', 'courier, monospace', 'monaco, monospace', 'consolas, monospace', 'copperplate, fantasy', 'papyrus, fantasy', ] font_parser = FontParser() for i, font in enumerate(input_fonts): font_parser.font_value = font fallback_fonts = font_parser.generate_fallback_fonts() self.assertEqual(fallback_fonts, expected[i])
def decode_property_value(self, value=''): """ Decode the encoded property ``value`` input e.g. 'bold', '1-5-1-5', '1_32rem', '1p-10p-3p-1p', 'n12px', 'n5_25cm-n6_1cm'. Returns parsed, but non-validated CSS property value. :type value: str :param value: An encoded CSS property value. :return: (*str*) -- Returns the decoded, but non-validated CSS property value. **Examples:** >>> value_parser = CSSPropertyValueParser( >>> property_name='padding', use_em=True >>> ) >>> value_parser.decode_property_value(value='1-5-1-5') '0.0625em 0.3125em 0.0625em 0.3125em' >>> value_parser.unit_parser.use_em = False >>> value_parser.decode_property_value(value='1-5-1-5') '1px 5px 1px 5px' """ # Skip values that are built-in to the CSS standard, and represent the literal property value. if not self.is_built_in(value=value): # Apply to all non-built-in values. value = self.replace_dashes(value=value) # These only apply if value contains a digit. value = self.replace_underscore_with_decimal(value=value) value = self.replace_p_with_percent(value=value) value = self.replace_n_with_minus(value=value) # Parse color and units value = self.color_parser.replace_h_with_hash(value=value) value = self.color_parser.add_color_parenthetical(value=value) value = self.unit_parser.add_units(property_value=value) else: # Generate web safe font-family fallback strings. if self.property_name == 'font-family': font_parser = FontParser(font_value=value) value = font_parser.generate_fallback_fonts() return value