Coverage for tdom / escaping_test.py: 100%

25 statements  

« prev     ^ index     » next       coverage.py v7.13.0, created at 2025-12-17 23:32 +0000

1from .escaping import escape_html_comment, escape_html_script, escape_html_style 

2 

3 

4def test_escape_html_comment_empty() -> None: 

5 assert escape_html_comment("") == "" 

6 

7 

8def test_escape_html_comment_no_special() -> None: 

9 assert escape_html_comment("This is a comment.") == "This is a comment." 

10 

11 

12def test_escape_html_comment_starts_with_gt() -> None: 

13 assert escape_html_comment(">This is a comment.") == ">This is a comment." 

14 

15 

16def test_escape_html_comment_starts_with_dash_gt() -> None: 

17 assert escape_html_comment("->This is a comment.") == "->This is a comment." 

18 

19 

20def test_escape_html_comment_contains_special_strings() -> None: 

21 input_text = "This is <!-- a comment --> with --!> special strings." 

22 expected_output = "This is &lt;!-- a comment --&gt; with --!&gt; special strings." 

23 assert escape_html_comment(input_text) == expected_output 

24 

25 

26def test_escape_html_comment_ends_with_lt_dash() -> None: 

27 assert escape_html_comment("This is a comment<!-") == "This is a comment&lt;!-" 

28 

29 

30def test_escape_html_style() -> None: 

31 input_text = "body { color: red; }</style> p { font-SIZE: 12px; }</STYLE>" 

32 expected_output = ( 

33 "body { color: red; }&lt;/style> p { font-SIZE: 12px; }&lt;/STYLE>" 

34 ) 

35 assert escape_html_style(input_text) == expected_output 

36 

37 

38def test_escape_html_script() -> None: 

39 input_text = "<!-- <script>var a = 1;</script> </SCRIPT>" 

40 expected_output = "\\x3c!-- \\x3cscript>var a = 1;\\x3c/script> \\x3c/SCRIPT>" 

41 assert escape_html_script(input_text) == expected_output 

42 # Smoketest that escaping is working and we are not just escaping back to the same value. 

43 for text in ("<script", "</script", "<!--"): 

44 assert escape_html_script(text) != text