Repository Public
1# =============================================================================2# SP BOUCHER | SOFTWARE & RESEARCH3# ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4# =============================================================================5#6# Project : SPIDER7# File : tests/unit/extract/test_extract_html.py8# Module : Tests9# Repository : https://git.spboucher.ai/spider10# Project Website : Not specified11# Created : 2026-12-0412# Last Modified : 2026-12-0413#14# Author : Simon-Pierre Boucher15# Position : Professor of Finance16# Institution : Université du Québec en Outaouais (UQO)17# Organization : SP Boucher18# Website : https://spboucher.ai19# Git : https://git.spboucher.ai20#21# Preferred Contact: contact@spboucher.ai22# Development : dev@spboucher.ai23# Support : support@spboucher.ai24# Security/Admin : admin@spboucher.ai25#26# Copyright (c) 202627# Simon-Pierre Boucher28# All rights reserved where applicable. This file is governed by the29# applicable project license, copyright notices and relevant law.30# Existing third-party rights and notices remain unaffected.31#32# License : ISC33#34# Do not embed credentials, private keys, access tokens,35# certificates or confidential personal information.36#37# =============================================================================38# END OF SP BOUCHER SOURCE HEADER39# =============================================================================4041"""HTML document extraction."""42from spider.extract.html import extract_html4344PAGE = """<!doctype html><html lang="fr-CA"><head><title> Appartement 4½ – Exemple </title>45<base href="https://ex.com/fr/"><link rel="canonical" href="/fr/annonce/42">46<link rel="alternate" hreflang="en" href="/en/listing/42"><link rel="next" href="?page=2">47<meta name="Description" content="Bel appartement"><meta property="og:title" content="OG">48<style>.x{color:red}</style></head><body>49<header><nav><a href="/">Accueil</a><a href="/louer" rel="nofollow">Louer</a>50<a href="javascript:void(0)">JS</a><a href="mailto:a@b.c">Mail</a><a href="tel:+1819">Tel</a>51<a href="data:text/html,x">Data</a><a href="#top">Top</a></nav></header>52<main><h1>Appartement <em>4½</em> à louer</h1><h2 hidden>Caché</h2>53<p>Grand logement près du <a href="../metro?x=1">métro</a>.<br>Deuxième ligne.</p>54<ul><li>Un</li><li>Deux</li></ul><ol><li>Premier</li></ol>55<div style="display:none">invisible</div><script>var secret = 1;</script>56<a href="/img.jpg"><img src="/t.jpg" alt="Photo du salon"></a>57<map><area href="/zone" alt="Zone"></map><iframe src="//player.example/v" title="Vidéo"></iframe>58</main>59<ul class="pagination"><li><a href="?page=2">2</a></li></ul>60<form action="/recherche"><input name="q"></form>61<form action="/contact" method="post"><input name="m"></form>62<form action="/req"><input name="q" required></form>63<footer><a href="/legal">Mentions</a></footer></body></html>"""646566def _doc():67 return extract_html(PAGE, "https://ex.com/start")686970def test_title_lang_meta_canonical_hreflang():71 doc = _doc()72 assert doc.title == "Appartement 4½ – Exemple"73 assert doc.lang == "fr-CA"74 assert doc.meta["description"] == "Bel appartement"75 assert doc.meta["og:title"] == "OG"76 assert doc.canonical == "https://ex.com/fr/annonce/42"77 assert [(a.hreflang, a.url) for a in doc.hreflang] == [("en", "https://ex.com/en/listing/42")]78 assert doc.base_url == "https://ex.com/fr/"798081def test_headings_and_lists():82 doc = _doc()83 assert [(h.level, h.text) for h in doc.headings] == [(1, "Appartement 4½ à louer")]84 assert [(lst.ordered, lst.items) for lst in doc.lists][:2] == [(False, ["Un", "Deux"]), (True, ["Premier"])]858687def test_text_raw_and_normalized():88 doc = _doc()89 assert "invisible" not in doc.text and "secret" not in doc.text and "Caché" not in doc.text90 assert "color:red" not in doc.text_raw91 assert "Deuxième ligne." in doc.text # NBSP collapsed in the normalized text92 assert "Deuxième ligne." in doc.text_raw # raw text keeps the original characters93 assert "Grand logement près du métro.\nDeuxième ligne." in doc.text949596def test_link_kinds_and_resolution():97 links = {(link.kind, link.url): link for link in _doc().links}98 assert ("nav", "https://ex.com/") in links99 assert links[("nav", "https://ex.com/louer")].rel == "nofollow"100 assert ("anchor", "https://ex.com/metro?x=1") in links # resolved against <base href>101 assert ("canonical", "https://ex.com/fr/annonce/42") in links102 assert links[("hreflang", "https://ex.com/en/listing/42")].text == "en"103 assert ("pagination", "https://ex.com/fr/?page=2") in links104 assert ("iframe", "https://player.example/v") in links105 assert links[("area", "https://ex.com/zone")].text == "Zone"106 assert ("form_get", "https://ex.com/recherche") in links107 assert ("nav", "https://ex.com/legal") in links108 assert links[("anchor", "https://ex.com/img.jpg")].text == "Photo du salon" # falls back to the image alt109110111def test_skipped_links():112 urls = [link.url for link in _doc().links]113 assert not any(u.startswith(("javascript:", "mailto:", "tel:", "data:")) for u in urls)114 assert "https://ex.com/contact" not in urls # POST form115 assert "https://ex.com/req" not in urls # GET form with a required input116 assert not any(u.endswith("#top") for u in urls)117118119def test_pagination_container_and_rel():120 kinds = [link.kind for link in _doc().links if link.url == "https://ex.com/fr/?page=2"]121 assert kinds and set(kinds) == {"pagination"}122123124def test_broken_and_empty_inputs():125 assert extract_html("", "https://ex.com/").text == ""126 assert extract_html("<<<>>><p>ok<b>bold<p>next", "https://ex.com/").text.endswith("okbold\nnext")127 doc = extract_html("<?xml version='1.0' encoding='utf-8'?><html><body><p>x</p></body></html>", "https://e.com/")128 assert doc.text == "x"129 doc = extract_html("<a href='http://[bad'>x</a><a href=' /ok\n'>y</a>", "https://e.com/")130 assert [link.url for link in doc.links] == ["https://e.com/ok"]131132133def test_deep_nesting_and_large_input_do_not_crash():134 # libxml2 stops building below 255 nested elements: the result is partial, never an exception.135 deep = "<p>avant</p>" + "<div>" * 6000 + "fond" + "</div>" * 6000136 assert "avant" in extract_html(deep, "https://e.com/").text137 shallow = "<div>" * 200 + "fond" + "</div>" * 200138 assert extract_html(shallow, "https://e.com/").text == "fond"139 big = "<p>" + ("mot " * 200_000) + "</p>"140 assert len(extract_html(big, "https://e.com/").text) > 100_000141