From f3e821eeab61d15795aed6ecf73edf34615afd64 Mon Sep 17 00:00:00 2001 From: c-tonneslan Date: Fri, 10 Jul 2026 08:09:12 -0400 Subject: [PATCH] Match direct-child
and in _stringify Signed-off-by: c-tonneslan --- legistar/base.py | 4 ++-- tests/test_parsing.py | 11 +++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/legistar/base.py b/legistar/base.py index 5e6327c..8516794 100644 --- a/legistar/base.py +++ b/legistar/base.py @@ -232,9 +232,9 @@ def _parse_detail(self, key, field_1, field_2): return None def _stringify(self, field): - for br in field.xpath("*//br"): + for br in field.xpath(".//br"): br.tail = "\n" + br.tail if br.tail else "\n" - for em in field.xpath("*//em"): + for em in field.xpath(".//em"): if em.text: em.text = "--em--" + em.text + "--em--" return field.text_content().replace(' ', ' ').strip() diff --git a/tests/test_parsing.py b/tests/test_parsing.py index 167c0c9..289670e 100644 --- a/tests/test_parsing.py +++ b/tests/test_parsing.py @@ -1,13 +1,24 @@ import os import lxml +import lxml.html import pytest +from legistar.base import LegistarScraper from legistar.bills import LegistarBillScraper from legistar.events import LegistarEventsScraper from legistar.people import LegistarPersonScraper +@pytest.mark.parametrize('markup, expected', [ + ('Line1
Line2', 'Line1\nLine2'), #
is a direct child of the cell + ('Line1
Line2
', 'Line1\nLine2'), #
nested in a child element +]) +def test_stringify_br_becomes_newline(markup, expected): + scraper = LegistarScraper() + assert scraper._stringify(lxml.html.fromstring(markup)) == expected + + @pytest.mark.parametrize('jurisdiction', ['chicago', 'metro', 'nyc']) def test_parse_bills(project_directory, jurisdiction): bills_fixture = os.path.join(project_directory, 'tests', 'fixtures', jurisdiction, 'bills.html')