Introduction

edit

Meziantou.Framework.WebPlatform implements the web platform in .NET. It parses HTML, XML, SVG, MathML and CSS exactly as a browser does, and exposes the DOM API that the scripts of a page use, with .NET naming. A layout engine computes the geometry of the pages, and the companion packages run their scripts, take screenshots, print them to PDF and automate them with Playwright.

What you can do with it

  • Parse and query HTML: parse a page as a browser does, whatever its errors, then query it with CSS selectors, XPath and LINQ. See HTML and the DOM.
  • Change and write HTML: create and move elements, insert untrusted markup safely, and write the document back, minified or indented. See Modifying documents and Sanitizing HTML.
  • Work with CSS: read the computed style of an element, edit style sheets, and format CSS. See CSS.
  • Load pages: navigate to a URL with its style sheets, images and frames, with cookies and storage, emulate a device, read the layout and inject the input of a user. See Loading pages.
  • Run the scripts of the pages with a JavaScript engine written in .NET, and WebAssembly modules. See Scripting.
  • Render pages: take screenshots of pages and elements, print them to images or PDF, draw on canvases. See Rendering.
  • Automate pages with Playwright: the Playwright clients for .NET, Node.js, Python and Java drive the engine as they drive Chromium. See Playwright.

Packages

The library is split into packages, so that an application only references what it uses.

PackageAdds
Meziantou.Framework.WebPlatformThe parsers, the DOM, CSS, the layout, the browsing contexts and their event loop
Meziantou.Framework.WebPlatform.JavaScriptA script engine: runs the scripts of the pages with Meziantou.Framework.JavaScriptEngine
Meziantou.Framework.WebPlatform.WasmtimeA WebAssembly runtime for the scripts, based on Wasmtime (a native library)
Meziantou.Framework.WebPlatform.ImagingA rendering backend: screenshots, pages printed as images, 2D canvases and image decoding, with Meziantou.Framework.Imaging
Meziantou.Framework.WebPlatform.PdfRenderingPrinting to PDF, with Meziantou.Framework.Pdf
Meziantou.Framework.WebPlatform.PlaywrightA Playwright server whose browser is this engine
Meziantou.Framework.WebPlatform.Playwright.ToolA .NET tool that runs the Playwright server from the command line

Principles

  • Behaves as a browser. The parsers, the DOM, the cascade and the layout follow the WHATWG and W3C specifications: a page gives the same tree, the same styles and the same events as in a browser. The library is tested against the web-platform-tests, the conformance test suite of the browsers (see the pass rate per section).
  • Everything is opt-in. A parsed document is only a tree. Nothing is fetched without a document loader, no script runs without a script engine, and no pixel is drawn without a rendering backend. You decide what a page can do.
  • Same pixels everywhere. The rendering is fully managed and the default fonts are part of the library: the same page gives the same screenshot on Windows, Linux and macOS.
  • The web API, with .NET names. Every member that maps a member of the web API has the [IdlMember(name, specUrl)] attribute. The names follow the .NET guidelines: acronyms of 3 letters or more are PascalCased and acronyms of 2 letters stay upper case (innerHTML is InnerHtml, toJSON is ToJson, getElementsByTagNameNS is GetElementsByTagNameNS, HTMLBRElement is HtmlBRElement).

Namespaces

NamespaceContains
Meziantou.Framework.WebPlatformThe parsers: HtmlDocumentParser, XmlDocumentParser and their options
Meziantou.Framework.WebPlatform.DomBrowsing contexts, documents, nodes and elements, events, the document loader, cookies
Meziantou.Framework.WebPlatform.FormattingThe HTML and CSS formatters
Meziantou.Framework.WebPlatform.Css.OmStyle sheets, rules and declarations (the CSSOM)
Meziantou.Framework.WebPlatform.Css.SelectorsThe parsed selectors
Meziantou.Framework.WebPlatform.Css.TypedOmCSS values as objects
Meziantou.Framework.WebPlatform.JavaScriptThe JavaScript script engine
Meziantou.Framework.WebPlatform.ImagingThe rendering backend, the screenshots and the printing to images
Meziantou.Framework.WebPlatform.PdfRenderingThe printing to PDF
Meziantou.Framework.WebPlatform.PlaywrightThe Playwright server

Next steps

  • Getting started: install the package, parse a page, read its styles and take a screenshot.
  • Loading pages: the browsing context, which turns a document into a page.
  • Limitations: what the engine does not do.
  • WPT coverage: the web-platform-tests that pass, and why the others do not.