Attributes

class domilite.accessors.Attributes(_tag: ReferenceType[S])[source]

Bases: MutableMapping[str, str | bool], Generic[S]

Provides a dictionary interface to DOM element attributes.

This interface also transparently forwards interactions with class to the classes object, so that classes can be managed as a set of strings.

The attributes interface normalizes attribute names, transparently handles boolean attributes appropriately, and can render attributes to a string.

This object should not be constructed individually, rather it should be accessed from the attributes attribute of dom_tag

Attributes Summary

classes

Access to the class attribute as a set of strings.

Methods Summary

clear()

delete(key)

Delete an attribute and return the underlying tag.

from_tag(tag)

Construct an Attributes object from a tag object.

get(k[,d])

items()

keys()

normalize_attribute(attribute)

Normalize the name of an attribute.

normalize_pair(attribute, value)

Normalize the name and value of an attribute, handling boolean values appropriately.

pop(k[,d])

If key is not found, d is returned if given, otherwise KeyError is raised.

popitem()

as a 2-tuple; but raise KeyError if D is empty.

render([indent, flags, pretty, xhtml])

Render the attributes as a string.

set(key, value)

Set an attribute to a value, and return the underlying tag.

setdefault(k[,d])

update([E, ]**F)

If E present and has a .keys() method, does: for k in E: D[k] = E[k] If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v In either case, this is followed by: for k, v in F.items(): D[k] = v

values()

Attributes Documentation

classes: Classes[S]

Access to the class attribute as a set of strings.

Methods Documentation

clear() None.  Remove all items from D.
delete(key: str) S[source]

Delete an attribute and return the underlying tag.

This is useful for chaining methods on a tag.

classmethod from_tag(tag: S) Self[source]

Construct an Attributes object from a tag object. Attributes retains a weak reference to the underlying tag.

get(k[, d]) D[k] if k in D, else d.  d defaults to None.
items() a set-like object providing a view on D's items
keys() a set-like object providing a view on D's keys
normalize_attribute(attribute: str) str[source]

Normalize the name of an attribute.

normalize_pair(attribute: str, value: str | bool) tuple[str, str | None][source]

Normalize the name and value of an attribute, handling boolean values appropriately.

Returning a value of None indicates that the attribute should be removed.

pop(k[, d]) v, remove specified key and return the corresponding value.

If key is not found, d is returned if given, otherwise KeyError is raised.

popitem() (k, v), remove and return some (key, value) pair

as a 2-tuple; but raise KeyError if D is empty.

render(indent: str = '  ', flags: ~domilite.render.RenderFlags = <RenderFlags.PRETTY: 1>, pretty: bool | None = None, xhtml: bool | None = None) str[source]

Render the attributes as a string.

Parameters:
  • indent (str, optional) – String to use for indenting in pretty mode. Defaults to two spaces: ` `

  • flags (RenderFlags) – Adjust the rendering properties to use (e.g. turn off PRETTY)

  • pretty (bool or None) – Explicitly enable or disable pretty rendering.

  • xhtml (bool or None) – Explicitly enable or disable xhtml rendering.

set(key: str, value: str | bool) S[source]

Set an attribute to a value, and return the underlying tag.

This is useful for chaining methods on a tag.

setdefault(k[, d]) D.get(k,d), also set D[k]=d if k not in D
update([E, ]**F) None.  Update D from mapping/iterable E and F.

If E present and has a .keys() method, does: for k in E: D[k] = E[k] If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v In either case, this is followed by: for k, v in F.items(): D[k] = v

values() an object providing a view on D's values