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

运行 Ionic 1 Flexbox 等宽列、百分比列宽、偏移量、纵向对齐与三档响应式断点离线核验

PYTHONWindows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。explore-grid.py
explore-grid.py266 行
"""Offline Ionic 1 Flexbox Grid (equal cols, col-*, col-offset-*, vertical align & responsive breakpoints) verifier for Ch15 (grid)."""
from __future__ import annotations

import json
from lxml import html

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

COL_PCT = {
    "col-10": 10.0,
    "col-20": 20.0,
    "col-25": 25.0,
    "col-33": 33.3333,
    "col-50": 50.0,
    "col-67": 66.6666,
    "col-75": 75.0,
    "col-80": 80.0,
    "col-90": 90.0,
}

OFFSET_PCT = {f"col-offset-{k.split('-')[1]}": v for k, v in COL_PCT.items()}

RESPONSIVE_BREAKPOINTS = {
    "responsive-sm": 567,
    "responsive-md": 767,
    "responsive-lg": 1023,
}


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


def compute_row_widths(row_el) -> list[dict]:
    cols = row_el.xpath("./div[contains(concat(' ', @class, ' '), ' col ')]")
    explicit_sum = 0.0
    flex_count = 0
    specs = []
    for c in cols:
        cls = c.get("class", "").split()
        w = next((COL_PCT[k] for k in cls if k in COL_PCT), None)
        off = next((OFFSET_PCT[k] for k in cls if k in OFFSET_PCT), 0.0)
        if w is not None:
            explicit_sum += w + off
        else:
            explicit_sum += off
            flex_count += 1
        specs.append((w, off))
    rem = max(0.0, 100.0 - explicit_sum)
    each_flex = round(rem / flex_count, 4) if flex_count else 0.0
    return [
        {"width_pct": round(w, 4) if w is not None else each_flex, "offset_pct": round(off, 4)}
        for w, off in specs
    ]


def case_equal_and_explicit_columns() -> dict:
    markup = """
    <section>
      <div class="row">
        <div class="col">.col</div>
        <div class="col">.col</div>
        <div class="col">.col</div>
        <div class="col">.col</div>
        <div class="col">.col</div>
      </div>
      <div class="row">
        <div class="col col-50">.col.col-50</div>
        <div class="col">.col</div>
        <div class="col">.col</div>
      </div>
      <div class="row">
        <div class="col col-75">.col.col-75</div>
        <div class="col">.col</div>
      </div>
      <div class="row">
        <div class="col">.col</div>
        <div class="col col-75">.col.col-75</div>
      </div>
      <div class="row">
        <div class="col">.col</div>
        <div class="col">.col</div>
      </div>
    </section>
    """
    root = html.fragment_fromstring(markup)
    rows = [compute_row_widths(r) for r in root.xpath("./div[@class='row']")]
    actual = {
        "row1_5_equal_widths": [c["width_pct"] for c in rows[0]],
        "row2_50_plus_2_auto": [c["width_pct"] for c in rows[1]],
        "row3_75_plus_auto": [c["width_pct"] for c in rows[2]],
        "row4_auto_plus_75": [c["width_pct"] for c in rows[3]],
        "row5_2_equal": [c["width_pct"] for c in rows[4]],
        "explicit_class_count": len(COL_PCT),
    }
    expected = {
        "row1_5_equal_widths": [20.0, 20.0, 20.0, 20.0, 20.0],
        "row2_50_plus_2_auto": [50.0, 25.0, 25.0],
        "row3_75_plus_auto": [75.0, 25.0],
        "row4_auto_plus_75": [25.0, 75.0],
        "row5_2_equal": [50.0, 50.0],
        "explicit_class_count": 9,
    }
    return make_case(
        "grid_equal_and_percentage_columns",
        "Verify flexbox equal-width .col distribution and all 9 .col-* explicit percentage widths",
        expected,
        actual,
    )


def case_offset_columns() -> dict:
    markup = """
    <section>
      <div class="row">
        <div class="col col-33 col-offset-33">.col</div>
        <div class="col">.col</div>
      </div>
      <div class="row">
        <div class="col col-33">.col</div>
        <div class="col col-33 col-offset-33">.col</div>
      </div>
      <div class="row">
        <div class="col col-33 col-offset-67">.col</div>
      </div>
    </section>
    """
    root = html.fragment_fromstring(markup)
    rows = [compute_row_widths(r) for r in root.xpath("./div[@class='row']")]
    actual = {
        "offset_class_count": len(OFFSET_PCT),
        "row1": rows[0],
        "row2": rows[1],
        "row3": rows[2],
    }
    expected = {
        "offset_class_count": 9,
        "row1": [
            {"width_pct": 33.3333, "offset_pct": 33.3333},
            {"width_pct": 33.3334, "offset_pct": 0.0},
        ],
        "row2": [
            {"width_pct": 33.3333, "offset_pct": 0.0},
            {"width_pct": 33.3333, "offset_pct": 33.3333},
        ],
        "row3": [
            {"width_pct": 33.3333, "offset_pct": 66.6666},
        ],
    }
    return make_case(
        "grid_column_offsets",
        "Verify .col-offset-* left-margin calculation and remaining flex column sizing",
        expected,
        actual,
    )


def case_vertical_alignment() -> dict:
    markup = """
    <section>
      <div class="row">
        <div class="col col-top">.col</div>
        <div class="col col-center">.col</div>
        <div class="col col-bottom">.col</div>
        <div class="col">1<br>2<br>3<br>4</div>
      </div>
      <div class="row row-top"><div class="col">.col</div></div>
      <div class="row row-center"><div class="col">.col</div></div>
      <div class="row row-bottom"><div class="col">.col</div></div>
    </section>
    """
    root = html.fragment_fromstring(markup)
    flex_map = {
        "col-top": "flex-start",
        "col-center": "center",
        "col-bottom": "flex-end",
        "row-top": "flex-start",
        "row-center": "center",
        "row-bottom": "flex-end",
    }
    col_aligns = [
        flex_map[next(k for k in c.get("class", "").split() if k in flex_map)]
        for c in root.xpath("./div[1]/div")[:3]
    ]
    row_aligns = [
        flex_map[next(k for k in r.get("class", "").split() if k in flex_map)]
        for r in root.xpath("./div")[1:]
    ]
    actual = {
        "per_column_align_self": col_aligns,
        "per_row_align_items": row_aligns,
    }
    expected = {
        "per_column_align_self": ["flex-start", "center", "flex-end"],
        "per_row_align_items": ["flex-start", "center", "flex-end"],
    }
    return make_case(
        "grid_vertical_alignment",
        "Verify col-top/center/bottom (align-self) and row-top/center/bottom (align-items) flexbox rules",
        expected,
        actual,
    )


def case_responsive_breakpoints() -> dict:
    markup = """
    <div class="row responsive-sm">
      <div class="col">.col</div>
      <div class="col">.col</div>
      <div class="col">.col</div>
      <div class="col">.col</div>
    </div>
    """
    root = html.fragment_fromstring(markup)
    resp_cls = [c for c in root.get("class", "").split() if c.startswith("responsive-")][0]

    def layout_direction(cls_name: str, viewport_w: int) -> str:
        return "column" if viewport_w <= RESPONSIVE_BREAKPOINTS[cls_name] else "row"

    actual = {
        "responsive_class": resp_cls,
        "col_count": len(root.xpath("./div[@class='col']")),
        "sm_at_375px": layout_direction("responsive-sm", 375),
        "sm_at_640px": layout_direction("responsive-sm", 640),
        "md_at_767px": layout_direction("responsive-md", 767),
        "md_at_800px": layout_direction("responsive-md", 800),
        "lg_at_1023px": layout_direction("responsive-lg", 1023),
        "lg_at_1280px": layout_direction("responsive-lg", 1280),
    }
    expected = {
        "responsive_class": "responsive-sm",
        "col_count": 4,
        "sm_at_375px": "column",
        "sm_at_640px": "row",
        "md_at_767px": "column",
        "md_at_800px": "row",
        "lg_at_1023px": "column",
        "lg_at_1280px": "row",
    }
    return make_case(
        "grid_responsive_stacking_breakpoints",
        "Verify .responsive-sm (<=567px), .responsive-md (<=767px), and .responsive-lg (<=1023px) stacking thresholds",
        expected,
        actual,
    )


def main() -> None:
    cases = [
        case_equal_and_explicit_columns(),
        case_offset_columns(),
        case_vertical_alignment(),
        case_responsive_breakpoints(),
    ]
    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-grid-lab/explore-grid.py

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

下载课程关联工程包:Ionic

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

下载课程关联工程包:Ionic

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

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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