|
| 1 | +"""Physical HTML cells preserve filed Inline XBRL fact identities. |
| 2 | +
|
| 3 | +GitHub Issue: https://github.com/dgunning/edgartools/issues/2016 |
| 4 | +""" |
| 5 | + |
| 6 | +import pytest |
| 7 | +from lxml import etree, html |
| 8 | + |
| 9 | +from edgar.documents import ParserConfig, parse_html |
| 10 | +from edgar.documents.strategies.table_processing import TableProcessor |
| 11 | +from edgar.documents.table_nodes import Cell |
| 12 | +from tests.paths import FIXTURES_DIR, REPO_ROOT |
| 13 | + |
| 14 | +pytestmark = pytest.mark.fast |
| 15 | + |
| 16 | +# https://www.sec.gov/Archives/edgar/data/1341439/000095017024104905/orcl-20240831.htm |
| 17 | +ORACLE_FIXTURE = REPO_ROOT / "data/html/Oracle.10-Q.html" |
| 18 | +ORACLE_EMEA = { |
| 19 | + "3,228": ("F_b8604f4b-10f0-4509-8e6a-4b2044b63073", "3228000000"), |
| 20 | + "3,005": ("F_6c2fb952-8c5e-4ea5-a1c3-e998c3f20684", "3005000000"), |
| 21 | +} |
| 22 | +EMEA_FOOTNOTE = "FNT_101dfb5e-8641-4a4f-a4a3-578450fa445b" |
| 23 | + |
| 24 | + |
| 25 | +@pytest.fixture(scope="module") |
| 26 | +def oracle_document(): |
| 27 | + return parse_html(ORACLE_FIXTURE.read_bytes(), ParserConfig(form="10-Q")) |
| 28 | + |
| 29 | + |
| 30 | +def find_cell(table, row_label, cell_text): |
| 31 | + return next( |
| 32 | + cell |
| 33 | + for row in table.rows |
| 34 | + if row.cells[0].text() == row_label |
| 35 | + for cell in row.cells |
| 36 | + if cell.text() == cell_text |
| 37 | + ) |
| 38 | + |
| 39 | + |
| 40 | +def test_oracle_emea_cells_lead_to_their_filed_footnote(oracle_document): |
| 41 | + """Oracle Form 10-Q, filed September 10, 2024, accession 0000950170-24-104905, Note 7.""" |
| 42 | + note_table = oracle_document.tables[17] |
| 43 | + facts = {fact.fact_id: fact for fact in oracle_document.xbrl_facts} |
| 44 | + |
| 45 | + for display, (fact_id, value) in ORACLE_EMEA.items(): |
| 46 | + cell = find_cell(note_table, "EMEA(1)", display) |
| 47 | + assert cell.numeric_value == float(display.replace(",", "")) |
| 48 | + assert cell.xbrl_fact_ids == (fact_id,) |
| 49 | + |
| 50 | + fact = facts[cell.xbrl_fact_ids[0]] |
| 51 | + assert fact.value == value |
| 52 | + assert fact.footnotes == [EMEA_FOOTNOTE] |
| 53 | + assert oracle_document.xbrl_footnotes[fact.footnotes[0]].text == ( |
| 54 | + "Comprised of Europe, the Middle East and Africa" |
| 55 | + ) |
| 56 | + |
| 57 | + reverse_cell, = ( |
| 58 | + candidate |
| 59 | + for table in oracle_document.tables |
| 60 | + for row_cells in table.headers + [row.cells for row in table.rows + table.footer] |
| 61 | + for candidate in row_cells |
| 62 | + if fact_id in candidate.xbrl_fact_ids |
| 63 | + ) |
| 64 | + assert reverse_cell is cell |
| 65 | + |
| 66 | + |
| 67 | +def test_oracle_markers_and_untagged_repeat_do_not_borrow_fact_ids(oracle_document): |
| 68 | + note_table = oracle_document.tables[17] |
| 69 | + mda_table = oracle_document.tables[21] |
| 70 | + |
| 71 | + assert find_cell(note_table, "EMEA(1)", "EMEA(1)").xbrl_fact_ids == () |
| 72 | + assert find_cell(mda_table, "EMEA(1)", "EMEA(1)").xbrl_fact_ids == () |
| 73 | + for display in ORACLE_EMEA: |
| 74 | + assert find_cell(mda_table, "EMEA(1)", display).xbrl_fact_ids == () |
| 75 | + |
| 76 | + # A real fact can belong to a cell without having any filed footnote. |
| 77 | + americas = find_cell(note_table, "Americas", "8,372") |
| 78 | + assert len(americas.xbrl_fact_ids) == 1 |
| 79 | + americas_fact, = ( |
| 80 | + fact for fact in oracle_document.xbrl_facts if fact.fact_id == americas.xbrl_fact_ids[0] |
| 81 | + ) |
| 82 | + assert americas_fact.value == "8372000000" |
| 83 | + assert americas_fact.footnotes == [] |
| 84 | + |
| 85 | + |
| 86 | +@pytest.mark.parametrize("table_extraction", [True, False], ids=["advanced", "basic"]) |
| 87 | +def test_cell_keeps_its_own_inline_fact_ids_in_source_order(table_extraction): |
| 88 | + markup = """<html><body><ix:nonNumeric id="ancestor" name="t:TextBlock"> |
| 89 | + <table><tr> |
| 90 | + <td><span><ix:nonFraction id="b" name="t:Amount">7</ix:nonFraction></span> |
| 91 | + <ix:nonNumeric id="a" name="t:Label">label</ix:nonNumeric> |
| 92 | + <ix:nonFraction id="b" name="t:Other">8</ix:nonFraction> |
| 93 | + <ix:fraction id="fraction" name="t:Fraction"><ix:numerator>1</ix:numerator> |
| 94 | + <ix:denominator>2</ix:denominator></ix:fraction> |
| 95 | + <ix:nonFraction name="t:NoID">9</ix:nonFraction> |
| 96 | + <ix:nonFraction id="" name="t:EmptyID">9</ix:nonFraction> |
| 97 | + <ix:nonFraction id=" " name="t:BlankID">9</ix:nonFraction> |
| 98 | + <ix:footnote id="resource">note resource</ix:footnote> |
| 99 | + <ix:continuation id="continued">continuation</ix:continuation> |
| 100 | + <ix:hidden><ix:nonFraction id="hidden" name="t:Amount">10</ix:nonFraction></ix:hidden> |
| 101 | + <table><tr><td><ix:nonFraction id="nested" name="t:Amount">11</ix:nonFraction></td></tr></table> |
| 102 | + </td> |
| 103 | + </tr></table></ix:nonNumeric></body></html>""" |
| 104 | + doc = parse_html(markup, ParserConfig(table_extraction=table_extraction)) |
| 105 | + cell = doc.tables[0].rows[0].cells[0] |
| 106 | + |
| 107 | + # The enclosing fact and the nested cell's fact belong to different elements. |
| 108 | + assert cell.xbrl_fact_ids == ("b", "a", "b", "fraction") |
| 109 | + |
| 110 | + |
| 111 | +def test_manual_cell_keeps_existing_rendering_and_equality(): |
| 112 | + cell = Cell("3,726", xbrl_fact_ids=("fact",)) |
| 113 | + plain = Cell("3,726") |
| 114 | + |
| 115 | + assert plain.xbrl_fact_ids == () |
| 116 | + assert cell == plain |
| 117 | + assert repr(cell) == repr(plain) |
| 118 | + assert cell.text() == "3,726" |
| 119 | + assert cell.html() == "<td>3,726</td>" |
| 120 | + assert cell.numeric_value == 3726 |
| 121 | + |
| 122 | + |
| 123 | +def test_toc_section_tables_keep_distinct_facts_with_equal_displayed_values(): |
| 124 | + """NIKE Form 10-K, accession 0000320187-25-000047, Item 8.""" |
| 125 | + fixture = FIXTURES_DIR / "html/nke/10k/nke-10-k-2025-07-17.html" |
| 126 | + doc = parse_html(fixture.read_bytes(), ParserConfig(form="10-K")) |
| 127 | + section = doc.sections["part_ii_item_8"] |
| 128 | + assert section.detection_method == "toc" |
| 129 | + |
| 130 | + ids = [ |
| 131 | + cell.xbrl_fact_ids |
| 132 | + for table in section.tables() |
| 133 | + for row in table.rows |
| 134 | + if row.cells[0].text() == "Net income" |
| 135 | + for cell in row.cells |
| 136 | + if cell.text() == "3,219" |
| 137 | + ] |
| 138 | + assert ids == [("f-101",), ("f-185",), ("f-365",), ("f-366",)] |
| 139 | + |
| 140 | + |
| 141 | +def test_cell_keeps_fact_in_namespace_aware_inline_child(): |
| 142 | + table = html.fromstring("<table><tr><td>Example</td><td></td></tr></table>") |
| 143 | + value_cell = table.xpath(".//td")[1] |
| 144 | + namespace = "{http://www.xbrl.org/2013/inlineXBRL}" |
| 145 | + etree.SubElement(value_cell, namespace + "nonFraction", id="visible").text = "7" |
| 146 | + hidden = etree.SubElement(value_cell, namespace + "hidden") |
| 147 | + etree.SubElement(hidden, namespace + "nonFraction", id="hidden").text = "8" |
| 148 | + |
| 149 | + parsed = TableProcessor(ParserConfig()).process(table) |
| 150 | + assert parsed.rows[0].cells[1].xbrl_fact_ids == ("visible",) |
| 151 | + |
| 152 | + |
| 153 | +@pytest.mark.parametrize("nested_tag", ["td", "th"]) |
| 154 | +def test_basic_table_keeps_nested_cells_out_of_its_outer_grid(nested_tag): |
| 155 | + markup = f"""<table><tbody><tr> |
| 156 | + <td>Outer<ix:nonNumeric id="A" name="t:Label">a</ix:nonNumeric> |
| 157 | + <table><caption>Nested caption</caption><tr> |
| 158 | + <{nested_tag}>Inner<ix:nonNumeric id="B" name="t:Label">b</ix:nonNumeric></{nested_tag}> |
| 159 | + </tr></table> |
| 160 | + </td> |
| 161 | + <td>After<ix:nonNumeric id="C" name="t:Label">c</ix:nonNumeric></td> |
| 162 | + </tr></tbody></table>""" |
| 163 | + doc = parse_html(markup, ParserConfig(table_extraction=False)) |
| 164 | + outer, = doc.tables |
| 165 | + |
| 166 | + rows = [[cell.xbrl_fact_ids for cell in row.cells] for row in outer.rows] |
| 167 | + reverse_matches = [ |
| 168 | + cell for cells in [*outer.headers, *(row.cells for row in outer.rows)] |
| 169 | + for cell in cells if "B" in cell.xbrl_fact_ids |
| 170 | + ] |
| 171 | + assert (rows, len(reverse_matches)) == ([[("A",), ("C",)]], 0) |
| 172 | + assert not outer.headers |
| 173 | + assert outer.caption is None |
| 174 | + assert [" ".join(cell.text().split()) for cell in outer.rows[0].cells] == [ |
| 175 | + "Outera Nested caption Innerb", "Afterc", |
| 176 | + ] |
| 177 | + assert [fact.fact_id for fact in doc.xbrl_facts] == ["A", "B", "C"] |
| 178 | + |
| 179 | + |
| 180 | +@pytest.mark.parametrize( |
| 181 | + ("table_extraction", "expected_header", "rendered_header"), |
| 182 | + [ |
| 183 | + (False, [("Period", ("period",)), ("Label", ("label",))], |
| 184 | + "<tr><td>Period</td><th>Label</th></tr>"), |
| 185 | + (True, [("Label", ("label",)), ("Period", ("period",))], |
| 186 | + "<tr><th>Label</th><th>Period</th></tr>"), |
| 187 | + ], |
| 188 | + ids=["basic", "advanced"], |
| 189 | +) |
| 190 | +def test_table_preserves_mixed_header_fact_ownership( |
| 191 | + table_extraction, expected_header, rendered_header |
| 192 | +): |
| 193 | + markup = """<table><caption>Metrics</caption> |
| 194 | + <thead><tr> |
| 195 | + <th><ix:nonNumeric id="label" name="t:Label">Label</ix:nonNumeric></th> |
| 196 | + <td><ix:nonNumeric id="period" name="t:Period">Period</ix:nonNumeric></td> |
| 197 | + </tr></thead> |
| 198 | + <tbody><tr><td>Example</td><td>42</td></tr></tbody> |
| 199 | + </table>""" |
| 200 | + doc = parse_html(markup, ParserConfig(table_extraction=table_extraction)) |
| 201 | + table, = doc.tables |
| 202 | + header, = table.headers |
| 203 | + |
| 204 | + assert table.caption == "Metrics" |
| 205 | + assert [(cell.text(), cell.xbrl_fact_ids) for cell in header] == expected_header |
| 206 | + assert table.rows[0].cells[1].numeric_value == 42 |
| 207 | + assert rendered_header in table.html() |
| 208 | + assert "<tr><td>Example</td><td>42</td></tr>" in table.html() |
0 commit comments