Exemple #1
0
    def replace_n_with_minus(value=''):
        """ If a space plus the letter ``' n'`` is immediately followed by a digit replace it with ``' -'``.
        If ``n`` is the first letter of the string and followed by digits replace it with ``-``.
        The letter ``n`` is an encoding for a negative sign. Leaves other ``n's`` unmodified.

        | ``' n2'`` becomes ``' -2'``  Mind the space.
        | ``'n5'`` becomes ``'-5'``

        :type value: str

        :param value: Encoded CSS property value.
        :return: (*str*) -- Return the value with minus signs added if necessary.

        **Example:**

        >>> # Multi-value: 'n5cm n6cm' --> '-5cm -6cm'
        >>> value_parser = CSSPropertyValueParser(
        >>>     property_name='padding', use_em=True
        >>> )
        >>> value_parser.replace_n_with_minus('n5cm n6cm')
        '-5cm -6cm'
        >>> #
        >>> # 'n9in' --> '-9in' (note that the 'n' at the end is not touched)
        >>> value_parser.replace_n_with_minus('n9in')
        '-9in'

        """
        if contains_a_digit(string=value):
            value = value.replace(' n', ' -')
            if value.startswith('n'):
                value = '-' + value[1:]     # add minus sign and chop first character
        return value
Exemple #2
0
    def replace_p_with_percent(value=''):
        """ Replace ``'p'`` suffix with ``'%'`` if found at the end of any substring containing digits.

        ``'p '`` becomes ``'%'``

        Mind the space

        :type value: str

        :param value: Encoded CSS property value.
        :return: (*str*) -- Return the value with percent signs added if necessary.

        **Example:**

        >>> # Multi-value: '1p 10p 3p 1p' --> '1% 10% 3% 1%'
        >>> value_parser = CSSPropertyValueParser(
        >>>     property_name='padding', use_em=True
        >>> )
        >>> value_parser.replace_p_with_percent(value='1p 10p 3p 1p')
        '1% 10% 3% 1%'
        >>> #
        >>> # Single value ' 1p' --> ' 1%'
        >>> value_parser = CSSPropertyValueParser(
        >>>     property_name='padding', use_em=True
        >>> )
        >>> value_parser.replace_p_with_percent(value=' 1p')
        ' 1%'

        """
        if contains_a_digit(string=value):
            value = value.replace('p ', '% ')
            if value.endswith('p'):
                value = value[:-1] + '%'    # chop last character and add percentage sign
        return value
Exemple #3
0
    def add_color_parenthetical(self, value=''):
        """ Convert parenthetical color values: rbg, rbga, hsl, hsla to valid css format

        Assumes that color conversion happens after dashes, decimal point, negative signs, and percentage signs
        are converted.

        **Note:** Currently not compatible with shorthand properties.

        :type value: str

        :param value: Space delimited rbg, rbga, hsl, hsla values.
        :return: (str) -- Returns the valid css color parenthetical. Returns the input ``value`` unchanged
            for the non-matching case.

        **Examples:**

        >>> color_parser = ColorParser('color', '')
        >>> color_parser.add_color_parenthetical('rgb 0 255 0')
        rgb(0, 255, 0)
        >>> color_parser.add_color_parenthetical('rgba 255 0 0 0.5')
        rgba(255, 0, 0, 0.5)
        >>> color_parser.add_color_parenthetical('hsl 120 60% 70%')
        hsl(120, 60%, 70%)
        >>> color_parser.add_color_parenthetical('hsla 120 60% 70% 0.3')
        hsla(120, 60%, 70%, 0.3)
        >>> # Pass-through case as no conversion is possible.
        >>> color_parser.add_color_parenthetical('hsla')
        hsla

        """
        if self.property_name_allows_color():
            if contains_a_digit(string=value):
                keywords = {'rgb ', 'rgba ', 'hsl ', 'hsla '}
                for key in keywords:
                    if value.startswith(key):
                        value = value.replace(key, key.strip() + '(')   # Remove key whitespace and add opening '('
                        value += ')'                                    # Add closing ')'
                        value = value.replace(' ', ', ')                # Add commas ', '
                        break
        return value
Exemple #4
0
    def replace_underscore_with_decimal(value=''):
        """ Replace underscore with decimal point. Underscores are used to encode a decimal point

        ``'_'`` becomes ``'.'``

        :type value: str

        :param value: Encoded CSS property value.
        :return: (*str*) -- Return the value with decimal points added if necessary.

        **Example**

        >>> value_parser = CSSPropertyValueParser(
        >>>     property_name='padding', use_em=True
        >>> )
        >>> value_parser.replace_underscore_with_decimal('1_32rem')
        '1.32rem'

        """
        if contains_a_digit(string=value):
            value = value.replace('_', '.')
        return value
Exemple #5
0
 def test_contains_a_digit_false(self):
     no_digits = ['bold', 'none', 'left']
     for value in no_digits:
         self.assertFalse(contains_a_digit(string=value), msg=value)
Exemple #6
0
 def test_contains_a_digit_true(self):
     digits = ['n12px', '1p 7p 1p 7p', '-1_25em', '-1.35%', 'rgba 255 0 0 0.5', 'h0ff48f']
     for value in digits:
         self.assertTrue(contains_a_digit(string=value), msg=value)