"""Offline ASP.NET WebForms verifier for Chapter 43 (textbox)."""
from __future__ import annotations
import html
import json
VERSION = "CPython 3.12.14 offline ASP.NET WebForms verifier (IIS/ASP.NET runtime absent)"
def render_textbox(
ctrl_id: str,
*,
text: str = "",
text_mode: str = "SingleLine",
rows: int | None = None,
columns: int | None = None,
auto_postback: bool = False,
) -> str:
mode = text_mode.lower()
attrs = [f'name="{html.escape(ctrl_id, quote=True)}"', f'id="{html.escape(ctrl_id, quote=True)}"']
if auto_postback:
attrs.append(f"onchange=\"__doPostBack('{html.escape(ctrl_id, quote=True)}','')\"")
if mode == "multiline":
if rows is not None:
attrs.append(f'rows="{rows}"')
if columns is not None:
attrs.append(f'cols="{columns}"')
return f"<textarea {' '.join(attrs)}>{html.escape(text)}</textarea>"
input_type = "password" if mode == "password" else "text"
attrs.insert(0, f'type="{input_type}"')
# WebForms Password TextBox does not echo plain-text password value by default
if mode != "password" and text:
attrs.append(f'value="{html.escape(text, quote=True)}"')
if columns is not None:
attrs.append(f'size="{columns}"')
return f"<input {' '.join(attrs)} />"
def handle_text_changed(previous_text: str, posted_text: str) -> dict[str, object]:
changed = previous_text != posted_text
lbl1 = f"You changed text to {posted_text}" if changed else ""
return {"event_fired": changed, "lbl1": lbl1}
def main() -> None:
tb1 = render_textbox("tb1")
tb2 = render_textbox("tb2", text="secret", text_mode="Password")
tb4 = render_textbox("tb4", text="Hello World!")
tb3 = render_textbox("tb3", text_mode="MultiLine")
tb6 = render_textbox("tb6", text_mode="MultiLine", rows=5)
tb5 = render_textbox("tb5", columns=30)
# Case 2: Button submit updates Label
submitted_name = "Linus"
submit_lbl = f"Your name is {submitted_name}"
# Case 3: AutoPostBack + OnTextChanged
tb_auto = render_textbox("txt1", text="Hello World!", auto_postback=True)
change_fired = handle_text_changed("Hello World!", "ASP.NET")
change_unchanged = handle_text_changed("Hello World!", "Hello World!")
# Case 4: HTML escaping in TextBox and Textarea
xss_single = render_textbox("tbXss", text='" onclick="alert(1)')
xss_multi = render_textbox("tbMultiXss", text="</textarea><script>x()</script>", text_mode="MultiLine")
cases = [
{
"name": "textbox_modes_and_dimensions",
"summary": "验证 TextBox 六种属性组合(默认、Password 不回显明文、预置 Text、MultiLine、Rows=5、Columns=30)",
"expected": {
"tb1": '<input type="text" name="tb1" id="tb1" />',
"tb2": '<input type="password" name="tb2" id="tb2" />',
"tb4": '<input type="text" name="tb4" id="tb4" value="Hello World!" />',
"tb3": '<textarea name="tb3" id="tb3"></textarea>',
"tb6": '<textarea name="tb6" id="tb6" rows="5"></textarea>',
"tb5": '<input type="text" name="tb5" id="tb5" size="30" />',
},
"actual": {
"tb1": tb1,
"tb2": tb2,
"tb4": tb4,
"tb3": tb3,
"tb6": tb6,
"tb5": tb5,
},
"passed": (
tb1 == '<input type="text" name="tb1" id="tb1" />'
and tb2 == '<input type="password" name="tb2" id="tb2" />'
and tb4 == '<input type="text" name="tb4" id="tb4" value="Hello World!" />'
and tb3 == '<textarea name="tb3" id="tb3"></textarea>'
and tb6 == '<textarea name="tb6" id="tb6" rows="5"></textarea>'
and tb5 == '<input type="text" name="tb5" id="tb5" size="30" />'
),
},
{
"name": "textbox_button_submit_label",
"summary": "模拟点击 Submit 按钮执行 submit 子例程并将 txt1.Text 拼接写入 lbl1.Text",
"expected": "Your name is Linus",
"actual": submit_lbl,
"passed": submit_lbl == "Your name is Linus",
},
{
"name": "textbox_autopostback_textchanged",
"summary": "验证 AutoPostBack='true' 生成 __doPostBack 并在文本发生真实变化时触发 OnTextChanged",
"expected": {
"has_autopostback_hook": True,
"when_changed": {"event_fired": True, "lbl1": "You changed text to ASP.NET"},
"when_same": {"event_fired": False, "lbl1": ""},
},
"actual": {
"has_autopostback_hook": "__doPostBack('txt1','')" in tb_auto,
"when_changed": change_fired,
"when_same": change_unchanged,
},
"passed": (
"__doPostBack('txt1','')" in tb_auto
and change_fired == {"event_fired": True, "lbl1": "You changed text to ASP.NET"}
and change_unchanged == {"event_fired": False, "lbl1": ""}
),
},
{
"name": "textbox_html_encoding_safety",
"summary": "验证单行与多行 TextBox 渲染用户输入时自动执行 HTML 实体转义",
"expected": {
"single_escaped": "" onclick="alert(1)" in xss_single,
"multi_escaped": "</textarea><script>x()</script>" in xss_multi,
},
"actual": {
"single_escaped": "" onclick="alert(1)" in xss_single,
"multi_escaped": "</textarea><script>x()</script>" in xss_multi,
},
"passed": (
"" onclick="alert(1)" in xss_single
and "</textarea><script>x()</script>" in xss_multi
),
},
]
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/web-forms-aspnet-textbox-lab/explore-aspnet-textbox.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-aspnet-textbox.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14 离线 ASP.NET WebForms 语义验证器(本机未启动 IIS / ASP.NET WebForms 运行时宿主)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联55章) · 含当前章节或实例 · mbskycom202612a2981.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2977.zip