Transform an lxml etree node into a DOM model Parameters: node: The ``lxml.etree._Element`` node transforms: Functions of the form ``transform(old) -> new`` where ``old`` is a VDOM dictionary which will be replaced by ``new``. For example, you
(
node: etree._Element, transforms: Iterable[_ModelTransform]
)
| 124 | |
| 125 | |
| 126 | def _etree_to_vdom( |
| 127 | node: etree._Element, transforms: Iterable[_ModelTransform] |
| 128 | ) -> VdomDict: |
| 129 | """Transform an lxml etree node into a DOM model |
| 130 | |
| 131 | Parameters: |
| 132 | node: |
| 133 | The ``lxml.etree._Element`` node |
| 134 | transforms: |
| 135 | Functions of the form ``transform(old) -> new`` where ``old`` is a VDOM |
| 136 | dictionary which will be replaced by ``new``. For example, you could use a |
| 137 | transform function to add highlighting to a ``<code/>`` block. |
| 138 | """ |
| 139 | if not isinstance(node, etree._Element): # nocov |
| 140 | msg = f"Expected node to be a etree._Element, not {type(node).__name__}" |
| 141 | raise TypeError(msg) |
| 142 | |
| 143 | # Recursively call _etree_to_vdom() on all children |
| 144 | children = _generate_vdom_children(node, transforms) |
| 145 | |
| 146 | # Convert the lxml node to a VDOM dict |
| 147 | el = vdom(node.tag, dict(node.items()), *children) |
| 148 | |
| 149 | # Perform any necessary mutations on the VDOM attributes to meet VDOM spec |
| 150 | _mutate_vdom(el) |
| 151 | |
| 152 | # Apply any provided transforms. |
| 153 | for transform in transforms: |
| 154 | el = transform(el) |
| 155 | |
| 156 | return el |
| 157 | |
| 158 | |
| 159 | def _add_vdom_to_etree(parent: etree._Element, vdom: VdomDict | dict[str, Any]) -> None: |
no test coverage detected