Ejemplo n.º 1
0
 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)
Ejemplo n.º 2
0
 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)
Ejemplo n.º 3
0
 def test_build_media_query_up_display(self):
     css_class = 'display-small-up'
     name = 'display'
     value = 'none'
     expected = (
         '@media only screen and (max-width: 15.0625em) {\n' +
         '\t.display-small-up {\n' +
         '\t\tdisplay: none;\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)
Ejemplo n.º 4
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)