"""Quiz PPT: CMS HTML clean-up before rendering (ppt_api.normalize_cms_html). CMS content is authored at editor size (~14-16px) and some of it hard-codes that size — MathType/ChemType mathsize, inline font-size, fixed table widths, small pasted bitmaps — which came out tiny or cramped on the 30px slide card.""" import base64 import io import re from PIL import Image import ppt_api def _png_uri(img): buf = io.BytesIO() img.save(buf, 'PNG') return 'data:image/png;base64,' + base64.b64encode(buf.getvalue()).decode() def _decode(tag): src = re.search(r'src="data:image/png;base64,([^"]+)"', tag).group(1) return Image.open(io.BytesIO(base64.b64decode(src))).convert('RGBA') def test_hardcoded_math_size_is_removed_chemtype_included(): html = ('

' 'CO

') out = ppt_api.normalize_cms_html(html) assert 'mathsize' not in out assert 'class="wrs_chemistry"' in out and 'CO' in out def test_inline_font_size_dropped_other_styles_kept(): out = ppt_api.normalize_cms_html('x') assert 'font-size' not in out and 'color:red' in out assert ppt_api.normalize_cms_html('x') == 'x' def test_empty_paragraphs_and_table_rows_removed(): html = ('

Q

 


' '
' '' '
A)x
  
') out = ppt_api.normalize_cms_html(html) assert out.count('') assert 'style="width:180px"' in out # 2x the editor size px = _decode(out) assert px.getpixel((1, 1))[3] == 0 # background cleared assert px.getpixel((40, 15))[3] == 255 # ink kept def test_transparent_image_left_as_is_but_scaled(): img = Image.new('RGBA', (260, 147), (0, 0, 0, 0)) uri = _png_uri(img) out = ppt_api.normalize_cms_html(f'') assert uri in out # not re-encoded assert 'style="width:400px"' in out # editor width wins assert 'width="200"' not in out def test_large_image_capped(): out = ppt_api.normalize_cms_html( f'') assert f'style="width:{ppt_api.IMG_MAX_W}px"' in out def test_pasted_font_family_is_dropped_so_one_typeface_is_used(): """Seen live (ILQ-2475149): some lines carried font-family:"Aptos", others none, so one question mixed two typefaces.""" html = 'x' assert ppt_api.normalize_cms_html(html) == 'x' out = ppt_api.normalize_cms_html('x') assert 'font-family' not in out and 'color:red' in out # An entity in a property that is KEPT survives intact. out = ppt_api.normalize_cms_html( 'x') assert out == 'x' def test_trailing_line_break_in_an_option_is_dropped(): """Seen live (ILQ-2465317): "20 J 
 " drew a blank line under that option only, so option gaps looked random.""" assert ppt_api.normalize_cms_html('

20 J 
 

') == '

20 J

' assert ppt_api.normalize_cms_html('

1000 J
 

') == '

1000 J

' assert ppt_api.normalize_cms_html('


  x

') == '

x

' assert ppt_api.normalize_cms_html('A 
') == 'A' def test_line_breaks_between_text_are_kept(): out = ppt_api.normalize_cms_html('

Statement I
Statement II

') assert out == '

Statement I
Statement II

' def test_tall_image_height_is_capped_keeping_aspect(): img = Image.new('RGBA', (100, 300), (0, 0, 0, 0)) # 1:3 portrait out = ppt_api.normalize_cms_html(f'', img_max_h=210) assert 'style="width:70px"' in out # 210 tall, aspect kept def test_wide_image_is_not_shrunk_by_the_height_cap(): img = Image.new('RGBA', (300, 60), (0, 0, 0, 0)) out = ppt_api.normalize_cms_html(f'', img_max_h=210) assert 'style="width:600px"' in out def test_picture_options_go_two_per_row_text_options_do_not(): img = _png_uri(Image.new('RGBA', (40, 40), (0, 0, 0, 0))) pics = {'question': {'question_text': '

Q

', 'answer_options': [f'

'] * 4}} text = {'question': {'question_text': '

Q

', 'answer_options': ['

a

', '

b

', '

c

', '

d

']}} assert '
' in ppt_api.build_html(pics) assert '
' not in ppt_api.build_html(text) def test_build_html_normalises_stem_and_options(): data = {'question': { 'question_text': '

Qx

 

', 'answer_options': ['

1

', '

2

']}} html = ppt_api.build_html(data) assert 'mathsize' not in html and ' ' not in html