"""Tests for turnstone.core.web — strip_html.""" from turnstone.core.web import strip_html class TestStripHtml: def test_removes_tags(self): assert strip_html("hello") == "hello" def test_removes_nested_tags(self): assert strip_html("

text

") == "text" def test_decodes_entities(self): assert strip_html("& < >") == "& < >" def test_collapses_whitespace(self): result = strip_html("hello world") assert result == "hello world" def test_collapses_blank_lines(self): result = strip_html("a\n\n\n\n\nb") assert result == "a\n\nb" def test_empty_string(self): assert strip_html("") == "" def test_strips_leading_trailing_whitespace(self): assert strip_html(" hello ") == "hello" def test_complex_html(self): html = "

Title

Some & text

" result = strip_html(html) assert "Title" in result assert "Some & text" in result assert "<" not in result def test_br_becomes_newline(self): #
is a line break, not a no-op: text must not glue together. assert strip_html("hello
world") == "hello\nworld" assert strip_html("hello
world") == "hello\nworld" # -- invisible element stripping ----------------------------------------- def test_strips_script_content(self): html = "

before

after

" result = strip_html(html) assert "var x" not in result assert "before" in result assert "after" in result def test_strips_style_content(self): html = "

visible

" result = strip_html(html) assert "color" not in result assert "visible" in result def test_strips_template_content(self): html = "

shown

" result = strip_html(html) assert "hidden" not in result assert "shown" in result def test_strips_noscript_content(self): html = "

content

" result = strip_html(html) assert "Enable JS" not in result assert "content" in result def test_strips_multiple_script_blocks(self): html = "

middle

" result = strip_html(html) assert "a()" not in result assert "b()" not in result assert "middle" in result def test_strips_multiline_script(self): html = "

ok

" result = strip_html(html) assert "function" not in result assert "ok" in result def test_strips_script_case_insensitive(self): html = "

text

" result = strip_html(html) assert "code()" not in result assert "text" in result def test_strips_script_with_attributes(self): html = '

done

' result = strip_html(html) assert "init()" not in result assert "done" in result class TestStripHtmlBlockStructure: """Block-level boundaries become line breaks instead of gluing text together.""" def test_paragraphs_separated(self): assert strip_html("

a

b

") == "a\n\nb" def test_heading_not_glued_to_body(self): result = strip_html("

Title

Body text

") assert "TitleBody" not in result assert result == "Title\n\nBody text" def test_list_items_separated(self): result = strip_html("") assert "firstsecond" not in result lines = [ln for ln in result.splitlines() if ln.strip()] assert lines == ["first", "second"] def test_table_cells_not_glued(self): # The motivating case: digits in adjacent cells must not run together. result = strip_html("123456") assert "123456" not in result assert "123" in result assert "456" in result def test_divs_separated(self): result = strip_html("
one
two
") assert "onetwo" not in result def test_inline_tags_still_join_without_breaks(self): # Inline elements carry no block boundary and must not introduce newlines. assert strip_html("foobar") == "foobar" assert strip_html("abc") == "abc" def test_block_tags_with_attributes(self): result = strip_html('

a

b

') assert result == "a\n\nb" def test_no_false_match_on_similar_tag_names(self): # / are not newline tags;

is. Name lookup is exact, # so lookalike prefixes must not introduce breaks. assert strip_html("img") == "img" assert strip_html("x") == "x" def test_uppercase_tags_break(self): # Tag-name matching lowercases; real-world HTML often uses uppercase tags. assert strip_html("

a

b

") == "a\n\nb" assert strip_html("a
b") == "a\nb" def test_br_with_attributes_breaks(self): # A
carrying attributes must still produce a line break, not glue. assert strip_html("one
two") == "one\ntwo" def test_pathological_whitespace_is_linear(self): # A '<' (or '