onehtmlCookbook
onehtml cookbook
Short recipes for common onehtml tasks, each with complete code you can copy.
Recipes
The accessibility tree of a pageCompute the tree a browser exposes to screen readers, with roles, accessible names, levels and hidden state, from HTML and CSS alone, and get the landmark and heading lists a screen-reader user navigates by.Apply suggested accessibility fixesTake the edits the checker suggests for rules with exactly one correct fix, apply the ones you accept as lossless edits, and re-check only what changed.Compute CSS styles for an elementGet the computed value of any CSS property for an element, find the rule it came from, and recompute styles for a focused element or a narrow viewport, including stylesheets the page links to.Query HTML with CSS selectorsFind elements with QuerySelector, QuerySelectorAll, Matches and Closest using Selectors Level 4, including :has(), :is(), :not() with selector lists, and form-state pseudo-classes.Detect the encoding of an HTML pageDecode HTML bytes the way a browser does, using the byte order mark, the HTTP Content-Type header, the meta charset prescan and the windows-1252 fallback, and override the fallback when you know better.Edit HTML without reformatting itChange attributes, text and inline styles in an HTML file so that only the edited bytes change: quoting, character references, comments, whitespace and the rest of the file stay exactly as they were.Extract all links and images from a pageList every link and image on a page with its resolved URL, its attributes in source order, and the accessible name a screen reader announces for it.Extract visible text from HTMLGet the readable text of a page without its script and style contents, either with a short walk over the text nodes or with the cascade-aware reading view, which also drops content hidden by CSS, the hidden attribute or aria-hidden.Parse HTML fragmentsParse a snippet of HTML, such as table rows or list items, in the context of the element it belongs in, so you get the elements a browser would create and not a whole html and body document.Coming from AngleSharp: where the trees differIf you are moving HTML parsing from AngleSharp to onehtml, this is what changes in the trees you get. On a corpus of real pages the two agree exactly on about nine in ten pages, and every difference in the rest comes from one of a short list of documented causes.Parse and edit CSSParse a stylesheet into rules and declarations with source ranges, walk nested @media rules, and change a declaration or add a rule without reformatting the rest of the file.Parse HTML from a string, file or streamLoad HTML into a document from a string, a file or a stream, let the parser pick the encoding from the bytes, and see the tokenizer’s parse errors when you need them.Remove scripts and event handlersStrip script elements, inline event-handler attributes and javascript: links from a page with lossless edits, and know where this stops: onehtml has no HTML sanitizer.Save edits as JSON and replay themStore edits in the versioned onehtml-edits JSON format and apply them to another copy of the page later. Replay finds each target again or reports why it can’t, and never edits the wrong element.Run WCAG checks in CI with SARIF outputCheck every HTML file of a built site against WCAG 2.2 AA without a browser, write one SARIF 2.1.0 file per page for your code-scanning tool, and fail the build when a rule fails.