Spaces:
Running
Running
File size: 5,785 Bytes
51aa28e 1086400 51aa28e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | """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 = ('<p><math class="wrs_chemistry" xmlns="http://www.w3.org/1998/Math/MathML">'
'<mstyle mathsize="16px"><mi>CO</mi></mstyle></math></p>')
out = ppt_api.normalize_cms_html(html)
assert 'mathsize' not in out
assert 'class="wrs_chemistry"' in out and '<mi>CO</mi>' in out
def test_inline_font_size_dropped_other_styles_kept():
out = ppt_api.normalize_cms_html('<span style="font-size:14px;color:red">x</span>')
assert 'font-size' not in out and 'color:red' in out
assert ppt_api.normalize_cms_html('<span style="font-size:14px">x</span>') == '<span>x</span>'
def test_empty_paragraphs_and_table_rows_removed():
html = ('<p>Q</p><p> </p><p> <br> </p>'
'<figure class="table" style="width:400px;"><table width="400"><tbody>'
'<tr><td>A)</td><td>x</td></tr><tr><td> </td><td> </td></tr>'
'</tbody></table></figure>')
out = ppt_api.normalize_cms_html(html)
assert out.count('<p') == 1
assert out.count('<tr') == 1
assert 'width' not in out
def test_white_backed_image_is_scaled_and_made_transparent():
img = Image.new('RGB', (90, 36), 'white')
img.paste((0, 0, 0), (20, 10, 70, 26)) # black "ink"
out = ppt_api.normalize_cms_html(f'<img src="{_png_uri(img)}">')
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'<img src="{uri}" width="200">')
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'<img src="{_png_uri(Image.new("RGB", (900, 50), "navy"))}">')
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 = '<span style="font-family:"Aptos",sans-serif;">x</span>'
assert ppt_api.normalize_cms_html(html) == '<span>x</span>'
out = ppt_api.normalize_cms_html('<span style="font-family:Arial;color:red">x</span>')
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(
'<span style="font-size:9px;quotes:"a"">x</span>')
assert out == '<span style="quotes:"a"">x</span>'
def test_trailing_line_break_in_an_option_is_dropped():
"""Seen live (ILQ-2465317): "20 J <br> " drew a blank line under
that option only, so option gaps looked random."""
assert ppt_api.normalize_cms_html('<p>20 J <br> </p>') == '<p>20 J</p>'
assert ppt_api.normalize_cms_html('<p>1000 J<br> </p>') == '<p>1000 J</p>'
assert ppt_api.normalize_cms_html('<p><br> x</p>') == '<p>x</p>'
assert ppt_api.normalize_cms_html('A <br>') == 'A'
def test_line_breaks_between_text_are_kept():
out = ppt_api.normalize_cms_html('<p>Statement I<br>Statement II</p>')
assert out == '<p>Statement I<br>Statement II</p>'
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 src="{_png_uri(img)}">', 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 src="{_png_uri(img)}">', 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': '<p>Q</p>',
'answer_options': [f'<p><img src="{img}"></p>'] * 4}}
text = {'question': {'question_text': '<p>Q</p>',
'answer_options': ['<p>a</p>', '<p>b</p>', '<p>c</p>', '<p>d</p>']}}
assert '<div class="ogrid">' in ppt_api.build_html(pics)
assert '<div class="ogrid">' not in ppt_api.build_html(text)
def test_build_html_normalises_stem_and_options():
data = {'question': {
'question_text': '<p>Q<math><mstyle mathsize="12px"><mi>x</mi></mstyle></math></p><p> </p>',
'answer_options': ['<p><math><mstyle mathsize="14px"><mn>1</mn></mstyle></math></p>', '<p>2</p>']}}
html = ppt_api.build_html(data)
assert 'mathsize' not in html and ' ' not in html
|