Skip to content

Commit 24e4c1b

Browse files
committed
oxml: add .inner_content_elements props
1 parent f46751f commit 24e4c1b

File tree

6 files changed

+73
-1
lines changed

6 files changed

+73
-1
lines changed

src/docx/oxml/document.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,12 @@ def clear_content(self):
7777
"""
7878
for content_elm in self.xpath("./*[not(self::w:sectPr)]"):
7979
self.remove(content_elm)
80+
81+
@property
82+
def inner_content_elements(self) -> List[CT_P | CT_Tbl]:
83+
"""Generate all `w:p` and `w:tbl` elements in this document-body.
84+
85+
Elements appear in document order. Elements shaded by nesting in a `w:ins` or
86+
other "wrapper" element will not be included.
87+
"""
88+
return self.xpath("./w:p | ./w:tbl")

src/docx/oxml/section.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@ class CT_HdrFtr(BaseOxmlElement):
3838
p = ZeroOrMore("w:p", successors=())
3939
tbl = ZeroOrMore("w:tbl", successors=())
4040

41+
@property
42+
def inner_content_elements(self) -> List[CT_P | CT_Tbl]:
43+
"""Generate all `w:p` and `w:tbl` elements in this header or footer.
44+
45+
Elements appear in document order. Elements shaded by nesting in a `w:ins` or
46+
other "wrapper" element will not be included.
47+
"""
48+
return self.xpath("./w:p | ./w:tbl")
49+
4150

4251
class CT_HdrFtrRef(BaseOxmlElement):
4352
"""`w:headerReference` and `w:footerReference` elements."""

src/docx/oxml/table.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,15 @@ def grid_span(self, value):
415415
tcPr = self.get_or_add_tcPr()
416416
tcPr.grid_span = value
417417

418+
@property
419+
def inner_content_elements(self) -> List[CT_P | CT_Tbl]:
420+
"""Generate all `w:p` and `w:tbl` elements in this document-body.
421+
422+
Elements appear in document order. Elements shaded by nesting in a `w:ins` or
423+
other "wrapper" element will not be included.
424+
"""
425+
return self.xpath("./w:p | ./w:tbl")
426+
418427
def iter_block_items(self):
419428
"""Generate a reference to each of the block-level content elements in this
420429
cell, in the order they appear."""

tests/oxml/test_document.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"""Unit-test suite for `docx.oxml.document` module."""
2+
3+
from __future__ import annotations
4+
5+
from typing import cast
6+
7+
from docx.oxml.document import CT_Body
8+
from docx.oxml.table import CT_Tbl
9+
from docx.oxml.text.paragraph import CT_P
10+
11+
from ..unitutil.cxml import element
12+
13+
14+
class DescribeCT_Body:
15+
"""Unit-test suite for selected units of `docx.oxml.document.CT_Body`."""
16+
17+
def it_knows_its_inner_content_block_item_elements(self):
18+
body = cast(CT_Body, element("w:body/(w:tbl, w:p,w:p)"))
19+
assert [type(e) for e in body.inner_content_elements] == [CT_Tbl, CT_P, CT_P]

tests/oxml/test_section.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"""Unit-test suite for `docx.oxml.section` module."""
2+
3+
from __future__ import annotations
4+
5+
from typing import cast
6+
7+
from docx.oxml.section import CT_HdrFtr
8+
from docx.oxml.table import CT_Tbl
9+
from docx.oxml.text.paragraph import CT_P
10+
11+
from ..unitutil.cxml import element
12+
13+
14+
class DescribeCT_HdrFtr:
15+
"""Unit-test suite for selected units of `docx.oxml.section.CT_HdrFtr`."""
16+
17+
def it_knows_its_inner_content_block_item_elements(self):
18+
hdr = cast(CT_HdrFtr, element("w:hdr/(w:tbl,w:tbl,w:p)"))
19+
assert [type(e) for e in hdr.inner_content_elements] == [CT_Tbl, CT_Tbl, CT_P]

tests/oxml/test_table.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22

33
from __future__ import annotations
44

5+
from typing import cast
6+
57
import pytest
68

79
from docx.exceptions import InvalidSpanError
810
from docx.oxml.parser import parse_xml
9-
from docx.oxml.table import CT_Row, CT_Tc
11+
from docx.oxml.table import CT_Row, CT_Tbl, CT_Tc
12+
from docx.oxml.text.paragraph import CT_P
1013

1114
from ..unitutil.cxml import element, xml
1215
from ..unitutil.file import snippet_seq
@@ -102,6 +105,10 @@ def it_can_extend_its_horz_span_to_help_merge(
102105
]
103106
assert tc.vMerge == vMerge
104107

108+
def it_knows_its_inner_content_block_item_elements(self):
109+
tc = cast(CT_Tc, element("w:tc/(w:p,w:tbl,w:p)"))
110+
assert [type(e) for e in tc.inner_content_elements] == [CT_P, CT_Tbl, CT_P]
111+
105112
def it_can_swallow_the_next_tc_help_merge(self, swallow_fixture):
106113
tc, grid_width, top_tc, tr, expected_xml = swallow_fixture
107114
tc._swallow_next_tc(grid_width, top_tc)

0 commit comments

Comments
 (0)