Ejemplo n.º 1
0
 def test_generate_scaling_css_invalid_property_name_is_not_scaling(self):
     css_class = 'white-space-nowrap-s'
     name = 'white-space'
     value = 'nowrap'
     priority = ''
     css_property = Property(name=name, value=value, priority=priority)
     expected = ''
     scaling_parser = ScalingParser(css_class=css_class, css_property=css_property)
     css = scaling_parser.build_media_query()
     self.assertEqual(css, expected, msg=css)
Ejemplo n.º 2
0
    def test_strip_scaling_flag(self):
        valid_css_classes = [
            'font-size-34-s', 'font-size-24-s-i', 'padding-12-s', 'margin-31-s-i',
            'font-size-10', 'font-size-13-i',
        ]
        names = ['font-size', 'font-size', 'padding', 'margin', 'font-size', 'font-size', ]
        values = ['34px', '24px', '12px', '31px', '15px', '52px', '10px', '13px', ]
        priorities = ['', 'important', '', 'important', '', '', '', '', ]
        expected = [
            'font-size-34', 'font-size-24-i', 'padding-12', 'margin-31-i',
            'font-size-10', 'font-size-13-i',
        ]

        for i, css_class in enumerate(valid_css_classes):
            css_property = Property(name=names[i], value=values[i], priority=priorities[i])
            scaling_parser = ScalingParser(css_class=css_class, css_property=css_property)
            clean_css_class = scaling_parser.strip_scaling_flag()
            self.assertEqual(clean_css_class, expected[i])
Ejemplo n.º 3
0
 def test_generate_scaling_css_em(self):
     css_class = 'font-size-24-s'
     name = 'font-size'
     value = '1.5em'
     priority = ''
     css_property = Property(name=name, value=value, priority=priority)
     expected = (
         '.font-size-24-s { font-size: ' + value + '; }\n\n' +
         '@media only screen and (max-width: 64.0em) {\n' +
         '\t.font-size-24-s { font-size: 1.4382em; }\n' +
         '}\n\n' +
         '@media only screen and (max-width: 45.0em) {\n' +
         '\t.font-size-24-s { font-size: 1.3333em; }\n' +
         '}\n\n' +
         '@media only screen and (max-width: 30.0em) {\n' +
         '\t.font-size-24-s { font-size: 1.2em; }\n' +
         '}\n\n'
     )
     scaling_parser = ScalingParser(css_class=css_class, css_property=css_property)
     css = scaling_parser.build_media_query()
     self.assertEqual(css, expected, msg=css)
Ejemplo n.º 4
0
 def test_generate_scaling_css_rem_important(self):
     css_class = 'font-size-24rem-s-i'
     name = 'font-size'
     value = '24rem'
     priority = 'important'
     css_property = Property(name=name, value=value, priority=priority)
     expected = (
         '.font-size-24rem-s-i { font-size: ' + value + ' !important; }\n\n' +
         '@media only screen and (max-width: 64.0em) {\n' +
         '\t.font-size-24rem-s-i { font-size: 23.0105rem !important; }\n' +
         '}\n\n' +
         '@media only screen and (max-width: 45.0em) {\n' +
         '\t.font-size-24rem-s-i { font-size: 21.3333rem !important; }\n' +
         '}\n\n' +
         '@media only screen and (max-width: 30.0em) {\n' +
         '\t.font-size-24rem-s-i { font-size: 19.2rem !important; }\n' +
         '}\n\n'
     )
     scaling_parser = ScalingParser(css_class=css_class, css_property=css_property)
     css = scaling_parser.build_media_query()
     self.assertEqual(css, expected, msg=css)
Ejemplo n.º 5
0
 def test_generate_scaling_css_invalid_units(self):
     # TODO: Decide whether to create unit validator or to continue relying on cssutils for validation.
     css_class = 'font-size-24parsecs-s-i'
     name = 'font-size'
     value = '24parsecs'
     priority = 'important'
     css_property = Property(name=name, value=value, priority=priority)
     expected = (
         '.font-size-24parsecs-s-i { font-size: ' + value + ' !important; }\n\n' +
         '@media only screen and (max-width: 64.0em) {\n' +
         '\t.font-size-24parsecs-s-i { font-size: 23.0105parsecs !important; }\n' +
         '}\n\n' +
         '@media only screen and (max-width: 45.0em) {\n' +
         '\t.font-size-24parsecs-s-i { font-size: 21.3333parsecs !important; }\n' +
         '}\n\n' +
         '@media only screen and (max-width: 30.0em) {\n' +
         '\t.font-size-24parsecs-s-i { font-size: 19.2parsecs !important; }\n' +
         '}\n\n'
     )
     scaling_parser = ScalingParser(css_class=css_class, css_property=css_property)
     css = scaling_parser.build_media_query()
     self.assertEqual(css, expected, msg=css)
Ejemplo n.º 6
0
    def __init__(self, property_parser=ClassPropertyParser()):
        message = 'MediaQueryBuilder Running...'
        print(message)
        logging.debug(msg=message)
        self.property_parser = property_parser
        self.css_media_queries = set()
        self.media_query_text = ''

        not_media_classes = dict()
        for css_class in self.property_parser.class_set:
            name = self.property_parser.get_property_name(css_class=css_class)
            priority = self.property_parser.get_property_priority(css_class=css_class)
            clean_css_class = ''    # Prevents css_class from being modified.

            if name:
                # value='inherit' since we do not know if the class is valid yet.
                inherit_property = Property(name=name, value='inherit', priority=priority)

                scaling_parser = ScalingParser(css_class=css_class, css_property=inherit_property)
                is_scaling = scaling_parser.is_scaling
                if is_scaling:
                    clean_css_class = scaling_parser.strip_scaling_flag()

                breakpoint_parser = BreakpointParser(css_class=css_class, css_property=inherit_property)
                is_breakpoint = breakpoint_parser.is_breakpoint
                if is_breakpoint:
                    clean_css_class = breakpoint_parser.strip_breakpoint_limit()

                if is_breakpoint and is_scaling:                                                    # Mixed syntax
                    not_media_classes[css_class] = ' (Breakpoint and scaling media query syntax cannot be combined.)'
                    continue

                if not is_breakpoint and not is_scaling:                                            # Missing syntax
                    not_media_classes[css_class] = ' is not a media query css_class selector.'
                    continue
            else:
                not_media_classes[css_class] = ' is not a media query css_class selector.'
                continue

            if clean_css_class and property_parser.is_important(css_class=clean_css_class):
                clean_css_class = property_parser.strip_priority_designator(css_class=clean_css_class)

            # Set property value.
            # Handles case where css_class equals 'small-down', 'large-only', 'medium-up', etc.
            # Specifically handle the 'display' case.
            if clean_css_class and clean_css_class != 'display':
                # Can return an empty string '' if css_class does not match any patterns in the property_alias_dict.
                try:
                    encoded_property_value = self.property_parser.get_encoded_property_value(
                        property_name=name,
                        css_class=clean_css_class
                    )
                    value = self.property_parser.get_property_value(
                        property_name=name,
                        encoded_property_value=encoded_property_value
                    )
                except ValueError:  # Impossible to get here if get_property_name() is working properly.
                    not_media_classes[css_class] = ' property_name not found in property_alias_dict.'
                    continue
            else:
                value = 'none'      # Breakpoint Parser Case -> display: none;

            # Build CSS Property AND Add to css_media_queries OR Remove invalid css_class from class_set.
            try:
                css_property = Property(name=name, value=value, priority=priority)

                if css_property.valid:
                    if is_breakpoint and breakpoint_parser:
                        breakpoint_parser.css_property = css_property
                        media_query = breakpoint_parser.build_media_query()
                        self.css_media_queries.add(media_query)
                    if is_scaling:
                        scaling_parser.css_property = css_property
                        media_query = scaling_parser.build_media_query()
                        self.css_media_queries.add(media_query)
                else:
                    not_media_classes[css_class] = ' (cssutils invalid property value: ' + value + ')'
                    continue
            # This exception can't be tested as clean_class_set() and get_property_value() prevent it.(Triple Redundant)
            except SyntaxErr:   # Special Case - Not Tested
                not_media_classes[css_class] = ' (cssutils SyntaxErr invalid property value: ' + value + ')'
                continue

        # Clean out invalid CSS Classes.
        for invalid_css_class, reason in not_media_classes.items():
            self.property_parser.class_set.remove(invalid_css_class)
            self.property_parser.removed_class_set.add(invalid_css_class + reason)