HTML¶
Utilities for working with HTML elements. Used for reading attributes and sanitizing HTML.
from prairielearn import ...
check_attribs ¶
check_attribs(
element: HtmlElement,
required_attribs: list[str],
optional_attribs: list[str],
) -> None
Verify that the element has all required attributes and no unknown attributes.
escape_invalid_string ¶
Wrap and escape string in <code>
tags.
Returns:
Type | Description |
---|---|
str
|
The sanitized user input wrapped in a code block. |
get_boolean_attrib ¶
Return the named attribute for the element, or the (optional) default value. If the default value is not provided and the attribute is missing then an exception is thrown. If the attribute is not a valid boolean then an exception is thrown.
Returns:
Type | Description |
---|---|
bool | None
|
The boolean value of attribute |
Raises:
Type | Description |
---|---|
ValueError
|
If the attribute is not a valid boolean value. |
get_color_attrib ¶
Return a 3-digit or 6-digit hex RGB string in CSS format (e.g., '#123' or '#1a2b3c'), or the (optional) default value. If the default value is not provided and the attribute is missing then an exception is thrown. If the attribute is not a valid RGB string then it will be checked against various named colors. If the attribute is still not valid an exception is thrown.
Returns:
Type | Description |
---|---|
str | None
|
A CSS color string. |
Raises:
Type | Description |
---|---|
ValueError
|
If the attribute is not a valid CSS color string. |
get_enum_attrib ¶
get_enum_attrib(
element: HtmlElement,
name: str,
enum_type: type[EnumT],
default: EnumT | None = None,
) -> EnumT
Return the named attribute for the element parsed as an enum, or the (optional) default value. If the default value is not provided and the attribute is missing then an exception is thrown. An exception is also thrown if the value for the enum provided is invalid. Also, alter the enum names to comply with PL naming convention automatically (replacing underscores with dashes and uppercasing). If a default value is provided, it must be a member of the given enum.
Returns:
Type | Description |
---|---|
EnumT
|
The value of attribute |
Raises:
Type | Description |
---|---|
ValueError
|
If the attribute is not a valid enum value. |
get_float_attrib ¶
Return the named attribute for the element, or the (optional) default value. If the default value is not provided and the attribute is missing then an exception is thrown. If the attribute is not a valid floating-point number then an exception is thrown.
Returns:
Type | Description |
---|---|
float | None
|
The float value of attribute |
Raises:
Type | Description |
---|---|
ValueError
|
If the attribute is not a valid floating-point number. |
Examples:
>>> get_float_attrib(element, "stroke-width", 4.0)
10.0
get_integer_attrib ¶
Return the named attribute for the element, or the (optional) default value. If the default value is not provided and the attribute is missing then an exception is thrown. If the attribute is not a valid integer then an exception is thrown.
Returns:
Type | Description |
---|---|
int | None
|
The int value of attribute |
Raises:
Type | Description |
---|---|
ValueError
|
If the attribute is not a valid integer value. |
get_string_attrib ¶
Return the named attribute for the element, or the (optional) default value. If the default value is not provided and the attribute is missing then an exception is thrown.
Returns:
Type | Description |
---|---|
str | None
|
The string value of attribute |