#NEWLINE# def parse_at_rule( self , rule , previous_rules , errors , context ) : #NEWLINE# #INDENT# "Parse an at-rule.\n\n        Subclasses that override this method must use ``super()`` and\n        pass its return value for at-rules they do not know.\n\n        In CSS 2.1, this method handles @charset, @import, @media and @page\n        rules.\n\n        :param rule:\n            An unparsed :class:`AtRule`.\n        :param previous_rules:\n            The list of at-rules and rulesets that have been parsed so far\n            in this context. This list can be used to decide if the current\n            rule is valid. (For example, @import rules are only allowed\n            before anything but a @charset rule.)\n        :param context:\n            Either ``'stylesheet'`` or an at-keyword such as ``'@media'``.\n            (Most at-rules are only allowed in some contexts.)\n        :raises:\n            :class:`~.parsing.ParseError` if the rule is invalid.\n        :return:\n            A parsed at-rule\n\n        " #NEWLINE# if ( rule . at_keyword == '@page' ) : #NEWLINE# #INDENT# if ( context != 'stylesheet' ) : #NEWLINE# #INDENT# raise ParseError ( rule , ( '@page rule not allowed in ' + context ) ) #NEWLINE# #UNINDENT# ( selector , specificity ) = self . parse_page_selector ( rule . head ) #NEWLINE# if ( rule . body is None ) : #NEWLINE# #INDENT# raise ParseError ( rule , 'invalid {0} rule: missing block' . format ( rule . at_keyword ) ) #NEWLINE# #UNINDENT# ( declarations , at_rules , rule_errors ) = self . parse_declarations_and_at_rules ( rule . body , '@page' ) #NEWLINE# errors . extend ( rule_errors ) #NEWLINE# return PageRule ( selector , specificity , declarations , at_rules , rule . line , rule . column ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( rule . at_keyword == '@media' ) : #NEWLINE# #INDENT# if ( context != 'stylesheet' ) : #NEWLINE# #INDENT# raise ParseError ( rule , ( '@media rule not allowed in ' + context ) ) #NEWLINE# #UNINDENT# if ( not rule . head ) : #NEWLINE# #INDENT# raise ParseError ( rule , 'expected media types for @media' ) #NEWLINE# #UNINDENT# media = self . parse_media ( rule . head ) #NEWLINE# if ( rule . body is None ) : #NEWLINE# #INDENT# raise ParseError ( rule , 'invalid {0} rule: missing block' . format ( rule . at_keyword ) ) #NEWLINE# #UNINDENT# ( rules , rule_errors ) = self . parse_rules ( rule . body , '@media' ) #NEWLINE# errors . extend ( rule_errors ) #NEWLINE# return MediaRule ( media , rules , rule . line , rule . column ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( rule . at_keyword == '@import' ) : #NEWLINE# #INDENT# if ( context != 'stylesheet' ) : #NEWLINE# #INDENT# raise ParseError ( rule , ( '@import rule not allowed in ' + context ) ) #NEWLINE# #UNINDENT# for previous_rule in previous_rules : #NEWLINE# #INDENT# if ( previous_rule . at_keyword not in ( '@charset' , '@import' ) ) : #NEWLINE# #INDENT# if previous_rule . at_keyword : #NEWLINE# #INDENT# type_ = 'an {0} rule' . format ( previous_rule . at_keyword ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# type_ = 'a ruleset' #NEWLINE# #UNINDENT# raise ParseError ( previous_rule , ( '@import rule not allowed after ' + type_ ) ) #NEWLINE# #UNINDENT# #UNINDENT# head = rule . head #NEWLINE# if ( not head ) : #NEWLINE# #INDENT# raise ParseError ( rule , 'expected URI or STRING for @import rule' ) #NEWLINE# #UNINDENT# if ( head [ 0 ] . type not in ( 'URI' , 'STRING' ) ) : #NEWLINE# #INDENT# raise ParseError ( rule , ( 'expected URI or STRING for @import rule, got ' + head [ 0 ] . type ) ) #NEWLINE# #UNINDENT# uri = head [ 0 ] . value #NEWLINE# media = self . parse_media ( strip_whitespace ( head [ 1 : ] ) ) #NEWLINE# if ( rule . body is not None ) : #NEWLINE# #INDENT# raise ParseError ( head [ ( - 1 ) ] , "expected ';', got a block" ) #NEWLINE# #UNINDENT# return ImportRule ( uri , media , rule . line , rule . column ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( rule . at_keyword == '@charset' ) : #NEWLINE# #INDENT# raise ParseError ( rule , 'mis-placed or malformed @charset rule' ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# raise ParseError ( rule , 'unknown at-rule in {0} context: {1}' . format ( context , rule . at_keyword ) )