"""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 = ('
') out = ppt_api.normalize_cms_html(html) assert 'mathsize' not in out assert 'class="wrs_chemistry"' in out and 'Q
| A) | x |
')
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('AStatement I
Statement II
Statement I
Statement II
Q
', 'answer_options': [f'Q
', 'answer_options': ['a
', 'b
', 'c
', 'd
']}} assert 'Q
', 'answer_options': ['', '
2
']}} html = ppt_api.build_html(data) assert 'mathsize' not in html and ' ' not in html