Reject a deletion by re-inserting the deleted content. Creates w:ins elements after each w:del, copying deleted content and converting w:delText back to w:t. Can process a single w:del element or a container element with multiple w:del. Args: elem: Eleme
(self, elem)
| 340 | return [elem] |
| 341 | |
| 342 | def revert_deletion(self, elem): |
| 343 | """Reject a deletion by re-inserting the deleted content. |
| 344 | |
| 345 | Creates w:ins elements after each w:del, copying deleted content and |
| 346 | converting w:delText back to w:t. |
| 347 | Can process a single w:del element or a container element with multiple w:del. |
| 348 | |
| 349 | Args: |
| 350 | elem: Element to process (w:del, w:p, w:body, etc.) |
| 351 | |
| 352 | Returns: |
| 353 | list: If elem is w:del, returns [elem, new_ins]. Otherwise returns [elem]. |
| 354 | |
| 355 | Raises: |
| 356 | ValueError: If the element contains no w:del elements |
| 357 | |
| 358 | Example: |
| 359 | # Reject a single deletion - returns [w:del, w:ins] |
| 360 | del_elem = doc["word/document.xml"].get_node(tag="w:del", attrs={"w:id": "3"}) |
| 361 | nodes = doc["word/document.xml"].revert_deletion(del_elem) |
| 362 | |
| 363 | # Reject all deletions in a paragraph - returns [para] |
| 364 | para = doc["word/document.xml"].get_node(tag="w:p", line_number=42) |
| 365 | nodes = doc["word/document.xml"].revert_deletion(para) |
| 366 | """ |
| 367 | # Collect deletions FIRST - before we modify the DOM |
| 368 | del_elements = [] |
| 369 | is_single_del = elem.tagName == "w:del" |
| 370 | |
| 371 | if is_single_del: |
| 372 | del_elements.append(elem) |
| 373 | else: |
| 374 | del_elements.extend(elem.getElementsByTagName("w:del")) |
| 375 | |
| 376 | # Validate that there are deletions to reject |
| 377 | if not del_elements: |
| 378 | raise ValueError( |
| 379 | f"revert_deletion requires w:del elements. " |
| 380 | f"The provided element <{elem.tagName}> contains no deletions. " |
| 381 | ) |
| 382 | |
| 383 | # Track created insertion (only relevant if elem is a single w:del) |
| 384 | created_insertion = None |
| 385 | |
| 386 | # Process all deletions - create insertions that copy the deleted content |
| 387 | for del_elem in del_elements: |
| 388 | # Clone the deleted runs and convert them to insertions |
| 389 | runs = list(del_elem.getElementsByTagName("w:r")) |
| 390 | if not runs: |
| 391 | continue |
| 392 | |
| 393 | # Create insertion wrapper |
| 394 | ins_elem = self.dom.createElement("w:ins") |
| 395 | |
| 396 | for run in runs: |
| 397 | # Clone the run |
| 398 | new_run = run.cloneNode(True) |
| 399 |
nothing calls this directly
no test coverage detected