HEX
Server: Apache/2.4.46 (Win64) OpenSSL/1.1.1j PHP/8.4.25
System: Windows NT DESKTOP-4TAV2RJ 10.0 build 19045 (Windows 10) AMD64
User: fred (0)
PHP: 8.4.25
Disabled: NONE
Upload Files
File: C:/Users/fred/anaconda3/Lib/site-packages/qstylizer/parser.py
# coding: utf-8

import tinycss2

import qstylizer.style


def parse(stylesheet):
    """Parse a stylesheet using tinycss2 and return a StyleSheet instance.

    :param stylesheet: A string of an existing stylesheet.

    """
    parsed_stylesheet = tinycss2.parse_stylesheet(
        stylesheet, skip_comments=True, skip_whitespace=True
    )
    css = qstylizer.style.StyleSheet()
    for node in parsed_stylesheet:
        if node.type == "error":
            raise ValueError("Cannot parse Stylesheet: " + node.message)
        selector = tinycss2.serialize(node.prelude).strip()
        declaration_list = tinycss2.parse_declaration_list(
            node.content, skip_comments=True, skip_whitespace=True
        )
        for declaration in declaration_list:
            if declaration.type == "declaration":
                prop = declaration.name.strip()
                css[selector][prop] = tinycss2.serialize(declaration.value).strip()
    return css