Layout Scorers API¶
Deterministic geometry/contrast defect detection measured directly off the rendered page — no LLM, no API key. Detects contrast failures, sibling overlap, clipped content, viewport protrusion (both edges), page-level horizontal overflow, ellipsis text truncation, WCAG-aware target spacing, complete focus obscuration, and rendered text occlusion. Target-size findings apply machine-measurable WCAG 2.5.8 exceptions. The scanner also exercises focusable controls for complete focus obscuration (WCAG 2.4.11) and detects painted DOM elements crossing rendered text, including graph labels. Semantic exceptions remain explicit manual-review fields; the report is not a site-wide conformance claim.
LayoutScorer¶
- class layoutlens.LayoutScorer(*, min_target_px=24, overlap_threshold_px2=200, clip_tolerance_px=2, protrude_tolerance_px=1, contrast_threshold=4.5, occlusion_samples_per_axis=5)[source]¶
Bases:
objectDeterministic layout/geometry scorer over a rendered page.
- Parameters:
- async detect_overlaps(page)[source]¶
Return findings for visible siblings whose bounding boxes overlap.
- Parameters:
page (Page)
- Return type:
- async detect_clipping(page)[source]¶
Return findings for elements whose content is clipped by hidden overflow.
- Parameters:
page (Page)
- Return type:
- async detect_protrusion(page)[source]¶
Return findings for elements protruding past either horizontal viewport edge.
- Parameters:
page (Page)
- Return type:
- async detect_page_overflow(page)[source]¶
Return a finding if the whole document scrolls horizontally.
- Parameters:
page (Page)
- Return type:
- async detect_truncation(page)[source]¶
Return findings for single-line text actually cut off by an ellipsis.
- Parameters:
page (Page)
- Return type:
- async detect_small_targets(page)[source]¶
Return undersized targets that also fail measurable WCAG spacing exceptions.
The spacing, inline, and unmodified user-agent-control exceptions are evaluated automatically. Equivalent-control and essential-presentation exceptions are semantic and remain manual-review fields on every finding.
- Parameters:
page (Page)
- Return type:
- async detect_text_occlusion(page)[source]¶
Return rendered text fragments covered by another painted DOM element.
- Parameters:
page (Page)
- Return type:
- async detect_focus_obscured(page)[source]¶
Return keyboard-focused components entirely hidden by author DOM content.
This automates the geometric core of WCAG 2.4.11. Whether an occluder was user-opened and can be dismissed without advancing focus can require interaction history, so each finding discloses those manual exceptions.
- Parameters:
page (Page)
- Return type:
- async scan_page(page, source=None, viewport='desktop')[source]¶
Run every detector on an already-loaded page and return a report.
- Parameters:
- Returns:
The structured layout report.
- Return type:
Report Types¶
- class layoutlens.LayoutReport(source, viewport, findings, timestamp=<factory>)[source]¶
Bases:
objectStructured deterministic layout report for a single page and viewport.
- Parameters:
source (str)
viewport (str)
findings (list[LayoutFinding])
timestamp (str)
- findings¶
All measured layout defects, in detector order.
- class layoutlens.LayoutFinding(defect_class, selector, bbox, measured, threshold, description, wcag_refs=<factory>)[source]¶
Bases:
objectA single measured layout defect affecting one or two DOM elements.
- Parameters:
- defect_class¶
Stable finding class such as
"contrast","overlap","clipping","viewport-protrusion","target-size","focus-obscured", or"text-occlusion".- Type:
- selector¶
A best-effort CSS selector locating the offending element (for overlap, the primary element; the partner is in
measured).- Type:
- measured¶
The measured numbers behind the finding (e.g. the contrast ratio, the intersection area, the overflow in pixels).
Contrast Math¶
- layoutlens.contrast_ratio(rgb1, rgb2)[source]¶
Return the WCAG contrast ratio (1..21) between two sRGB colors (symmetric).
- async layoutlens.check_contrast(page, *, threshold=4.5)[source]¶
Scan
pagefor text below the WCAG AA contrast threshold.Reads every visible text element’s computed foreground and effective opaque background, computes the WCAG contrast ratio, and returns a finding for each element under threshold (3.0 for large text,
thresholdotherwise).- Parameters:
page (Page) – A loaded Playwright page.
threshold (float) – The AA normal-text ratio to require (default 4.5).
- Returns:
One
LayoutFindingper low-contrast text element.- Return type: