"""Offline Playwright Chromium lab for AngularJS Ch 19: api."""
import json
from playwright.sync_api import sync_playwright
HTML_DOC = r"""<!DOCTYPE html>
<html lang="zh-CN">
<head><meta charset="utf-8"><title>AngularJS Global API Lab</title></head>
<body>
<div id="out-lower"></div>
<div id="out-upper"></div>
<div id="out-isstr"></div>
<div id="out-isnum"></div>
<script>
// Offline AngularJS 1.x Global API equivalence (including 1.7+ $$lowercase/$$uppercase transition)
window.angular = (function () {
const isString = (v) => typeof v === "string";
const isNumber = (v) => typeof v === "number";
const lowercase = (s) => (isString(s) ? s.toLowerCase() : s);
const uppercase = (s) => (isString(s) ? s.toUpperCase() : s);
function copy(src) {
if (src === null || typeof src !== "object") return src;
if (Array.isArray(src)) return src.map(copy);
const out = {};
for (const k of Object.keys(src)) out[k] = copy(src[k]);
return out;
}
function equals(a, b) {
if (a === b) return true;
if (a === null || b === null || typeof a !== "object" || typeof b !== "object") {
return Number.isNaN(a) && Number.isNaN(b);
}
if (Array.isArray(a) !== Array.isArray(b)) return false;
const ka = Object.keys(a).filter((k) => !k.startsWith("$"));
const kb = Object.keys(b).filter((k) => !k.startsWith("$"));
if (ka.length !== kb.length) return false;
return ka.every((k) => Object.prototype.hasOwnProperty.call(b, k) && equals(a[k], b[k]));
}
return {
// <1.7 public names + 1.7+ internal names for version-difference testing
lowercase,
uppercase,
$$lowercase: lowercase,
$$uppercase: uppercase,
isString,
isNumber,
copy,
equals
};
})();
(function () {
document.getElementById("out-lower").textContent = angular.$$lowercase("ANGULARJS");
document.getElementById("out-upper").textContent = angular.$$uppercase("angularjs");
document.getElementById("out-isstr").textContent = String(angular.isString("ANGULARJS"));
document.getElementById("out-isnum").textContent = String(angular.isNumber("ANGULARJS"));
})();
</script>
</body>
</html>
"""
def launch_browser(p):
try:
return p.chromium.launch(headless=True)
except Exception:
return p.chromium.launch(headless=True, channel="chrome")
def main():
cases = []
with sync_playwright() as p:
browser = launch_browser(p)
try:
page = browser.new_page()
page.set_content(HTML_DOC)
# Case 1: lowercase / $$lowercase and uppercase / $$uppercase parity + non-string passthrough
actual1 = page.evaluate("""() => ({
lower_dom: document.getElementById("out-lower").textContent,
upper_dom: document.getElementById("out-upper").textContent,
pre17_lower: angular.lowercase("HeLLo-1.6"),
post17_lower: angular.$$lowercase("HeLLo-1.7"),
pre17_upper: angular.uppercase("HeLLo-1.6"),
post17_upper: angular.$$uppercase("HeLLo-1.7"),
non_string_passthrough: angular.$$lowercase(42)
})""")
expected1 = {
"lower_dom": "angularjs",
"upper_dom": "ANGULARJS",
"pre17_lower": "hello-1.6",
"post17_lower": "hello-1.7",
"pre17_upper": "HELLO-1.6",
"post17_upper": "HELLO-1.7",
"non_string_passthrough": 42,
}
cases.append({"name": "lowercase_and_uppercase_api_parity", "expected": expected1, "actual": actual1, "passed": actual1 == expected1})
# Case 2: isString and isNumber type checks
actual2 = page.evaluate("""() => ({
dom_isstr: document.getElementById("out-isstr").textContent,
dom_isnum: document.getElementById("out-isnum").textContent,
str_empty: angular.isString(""),
str_num: angular.isString(123),
num_int: angular.isNumber(42),
num_float: angular.isNumber(3.14),
num_str_digits: angular.isNumber("42")
})""")
expected2 = {
"dom_isstr": "true",
"dom_isnum": "false",
"str_empty": True,
"str_num": False,
"num_int": True,
"num_float": True,
"num_str_digits": False,
}
cases.append({"name": "isstring_and_isnumber_type_checks", "expected": expected2, "actual": actual2, "passed": actual2 == expected2})
# Case 3: angular.copy and angular.equals deep object utilities
actual3 = page.evaluate("""() => {
const orig = { name: "AngularJS", meta: { ver: "1.8" }, $$hashKey: "obj:1" };
const cloned = angular.copy(orig);
const sameBefore = angular.equals(orig, { name: "AngularJS", meta: { ver: "1.8" } });
cloned.meta.ver = "1.7";
return {
distinct_ref: orig !== cloned && orig.meta !== cloned.meta,
equals_ignores_dollar_key: sameBefore,
orig_unmutated: orig.meta.ver,
equals_after_mutation: angular.equals(orig, cloned)
};
}""")
expected3 = {
"distinct_ref": True,
"equals_ignores_dollar_key": True,
"orig_unmutated": "1.8",
"equals_after_mutation": False,
}
cases.append({"name": "copy_and_equals_object_utilities", "expected": expected3, "actual": actual3, "passed": actual3 == expected3})
finally:
browser.close()
print(json.dumps({"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/angularjs-api-lab/explore-api.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-api.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联26章) · 含当前章节或实例 · mbskycom202612a851.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a849.zip