From 6165f97a259b8fde633788f9c96ca20712016e5d Mon Sep 17 00:00:00 2001 From: Yasser-Ameur Date: Wed, 9 Sep 2026 02:28:47 +0200 Subject: [PATCH] fix(scraping): run post-processing on the selected content, not body With css_selector or target_elements set, cleaned_html kept under only_text, emitted inline style, onclick and data-* attributes, empty elements and whole base64 image payloads, and let a + + + + + +""" + +COMMON = dict(url="raw://test", html=SAMPLE_HTML, only_text=True) + +SELECTORS = [ + pytest.param({}, id="no-selector"), + pytest.param({"css_selector": ".job"}, id="css_selector"), + pytest.param({"target_elements": [".job"]}, id="target_elements"), + pytest.param({"css_selector": "body", "target_elements": [".job"]}, id="both"), +] + + +@pytest.fixture +def scraper(): + return LXMLWebScrapingStrategy() + + +@pytest.mark.parametrize("selector", SELECTORS) +class TestPostProcessingRunsWithSelector: + def test_only_text_unwraps_inline_tags(self, scraper, selector): + """only_text should unwrap / whether or not a selector is set.""" + cleaned = scraper._scrap(**COMMON, **selector)["cleaned_html"] + assert "" not in cleaned + assert "" not in cleaned + assert "Postgres" in cleaned + assert "Python" in cleaned + + def test_base64_image_src_is_truncated(self, scraper, selector): + """The base64 payload should never reach cleaned_html.""" + cleaned = scraper._scrap(**COMMON, **selector)["cleaned_html"] + assert "base64" not in cleaned + assert 'src=""' in cleaned + + def test_empty_elements_are_removed(self, scraper, selector): + """The empty
should be dropped.""" + cleaned = scraper._scrap(**COMMON, **selector)["cleaned_html"] + assert "tracker" not in cleaned + + def test_unwanted_attributes_are_stripped(self, scraper, selector): + """style/onclick/data-* go, class/href stay.""" + cleaned = scraper._scrap(**COMMON, **selector)["cleaned_html"] + assert "style=" not in cleaned + assert "onclick=" not in cleaned + assert "data-tracking" not in cleaned + assert 'class="job"' in cleaned + assert 'href="/apply"' in cleaned + + def test_style_script_noscript_are_removed(self, scraper, selector): + """style/script/noscript inside the selection must not survive.""" + cleaned = scraper._scrap(**COMMON, **selector)["cleaned_html"] + assert "