Exemplo n.º 1
0
    def apply_style_attribute(self, inplace=False) -> "SVGShape":
        """Converts inlined CSS in "style" attribute to equivalent SVG attributes.

        Unsupported attributes for which no corresponding field exists in SVGShape
        dataclass are kept as text in the "style" attribute.
        """
        target = self
        if not inplace:
            target = copy.deepcopy(self)
        if target.style:
            attr_types = {
                f.name.replace("_", "-"): f.type
                for f in dataclasses.fields(self)
            }
            raw_attrs = {}
            unparsed_style = svg_meta.parse_css_declarations(
                target.style, raw_attrs, property_names=attr_types.keys())
            for attr_name, attr_value in raw_attrs.items():
                field_name = attr_name.replace("-", "_")
                field_value = attr_types[attr_name](attr_value)
                setattr(target, field_name, field_value)
            target.style = unparsed_style
        return target
Exemplo n.º 2
0
 def _apply_styles(self, el: etree.Element):
     parse_css_declarations(el.attrib.pop("style", ""), el.attrib)
Exemplo n.º 3
0
def test_parse_css_declarations_invalid(style):
    with pytest.raises(ValueError, match="Invalid CSS declaration syntax"):
        parse_css_declarations(style, {})
Exemplo n.º 4
0
def test_parse_css_declarations(style, property_names, expected_output,
                                expected_unparsed):
    output = {}
    unparsed = parse_css_declarations(style, output, property_names)
    assert output == expected_output
    assert unparsed == expected_unparsed