When using `picosvg` on https://github.com/googlefonts/picosvg (a Python module dealing with SVGs), there is a weird bug that when given an attribute name starting with dash (e.g. "-inkscape-stroke"), the lxml etree raises an attribute error as below.
Args:
style: CSS declaration list without the enclosing braces,
as found in an SVG element's "style" attribute.
output: a dictionary where to store the parsed properties. property_names: optional set of property names to limit the declarations
to be parsed; if not provided, all will be parsed.
Returns:
A string containing the unparsed style declarations, if any.
Raises:
ValueError if CSS declaration is invalid and can't be parsed.
References: https://www.w3.org/TR/SVG/styling.html#ElementSpecificStyling https://www.w3.org/TR/2013/REC-css-style-attr-20131107/#syntax
"""
unparsed = []
for declaration in style.split(";"):
if declaration.count(":") == 1: property_name, value = declaration.split(":") property_name = property_name.strip()
if property_names is None or property_name in property_names: output[property_name] = value.strip()
else: unparsed.append(declaration.strip())
elif declaration.strip():
raise ValueError(f"Invalid CSS declaration syntax: {declaration}")
return "; ".join(unparsed) + ";" if unparsed else ""
```
where `output` is given a dictionary of attributes:
```py
parse_css_declarations(el.attrib.pop("style", ""), el.attrib)
```
Error:
```
Traceback (most recent call last):
File "d:\Desktop\coding\svg2font\picosvgtest.py", line 15, in <module>
print(text_svg.topicosvg(allow_text=True).tostring())
File "D:\Desktop\coding\svg2font\kivy_venv\lib\site-packages\picosvg\svg.py", line 1343, in topicosvg
svg.topicosvg(ndigits=ndigits, inplace=True, allow_text=allow_text)
File "D:\Desktop\coding\svg2font\kivy_venv\lib\site-packages\picosvg\svg.py", line 1356, in topicosvg
self.apply_style_attributes(inplace=True)
File "D:\Desktop\coding\svg2font\kivy_venv\lib\site-packages\picosvg\svg.py", line 471, in apply_style_attributes
self._apply_styles(el)
File "D:\Desktop\coding\svg2font\kivy_venv\lib\site-packages\picosvg\svg.py", line 454, in _apply_styles
parse_css_declarations(el.attrib.pop("style", ""), el.attrib)
File "D:\Desktop\coding\svg2font\kivy_venv\lib\site-packages\picosvg\svg_meta.py", line 170, in parse_css_declarations
output[property_name] = value.strip()
File "src\lxml\etree.pyx", line 2447, in lxml.etree._Attrib.__setitem__
File "src\lxml\apihelpers.pxi", line 586, in lxml.etree._setAttributeValue
File "src\lxml\apihelpers.pxi", line 1764, in lxml.etree._attributeValidOrRaise
ValueError: Invalid attribute name '-inkscape-font-specification'
```
When using `picosvg` on https:/ /github. com/googlefonts /picosvg (a Python module dealing with SVGs), there is a weird bug that when given an attribute name starting with dash (e.g. "-inkscape- stroke" ), the lxml etree raises an attribute error as below.
Minimal recreate code (after installing picosvg):
```py
from picosvg.svg import SVG
text_svg = SVG.fromstring(""" www.w3. org/2000/ svg" viewBox="0 0 128 128" style=" enable- background: new 0 0 128 128;"> stroke: #FF0000; stroke- width:0. 5;fill: none;-inkscape- font-specificat ion:'Roboto' "/> svg.topicosvg( allow_text= True).tostring( ))
<svg xmlns="http://
<rect width="10" height="10"/>
<path d="M2,2 v6 h6 v-6 h-6 z" style="
</svg>
""")
print(text_
```
Traceback code: declarations( Container[ str]] = None,
```py
def parse_css_
style: str,
output: MutableMapping[str, Any],
property_names: Optional[
) -> str:
"""Parse CSS declaration list into {property: value} dict.
Args:
property_ names: optional set of property names to limit the declarations
style: CSS declaration list without the enclosing braces,
as found in an SVG element's "style" attribute.
output: a dictionary where to store the parsed properties.
to be parsed; if not provided, all will be parsed.
Returns:
A string containing the unparsed style declarations, if any.
Raises:
ValueError if CSS declaration is invalid and can't be parsed.
References: /www.w3. org/TR/ SVG/styling. html#ElementSpe cificStyling /www.w3. org/TR/ 2013/REC- css-style- attr-20131107/ #syntax count(" :") == 1:
property_ name, value = declaration. split(" :")
property_ name = property_ name.strip( )
output[ property_ name] = value.strip()
unparsed. append( declaration. strip() ) strip() : f"Invalid CSS declaration syntax: {declaration}") declarations( el.attrib. pop("style" , ""), el.attrib)
https:/
https:/
"""
unparsed = []
for declaration in style.split(";"):
if declaration.
if property_names is None or property_name in property_names:
else:
elif declaration.
raise ValueError(
return "; ".join(unparsed) + ";" if unparsed else ""
```
where `output` is given a dictionary of attributes:
```py
parse_css_
```
Error: coding\ svg2font\ picosvgtest. py", line 15, in <module> text_svg. topicosvg( allow_text= True).tostring( )) coding\ svg2font\ kivy_venv\ lib\site- packages\ picosvg\ svg.py" , line 1343, in topicosvg topicosvg( ndigits= ndigits, inplace=True, allow_text= allow_text) coding\ svg2font\ kivy_venv\ lib\site- packages\ picosvg\ svg.py" , line 1356, in topicosvg apply_style_ attributes( inplace= True) coding\ svg2font\ kivy_venv\ lib\site- packages\ picosvg\ svg.py" , line 471, in apply_style_ attributes _apply_ styles( el) coding\ svg2font\ kivy_venv\ lib\site- packages\ picosvg\ svg.py" , line 454, in _apply_styles css_declaration s(el.attrib. pop("style" , ""), el.attrib) coding\ svg2font\ kivy_venv\ lib\site- packages\ picosvg\ svg_meta. py", line 170, in parse_css_ declarations property_ name] = value.strip() etree.pyx" , line 2447, in lxml.etree. _Attrib. __setitem_ _ apihelpers. pxi", line 586, in lxml.etree. _setAttributeVa lue apihelpers. pxi", line 1764, in lxml.etree. _attributeValid OrRaise font-specificat ion'
```
Traceback (most recent call last):
File "d:\Desktop\
print(
File "D:\Desktop\
svg.
File "D:\Desktop\
self.
File "D:\Desktop\
self.
File "D:\Desktop\
parse_
File "D:\Desktop\
output[
File "src\lxml\
File "src\lxml\
File "src\lxml\
ValueError: Invalid attribute name '-inkscape-
```