"""Offline ASP.NET WebForms verifier for Chapter 50 (repeater)."""
from __future__ import annotations
import html
import json
import re
import xml.etree.ElementTree as ET
VERSION = "CPython 3.12.14 offline ASP.NET WebForms verifier (IIS/ASP.NET runtime absent)"
CDCATALOG_XML = """<?xml version="1.0" encoding="ISO-8859-1"?>
<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
<cd>
<title>Hide your heart</title>
<artist>Bonnie Tyler</artist>
<country>UK</country>
<company>CBS Records</company>
<price>9.90</price>
<year>1988</year>
</cd>
<cd>
<title>Greatest Hits</title>
<artist>Dolly Parton</artist>
<country>USA</country>
<company>RCA</company>
<price>9.90</price>
<year>1982</year>
</cd>
<cd>
<title>Still got the blues</title>
<artist>Gary Moore</artist>
<country>UK</country>
<company>Virgin records</company>
<price>10.20</price>
<year>1990</year>
</cd>
<cd>
<title>Eros</title>
<artist>Eros Ramazzotti</artist>
<country>EU</country>
<company>BMG</company>
<price>9.90</price>
<year>1997</year>
</cd>
</catalog>
"""
BIND_RE = re.compile(r'<%#\s*Container\.DataItem\("([^"]+)"\)\s*%>')
def load_cd_catalog(xml_text: str) -> list[dict[str, str]]:
root = ET.fromstring(xml_text)
return [
{child.tag: (child.text or "").strip() for child in cd}
for cd in root.findall("cd")
]
def bind_template(template: str, row: dict[str, str]) -> str:
return BIND_RE.sub(lambda m: html.escape(row[m.group(1)]), template)
def render_repeater(
rows: list[dict[str, str]],
*,
header: str,
item: str,
footer: str,
alternating_item: str | None = None,
separator: str | None = None,
) -> str:
parts = [header]
for idx, row in enumerate(rows):
if idx > 0 and separator is not None:
parts.append(separator)
tpl = alternating_item if (alternating_item is not None and idx % 2 == 1) else item
parts.append(bind_template(tpl, row))
parts.append(footer)
return "".join(parts)
def main() -> None:
rows = load_cd_catalog(CDCATALOG_XML)
header_tpl = (
'<table border="1" width="100%">'
"<tr><th>Title</th><th>Artist</th><th>Country</th><th>Company</th><th>Price</th><th>Year</th></tr>"
)
item_tpl = (
"<tr>"
'<td><%#Container.DataItem("title")%></td>'
'<td><%#Container.DataItem("artist")%></td>'
'<td><%#Container.DataItem("country")%></td>'
'<td><%#Container.DataItem("company")%></td>'
'<td><%#Container.DataItem("price")%></td>'
'<td><%#Container.DataItem("year")%></td>'
"</tr>"
)
alt_tpl = item_tpl.replace("<tr>", '<tr bgcolor="#e8e8e8">', 1)
sep_tpl = '<tr><td colspan="6"><hr /></td></tr>'
footer_tpl = "</table>"
basic_html = render_repeater(rows, header=header_tpl, item=item_tpl, footer=footer_tpl)
alt_html = render_repeater(
rows, header=header_tpl, item=item_tpl, alternating_item=alt_tpl, footer=footer_tpl
)
sep_html = render_repeater(
rows,
header=header_tpl.replace('border="1"', 'border="0"'),
item=item_tpl,
separator=sep_tpl,
footer=footer_tpl,
)
cases = [
{
"name": "parse_cdcatalog_xml_dataset",
"summary": "解析 cdcatalog.xml 获取 5 张唱片记录与 6 个标准字段",
"expected": {
"count": 5,
"fields": ["title", "artist", "country", "company", "price", "year"],
"first_title": "Empire Burlesque",
"last_artist": "Eros Ramazzotti",
},
"actual": {
"count": len(rows),
"fields": list(rows[0].keys()),
"first_title": rows[0]["title"],
"last_artist": rows[-1]["artist"],
},
"passed": (
len(rows) == 5
and list(rows[0].keys()) == ["title", "artist", "country", "company", "price", "year"]
and rows[0]["title"] == "Empire Burlesque"
and rows[-1]["artist"] == "Eros Ramazzotti"
),
},
{
"name": "repeater_basic_table_render",
"summary": "验证 Repeater 的 HeaderTemplate(1次)、ItemTemplate(5次) 与 FooterTemplate(1次) 完整渲染",
"expected": {
"table_open_count": 1,
"table_close_count": 1,
"tr_count": 6,
"contains_dylan": True,
"has_unrendered_binding": False,
},
"actual": {
"table_open_count": basic_html.count("<table "),
"table_close_count": basic_html.count("</table>"),
"tr_count": basic_html.count("<tr>"),
"contains_dylan": "<td>Bob Dylan</td>" in basic_html,
"has_unrendered_binding": "<%#" in basic_html,
},
"passed": (
basic_html.count("<table ") == 1
and basic_html.count("</table>") == 1
and basic_html.count("<tr>") == 6
and "<td>Bob Dylan</td>" in basic_html
and "<%#" not in basic_html
),
},
{
"name": "repeater_alternating_item_template",
"summary": "验证 AlternatingItemTemplate 为第 2、4 条记录(奇数索引)应用浅灰背景 #e8e8e8",
"expected": {
"alternating_row_count": 2,
"bonnie_is_alt": True,
"gary_is_alt": True,
},
"actual": {
"alternating_row_count": alt_html.count('<tr bgcolor="#e8e8e8">'),
"bonnie_is_alt": '<tr bgcolor="#e8e8e8"><td>Hide your heart</td>' in alt_html,
"gary_is_alt": '<tr bgcolor="#e8e8e8"><td>Still got the blues</td>' in alt_html,
},
"passed": (
alt_html.count('<tr bgcolor="#e8e8e8">') == 2
and '<tr bgcolor="#e8e8e8"><td>Hide your heart</td>' in alt_html
and '<tr bgcolor="#e8e8e8"><td>Still got the blues</td>' in alt_html
),
},
{
"name": "repeater_separator_template",
"summary": "验证 SeparatorTemplate 在 5 条记录之间恰好插入 4 条水平线分隔行(首前末后不插入)",
"expected": {
"separator_count": 4,
"ends_with_last_row_before_table_close": True,
},
"actual": {
"separator_count": sep_html.count(sep_tpl),
"ends_with_last_row_before_table_close": sep_html.endswith("<td>1997</td></tr></table>"),
},
"passed": (
sep_html.count(sep_tpl) == 4
and sep_html.endswith("<td>1997</td></tr></table>")
),
},
]
print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
本例需在 Windows x64、CPython 3.12.14 离线 ASP.NET WebForms 语义验证器(本机未启动 IIS / ASP.NET WebForms 运行时宿主)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14 离线 ASP.NET WebForms 语义验证器(本机未启动 IIS / ASP.NET WebForms 运行时宿主)。
入口文件:projects/mvc-aspnet-repeater-lab/explore-aspnet-repeater.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-aspnet-repeater.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14 离线 ASP.NET WebForms 语义验证器(本机未启动 IIS / ASP.NET WebForms 运行时宿主)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2864.zip
课程关联工程包(登记关联55章) · 含当前章节或实例 · mbskycom202612a2870.zip