gws.lib.style.icon

SVG icon parser.

Source code: gws.lib.style.icon

Module Contents

exception gws.lib.style.icon.Error

Bases: gws.Error

Raised when an icon cannot be loaded or parsed.

gws.lib.style.icon.parse(val: str, opts) → ParsedIcon | None

Load and parse an SVG icon.

The value can be wrapped in CSS url(...) and quotes. It is resolved as follows:

  • a data: URL is decoded,

  • a path is looked up in the image directories (opts.imageDirs),

  • in the trusted mode only, an http(s) URL is fetched, or any other value is read as a file path.

The SVG is normalized (unsafe elements and attributes are removed) and must have a width and a height.

Parameters:
  • val – Data URL, URL or path.

  • opts – Parser options (gws.lib.style.parser.Options).

Returns:

The parsed icon, or None if the value is empty or does not contain an SVG.

Raises:

Error – If the value is untrusted, cannot be loaded or decoded, or the SVG is invalid.

class gws.lib.style.icon.ParsedIcon(*args, **kwargs)

Bases: gws.Data

A parsed icon.

svg: gws.XmlElement

The normalized SVG element.

gws.lib.style.icon.to_data_url(icon: ParsedIcon) → str

Convert a parsed icon to a base64 data URL.

Parameters:

icon – Parsed icon.

Returns:

A data:image/svg+xml;base64,... URL, or an empty string if the icon has no SVG.