AgPerry's picture
Add files using upload-large-folder tool
489b6e2 verified
Raw History Blame Contribute Delete
21.4 kB
"""Shared page chrome for the Lowe's offline clone.
The header, category nav, footer and account drawer are rebuilt from the two
sanitized desktop screenshots under ``source-current/``. Structure and copy that
were legible there are reproduced; anything below the observed fold, and every
step past the drawer's Continue button, is clone-authored and marked in
``scope/claims.jsonl``.
Rendering rules that the tests depend on:
* no inline ``<script>`` and no inline ``style`` attribute, so the app can ship
a strict same-origin CSP without ``unsafe-inline``;
* every interactive control is a real link, form or submit button, so the page
works with JavaScript disabled — there is no ``href="#"`` anywhere;
* off-site destinations point at the local ``/external/<slug>`` boundary page
instead of a remote origin.
"""
from __future__ import annotations
import html
from dataclasses import dataclass, field
from typing import Any, Mapping, Sequence
from ui.icons import icon
SITE_LABEL = "Lowe's"
CLONE_LABEL = "WebsiteBench offline clone"
def esc(value: Any) -> str:
return html.escape("" if value is None else str(value), quote=True)
def attr(value: Any) -> str:
return html.escape("" if value is None else str(value), quote=True)
@dataclass
class Drawer:
"""State of the observed 'Sign In or Create Account' side panel."""
open: bool = False
step: str = "identify"
identifier: str = ""
identity_display: str = ""
remember: bool = True
errors: dict[str, str] = field(default_factory=dict)
notice: str = ""
sandbox_code: str | None = None
next_path: str = "/"
@dataclass
class Chrome:
"""Everything the shared chrome needs for one response."""
categories: Sequence[Mapping[str, Any]]
stores: Sequence[Mapping[str, Any]]
store: Mapping[str, Any] | None
postal_code: str
cart_units: int
account: Mapping[str, Any] | None
drawer: Drawer
path: str = "/"
query: str = ""
# ---------------------------------------------------------------------------
# top-level document
# ---------------------------------------------------------------------------
def document(
*,
title: str,
chrome: Chrome,
body: str,
description: str = "",
status_note: str = "",
) -> str:
return (
"<!doctype html>\n"
'<html lang="en">\n'
"<head>\n"
'<meta charset="utf-8">\n'
'<meta name="viewport" content="width=device-width, initial-scale=1">\n'
f"<title>{esc(title)}</title>\n"
f'<meta name="description" content="{attr(description)}">\n'
'<meta name="robots" content="noindex, nofollow">\n'
'<meta name="generator" content="WebsiteBench offline clone (lowes)">\n'
'<link rel="icon" href="/static/site/img/lowes-logo.svg" type="image/svg+xml">\n'
'<link rel="stylesheet" href="/static/site/lowes.css">\n'
'<script src="/static/site/lowes.js" defer></script>\n'
"</head>\n"
'<body data-route="' + attr(chrome.path) + '">\n'
'<a class="visually-hidden" href="#main">Skip to main content</a>\n'
+ promo_strip()
+ masthead(chrome)
+ catnav(chrome)
+ f'<main id="main">{body}</main>\n'
+ site_footer(status_note)
+ account_drawer(chrome)
+ "</body>\n</html>\n"
)
# ---------------------------------------------------------------------------
# chrome pieces
# ---------------------------------------------------------------------------
PROMO_LINKS: tuple[tuple[str, str], ...] = (
("Lowe's Credit Center", "/external/lowes-credit-center"),
("Order Status", "/account/orders"),
("Weekly Ad", "/search?sort=savings"),
("Lowe's PRO", "/external/lowes-pro"),
)
def promo_strip() -> str:
links = "".join(
f'<li><a href="{attr(href)}">{esc(label)}</a></li>'
for label, href in PROMO_LINKS
)
return (
'<div class="promo-strip"><div class="shell">'
'<a class="promo-strip__deal" href="/search?sort=savings">'
"Shop this week's top deals now. &gt;</a>"
f'<ul class="promo-strip__links">{links}</ul>'
"</div></div>\n"
)
def masthead(chrome: Chrome) -> str:
store = chrome.store or {}
store_name = store.get("name", "Select a store")
closes_at = store.get("closes_at", "")
account_label = "Sign In"
account_href = "/signin"
if chrome.account:
first = str(chrome.account.get("display_name", "")).split(" ")[0] or "Account"
account_label = first[:14]
account_href = "/account"
store_options = "".join(
f'<option value="{attr(item["id"])}"'
+ (" selected" if store.get("id") == item["id"] else "")
+ f'>{esc(item["name"])} &middot; {esc(item["zip"])}</option>'
for item in chrome.stores
)
return (
'<header class="masthead"><div class="shell masthead__row">\n'
'<a class="brand-logo" href="/" aria-label="Lowe\'s offline clone home">'
'<img src="/static/site/img/lowes-logo.svg" width="82" height="38"'
' alt="Lowe\'s offline clone"></a>\n'
'<form class="locator" method="post" action="/preferences">\n'
f'<input type="hidden" name="next" value="{attr(chrome.path)}">\n'
'<label class="visually-hidden" for="store-select">Selected store</label>\n'
+ icon("store", 18)
+ '<select class="locator__button" id="store-select" name="store_id"'
' data-autosubmit>'
+ store_options
+ "</select>\n"
f'<span class="locator__button" aria-hidden="true"><strong>{esc(closes_at)}</strong></span>\n'
'<span class="locator__divider"></span>\n'
+ icon("truck", 18)
+ '<label class="visually-hidden" for="zip-input">Delivery ZIP code</label>\n'
'<input class="locator__button" id="zip-input" name="postal_code"'
f' value="{attr(chrome.postal_code)}" size="6" inputmode="numeric"'
' pattern="[0-9]{5}" maxlength="5">\n'
'<button class="visually-hidden" type="submit">Update store and ZIP</button>\n'
"</form>\n"
f'<span class="visually-hidden">Selected store: {esc(store_name)}</span>\n'
+ searchbar(chrome)
+ '<div class="masthead__utilities">\n'
'<a class="utility" href="/assist">'
+ icon("sparkle", 22)
+ "<span>AI Assist</span></a>\n"
f'<a class="utility" href="{attr(account_href)}" data-account-open>'
+ icon("account", 22)
+ f"<span>{esc(account_label)}</span></a>\n"
'<a class="utility utility--cart" href="/cart">'
+ icon("cart", 22)
+ (
f'<span class="utility__count">{esc(chrome.cart_units)}</span>'
if chrome.cart_units
else ""
)
+ "<span>Cart</span></a>\n"
"</div>\n"
"</div></header>\n"
)
def searchbar(chrome: Chrome) -> str:
return (
'<div class="searchbar">\n'
'<form class="searchbar__form" method="get" action="/search" role="search"'
' data-suggest-form>\n'
'<label class="visually-hidden" for="site-search">What can we help you find?</label>\n'
'<input class="searchbar__input" id="site-search" type="search" name="q"'
' autocomplete="off" placeholder="What can we help you find?"'
f' value="{attr(chrome.query)}" data-suggest-input role="combobox"'
' aria-expanded="false" aria-controls="search-suggestions"'
' aria-autocomplete="list">\n'
'<button class="icon-button" type="submit" aria-label="Search">'
+ icon("search", 22)
+ "</button>\n"
'<a class="icon-button" href="/visual-search" aria-label="Visual search">'
+ icon("camera", 22)
+ "</a>\n"
"</form>\n"
'<div class="suggestions" id="search-suggestions" role="listbox"'
' aria-label="Search suggestions" data-suggest-panel hidden></div>\n'
"</div>\n"
)
NAV_PRIMARY: tuple[tuple[str, str, str | None, bool], ...] = (
("Shop All", "/departments", "grid", True),
("Installations", "/services/installation", "tools", False),
("Deals", "/search?sort=savings", "tag", False),
("Design & Ideas", "/ideas", "bulb", False),
("$99 Maintenance", "/services/maintenance", None, False),
)
def catnav(chrome: Chrome) -> str:
items: list[str] = []
for label, href, glyph, strong in NAV_PRIMARY:
badge = '<span class="badge-new">New</span>' if label.startswith("$99") else ""
items.append(
f'<li><a class="catnav__link{" catnav__link--strong" if strong else ""}"'
f' href="{attr(href)}">'
+ (icon(glyph, 18) if glyph else "")
+ f"<span>{esc(label)}</span>{badge}</a></li>"
)
for category in chrome.categories[:8]:
items.append(
f'<li><a class="catnav__link" href="/search?category={attr(category["id"])}">'
f'{esc(category["label"])}</a></li>'
)
return (
'<nav class="catnav" aria-label="Departments"><div class="shell">'
f'<ul class="catnav__list">{"".join(items)}</ul>'
"</div></nav>\n"
)
FOOTER_COLUMNS: tuple[tuple[str, tuple[tuple[str, str], ...]], ...] = (
(
"Customer Service",
(
("Order Status", "/account/orders"),
("Returns", "/help/returns"),
("Shipping & Delivery", "/help/delivery"),
("Store Pickup", "/help/pickup"),
),
),
(
"Shop",
(
("All Departments", "/departments"),
("Deals", "/search?sort=savings"),
("Top Sellers", "/search?sort=top-sellers"),
("Top Rated", "/search?sort=rating"),
),
),
(
"Services",
(
("Installation Services", "/services/installation"),
("$99 Maintenance", "/services/maintenance"),
("Design & Ideas", "/ideas"),
("Store Locator", "/stores"),
),
),
(
"About this clone",
(
("Clone scope and limits", "/about-this-clone"),
("Terms of Use", "/external/terms-of-use"),
("Privacy Statement", "/external/privacy-statement"),
("Lowe's PRO", "/external/lowes-pro"),
),
),
)
def site_footer(status_note: str = "") -> str:
columns = "".join(
f"<div><h3>{esc(title)}</h3><ul>"
+ "".join(
f'<li><a href="{attr(href)}">{esc(label)}</a></li>' for label, href in links
)
+ "</ul></div>"
for title, links in FOOTER_COLUMNS
)
note = f"<p>{esc(status_note)}</p>" if status_note else ""
return (
'<footer class="site-footer"><div class="shell">'
f'<div class="footer-grid">{columns}</div>'
'<div class="footer-note">'
"<p>This is a WebsiteBench offline clone of the Lowe's home page and "
"account drawer. It is not Lowe's, it is not connected to Lowe's, and "
"no request leaves this machine. Prices, products, ratings, reviews, "
"stores, orders and payments are synthetic local fixtures.</p>"
f"{note}"
"</div>"
"</div></footer>\n"
)
# ---------------------------------------------------------------------------
# account drawer
# ---------------------------------------------------------------------------
DRAWER_BENEFITS: tuple[tuple[str, str], ...] = (
("package", "Order Status"),
("wrench", "Projects & Installations"),
("heart", "Lists"),
("medal", "Military Discount"),
("asterisk", "First Responder Benefits"),
)
DRAWER_LEGAL: tuple[tuple[str, str], ...] = (
("Terms of Use", "/external/terms-of-use"),
("Personal Account Terms", "/external/personal-account-terms"),
("Privacy Statement", "/external/privacy-statement"),
(
"Notice of Financial Incentive / Loyalty Programs",
"/external/loyalty-programs",
),
)
def _field(
*,
name: str,
label: str,
kind: str = "text",
value: str = "",
error: str = "",
autocomplete: str = "",
placeholder: str = "",
required: bool = True,
extra: str = "",
) -> str:
complete = f' autocomplete="{attr(autocomplete)}"' if autocomplete else ""
place = f' placeholder="{attr(placeholder)}"' if placeholder else ""
error_html = (
f'<span class="field__error" id="{attr(name)}-error">{esc(error)}</span>'
if error
else ""
)
described = f' aria-describedby="{attr(name)}-error"' if error else ""
return (
f'<label class="field" for="field-{attr(name)}">'
f'<span class="field__label">{esc(label)}</span>'
f'<input class="field__control" id="field-{attr(name)}" type="{attr(kind)}"'
f' name="{attr(name)}" value="{attr(value)}"'
f'{" required" if required else ""}{complete}{place}{described}{extra}>'
f"{error_html}</label>"
)
def account_drawer(chrome: Chrome) -> str:
drawer = chrome.drawer
if chrome.account:
inner = _drawer_signed_in(chrome)
elif drawer.step == "sign-in":
inner = _drawer_sign_in(drawer)
elif drawer.step == "create":
inner = _drawer_create(drawer)
elif drawer.step == "verify":
inner = _drawer_verify(drawer)
else:
inner = _drawer_identify(drawer)
state = "open" if drawer.open else "closed"
return (
'<div class="drawer-overlay" data-account-overlay'
+ ("" if drawer.open else " hidden")
+ "></div>\n"
f'<aside class="drawer" data-account-drawer="{attr(state)}" role="dialog"'
' aria-modal="true" aria-labelledby="drawer-title"'
+ ("" if drawer.open else " hidden")
+ ">\n"
'<div class="drawer__head">'
'<h2 class="drawer__title" id="drawer-title">'
+ ("Your Account" if chrome.account else "Sign In or Create Account")
+ "</h2>"
'<a class="icon-button" href="' + attr(drawer.next_path) + '"'
' data-account-close aria-label="Close">' + icon("close", 22) + "</a>"
"</div>\n"
+ inner
+ "</aside>\n"
)
def _drawer_legal_block() -> str:
links = ", ".join(
f'<a href="{attr(href)}">{esc(label)}</a>' for label, href in DRAWER_LEGAL
)
return (
'<p class="drawer__legal">By signing in to this offline clone, you are '
"agreeing to the local stand-ins for " + links + ". These pages are not "
"cloned; each link opens a same-origin boundary page.</p>"
)
def _drawer_benefits_block() -> str:
items = "".join(
f"<li>{icon(glyph, 18)}<span>{esc(label)}</span></li>"
for glyph, label in DRAWER_BENEFITS
)
return f'<ul class="drawer__benefits">{items}</ul>'
def _notice(drawer: Drawer) -> str:
blocks = ""
if drawer.notice:
blocks += f'<p class="notice">{esc(drawer.notice)}</p>'
if drawer.errors.get("form"):
blocks += f'<p class="notice notice--error">{esc(drawer.errors["form"])}</p>'
return blocks
def _drawer_identify(drawer: Drawer) -> str:
return (
'<form method="post" action="/account/identify">'
f'<input type="hidden" name="next" value="{attr(drawer.next_path)}">'
+ _notice(drawer)
+ '<p class="drawer__intro">Enter your <strong>email or phone number</strong> '
"to get started. We&#8217;ll check if you have an account.</p>"
+ _field(
name="identifier",
label="Email or Phone Number",
value=drawer.identifier,
error=drawer.errors.get("identifier", ""),
autocomplete="username",
placeholder="Email or Phone Number",
)
+ '<label class="checkbox"><input type="checkbox" name="remember"'
+ (" checked" if drawer.remember else "")
+ ' value="1"><span>Remember me on this device</span>'
+ icon("info", 16)
+ "</label>"
'<button class="button button--primary button--block" type="submit">'
"Continue</button>"
+ _drawer_legal_block()
+ _drawer_benefits_block()
+ "</form>"
)
def _drawer_sign_in(drawer: Drawer) -> str:
return (
'<form method="post" action="/account/sign-in">'
f'<input type="hidden" name="next" value="{attr(drawer.next_path)}">'
f'<input type="hidden" name="identifier" value="{attr(drawer.identifier)}">'
f'<input type="hidden" name="remember" value="{"1" if drawer.remember else "0"}">'
+ _notice(drawer)
+ '<p class="drawer__intro">Welcome back. Enter the password for '
f"<strong>{esc(drawer.identity_display or drawer.identifier)}</strong>.</p>"
+ _field(
name="password",
label="Password",
kind="password",
error=drawer.errors.get("password", ""),
autocomplete="current-password",
)
+ '<button class="button button--primary button--block" type="submit">'
"Sign In</button>"
'<p class="drawer__legal">Wrong address? '
'<a href="/signin">Start over with a different email or phone number.</a></p>'
+ _drawer_benefits_block()
+ "</form>"
)
def _drawer_create(drawer: Drawer) -> str:
return (
'<form method="post" action="/account/create">'
f'<input type="hidden" name="next" value="{attr(drawer.next_path)}">'
f'<input type="hidden" name="identifier" value="{attr(drawer.identifier)}">'
f'<input type="hidden" name="remember" value="{"1" if drawer.remember else "0"}">'
+ _notice(drawer)
+ '<p class="drawer__intro">Create an account for '
f"<strong>{esc(drawer.identity_display or drawer.identifier)}</strong>.</p>"
+ _field(
name="first_name",
label="First Name",
value=drawer.errors.get("first_name_value", ""),
error=drawer.errors.get("first_name", ""),
autocomplete="given-name",
)
+ _field(
name="last_name",
label="Last Name",
value=drawer.errors.get("last_name_value", ""),
error=drawer.errors.get("last_name", ""),
autocomplete="family-name",
)
+ _field(
name="password",
label="Password (8 characters or more)",
kind="password",
error=drawer.errors.get("password", ""),
autocomplete="new-password",
)
+ '<button class="button button--primary button--block" type="submit">'
"Create Account</button>"
'<p class="drawer__legal">Already have an account? '
'<a href="/signin">Start over.</a></p>'
+ _drawer_legal_block()
+ "</form>"
)
def _drawer_verify(drawer: Drawer) -> str:
sandbox = ""
if drawer.sandbox_code:
sandbox = (
'<p class="notice notice--sandbox"><strong>Local sandbox helper.</strong> '
"This offline clone cannot send mail, so the code issued for this "
"browser session is shown here: "
f"<code>{esc(drawer.sandbox_code)}</code>. The code lives only in this "
"process&#8217;s memory, is never written to the database or a log, "
"and disappears on restart. Lowe&#8217;s does not do this; set "
"<code>LOWES_LOCAL_SANDBOX_HELP=0</code> to hide it.</p>"
)
return (
'<form method="post" action="/account/verify">'
f'<input type="hidden" name="next" value="{attr(drawer.next_path)}">'
f'<input type="hidden" name="identifier" value="{attr(drawer.identifier)}">'
f'<input type="hidden" name="remember" value="{"1" if drawer.remember else "0"}">'
+ _notice(drawer)
+ '<p class="drawer__intro">Enter the 6-digit verification code issued for '
f"<strong>{esc(drawer.identity_display or drawer.identifier)}</strong>.</p>"
+ sandbox
+ _field(
name="code",
label="Verification Code",
value="",
error=drawer.errors.get("code", ""),
autocomplete="one-time-code",
placeholder="000000",
extra=' inputmode="numeric" pattern="[0-9]{6}" maxlength="6"',
)
+ '<button class="button button--primary button--block" type="submit">'
"Verify and Create Account</button>"
"</form>"
'<form method="post" action="/account/resend">'
f'<input type="hidden" name="next" value="{attr(drawer.next_path)}">'
f'<input type="hidden" name="identifier" value="{attr(drawer.identifier)}">'
'<button class="link-button" type="submit">Send a new code</button>'
"</form>"
)
def _drawer_signed_in(chrome: Chrome) -> str:
account = chrome.account or {}
return (
'<p class="drawer__intro">Signed in as '
f"<strong>{esc(account.get('display_name', ''))}</strong><br>"
f"{esc(account.get('contact_display', ''))}</p>"
'<p><a class="button button--primary button--block" href="/account">'
"Go to your account</a></p>"
'<form method="post" action="/account/sign-out">'
f'<input type="hidden" name="next" value="{attr(chrome.drawer.next_path)}">'
'<button class="button button--outline button--block" type="submit">'
"Sign Out</button>"
"</form>" + _drawer_benefits_block()
)