蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

运行 Ionic 1 八类表单输入框布局与浮动标签状态离线核验

PYTHONWindows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。explore-form-input.py
explore-form-input.py223 行
"""Offline Ionic 1 Form & Input layouts (placeholder, input-label, stacked, floating, inset, search) verifier for Ch08 (form-input)."""
from __future__ import annotations

import json
from lxml import html

VERSION = "CPython 3.12.14 / lxml offline Ionic UI & component verifier"


def make_case(name: str, summary: str, expected, actual) -> dict:
    return {
        "name": name,
        "summary": summary,
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_placeholder_and_inline_label() -> dict:
    markup = """
    <section>
      <div class="list">
        <label class="item item-input"><input type="text" placeholder="First Name"></label>
        <label class="item item-input"><input type="text" placeholder="Last Name"></label>
        <label class="item item-input"><textarea placeholder="Comments"></textarea></label>
      </div>
      <div class="list">
        <label class="item item-input">
          <span class="input-label">用户名:</span>
          <input type="text">
        </label>
        <label class="item item-input">
          <span class="input-label">密码:</span>
          <input type="password">
        </label>
      </div>
    </section>
    """
    root = html.fragment_fromstring(markup)
    lists = root.xpath("./div[@class='list']")
    placeholders = lists[0].xpath(".//*[@placeholder]/@placeholder")
    control_tags = [el.tag for el in lists[0].xpath(".//label/*")]
    inline_labels = [s.text_content().strip() for s in lists[1].xpath(".//span[@class='input-label']")]
    input_types = lists[1].xpath(".//input/@type")
    actual = {
        "placeholders": placeholders,
        "placeholder_controls": control_tags,
        "inline_labels": inline_labels,
        "inline_input_types": input_types,
    }
    expected = {
        "placeholders": ["First Name", "Last Name", "Comments"],
        "placeholder_controls": ["input", "input", "textarea"],
        "inline_labels": ["用户名:", "密码:"],
        "inline_input_types": ["text", "password"],
    }
    return make_case(
        "form_placeholder_and_inline_input_label",
        "Verify placeholder-only inputs/textarea and left-aligned .input-label text/password rows",
        expected,
        actual,
    )


def case_stacked_and_floating_labels() -> dict:
    markup = """
    <section>
      <div class="list">
        <label class="item item-input item-stacked-label">
          <span class="input-label">First Name</span>
          <input type="text" placeholder="John">
        </label>
        <label class="item item-input item-stacked-label">
          <span class="input-label">Last Name</span>
          <input type="text" placeholder="Suhr">
        </label>
        <label class="item item-input item-stacked-label">
          <span class="input-label">Email</span>
          <input type="text" placeholder="john@suhr.com">
        </label>
      </div>
      <div class="list">
        <label class="item item-input item-floating-label">
          <span class="input-label">First Name</span>
          <input type="text" placeholder="First Name">
        </label>
        <label class="item item-input item-floating-label">
          <span class="input-label">Last Name</span>
          <input type="text" placeholder="Last Name">
        </label>
        <label class="item item-input item-floating-label">
          <span class="input-label">Email</span>
          <input type="text" placeholder="Email">
        </label>
      </div>
    </section>
    """
    root = html.fragment_fromstring(markup)
    stacked = root.xpath(".//label[contains(@class,'item-stacked-label')]")
    floating = root.xpath(".//label[contains(@class,'item-floating-label')]")

    # Simulate floating label CSS state (.has-input added when value is non-empty)
    def floating_state(value: str) -> dict:
        has_input = bool(value)
        return {"has_input_class": has_input, "label_opacity": 1.0 if has_input else 0.0}

    actual = {
        "stacked_count": len(stacked),
        "floating_count": len(floating),
        "floating_empty_state": floating_state(""),
        "floating_filled_state": floating_state("Ada"),
    }
    expected = {
        "stacked_count": 3,
        "floating_count": 3,
        "floating_empty_state": {"has_input_class": False, "label_opacity": 0.0},
        "floating_filled_state": {"has_input_class": True, "label_opacity": 1.0},
    }
    return make_case(
        "form_stacked_and_floating_labels",
        "Verify item-stacked-label and item-floating-label markup and floating label visibility state",
        expected,
        actual,
    )


def case_inset_form_and_inset_input() -> dict:
    markup = """
    <section>
      <div class="list list-inset">
        <label class="item item-input"><input type="text" placeholder="First Name"></label>
        <label class="item item-input"><input type="text" placeholder="Last Name"></label>
      </div>
      <div class="list">
        <div class="item item-input-inset">
          <label class="item-input-wrapper">
            <input type="text" placeholder="Email">
          </label>
          <button class="button button-small">Submit</button>
        </div>
      </div>
    </section>
    """
    root = html.fragment_fromstring(markup)
    inset_list_inputs = len(root.xpath("./div[contains(@class,'list-inset')]//input"))
    inset_item = root.xpath(".//div[contains(@class,'item-input-inset')]")[0]
    actual = {
        "inset_form_input_count": inset_list_inputs,
        "has_input_wrapper": bool(inset_item.xpath("./label[@class='item-input-wrapper']/input")),
        "inline_button_text": inset_item.xpath("./button/text()")[0].strip(),
        "inline_button_small": "button-small" in inset_item.xpath("./button/@class")[0].split(),
    }
    expected = {
        "inset_form_input_count": 2,
        "has_input_wrapper": True,
        "inline_button_text": "Submit",
        "inline_button_small": True,
    }
    return make_case(
        "form_inset_list_and_input_inset_button",
        "Verify list list-inset form and item-input-inset + item-input-wrapper + button-small layout",
        expected,
        actual,
    )


def case_icon_and_header_search_input() -> dict:
    markup = """
    <section>
      <div class="list list-inset">
        <label class="item item-input">
          <i class="icon ion-search placeholder-icon"></i>
          <input type="text" placeholder="Search">
        </label>
      </div>
      <div class="bar bar-header item-input-inset">
        <label class="item-input-wrapper">
          <i class="icon ion-ios-search placeholder-icon"></i>
          <input type="search" placeholder="搜索">
        </label>
        <button class="button button-clear">取消</button>
      </div>
    </section>
    """
    root = html.fragment_fromstring(markup)
    list_icon_before_input = root.xpath("./div[1]//label/*")[0].tag == "i"
    hdr = root.xpath("./div[contains(@class,'bar-header')]")[0]
    actual = {
        "list_icon_precedes_input": list_icon_before_input,
        "list_icon_has_placeholder_class": "placeholder-icon" in root.xpath("./div[1]//i/@class")[0].split(),
        "header_has_item_input_inset": "item-input-inset" in hdr.get("class", "").split(),
        "header_input_type": hdr.xpath(".//input/@type")[0],
        "header_cancel_button": hdr.xpath("./button/text()")[0].strip(),
    }
    expected = {
        "list_icon_precedes_input": True,
        "list_icon_has_placeholder_class": True,
        "header_has_item_input_inset": True,
        "header_input_type": "search",
        "header_cancel_button": "取消",
    }
    return make_case(
        "form_icon_and_header_search_bar",
        "Verify placeholder-icon placement before <input> and bar-header item-input-inset search bar",
        expected,
        actual,
    )


def main() -> None:
    cases = [
        case_placeholder_and_inline_label(),
        case_stacked_and_floating_labels(),
        case_inset_form_and_inset_input(),
        case_icon_and_header_search_input(),
    ]
    print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))


if __name__ == "__main__":
    main()
本例需在 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。

入口文件:projects/ionic-form-input-lab/explore-form-input.py

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-form-input.py,或下载右侧课程工程包。
  2. 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:Ionic

课程关联工程包(登记关联37章) · 含当前章节或实例 · mbskycom202612a2013.zip

下载课程关联工程包:Ionic

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2019.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习