All Projects → worker-tools → parsed-html-rewriter

worker-tools / parsed-html-rewriter

Licence: MIT license
A DOM-based implementation of Cloudflare Worker's HTMLRewriter.

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to parsed-html-rewriter

Dom4
Modern DOM functionalities for every browser
Stars: ✭ 903 (+2555.88%)
Mutual labels:  polyfill, dom
Webappsec Trusted Types
A browser API to prevent DOM-Based Cross Site Scripting in modern web applications.
Stars: ✭ 424 (+1147.06%)
Mutual labels:  polyfill, dom
deno-fetch-event-adapter
Dispatches global fetch events using Deno's native http server.
Stars: ✭ 18 (-47.06%)
Mutual labels:  polyfill, cloudflare-workers
smart-custom-element
Smart a lightweight web component library that provides capabilities for web components, such as data binding, using es6 native class inheritance. This library is focused for providing the developer the ability to write robust and native web components without the need of dependencies and an overhead of a framework.
Stars: ✭ 17 (-50%)
Mutual labels:  polyfill, dom
emerj
Emerj is a tiny JavaScript library to render live HTML/DOM updates efficiently and non-destructively, by merging an updated DOM with the live DOM, and only changing those elements that differ.
Stars: ✭ 56 (+64.71%)
Mutual labels:  dom
tsdom
Fast, lightweight TypeScript DOM manipulation utility
Stars: ✭ 16 (-52.94%)
Mutual labels:  dom
xpath2.js
xpath.js - Open source XPath 2.0 implementation in JavaScript (DOM agnostic)
Stars: ✭ 74 (+117.65%)
Mutual labels:  dom
Mei.js
a minimal, simple and helpful library for you
Stars: ✭ 15 (-55.88%)
Mutual labels:  dom
AdvancedHTMLParser
Fast Indexed python HTML parser which builds a DOM node tree, providing common getElementsBy* functions for scraping, testing, modification, and formatting. Also XPath.
Stars: ✭ 90 (+164.71%)
Mutual labels:  dom
IPFS PHOTO SHARE
💰用甚嚒服务器,ServerLess搭建一个图片分享站点!| 基于CloudFlareWorker无服务器函数和IPFS去中心化存储的图片分享网站
Stars: ✭ 76 (+123.53%)
Mutual labels:  cloudflare-workers
workers-unsplash-api
Serverless API for requesting images from Unsplash's API, designed for use with a React frontend
Stars: ✭ 20 (-41.18%)
Mutual labels:  cloudflare-workers
draggable-polyfill
🌈a beautify polyfill for native drag!
Stars: ✭ 49 (+44.12%)
Mutual labels:  polyfill
bassdrum
reactive, type safe components with preact and rxjs.
Stars: ✭ 44 (+29.41%)
Mutual labels:  dom
remix-guide
A platform for sharing everything about Remix
Stars: ✭ 197 (+479.41%)
Mutual labels:  cloudflare-workers
s2
A function for reactive web UI.
Stars: ✭ 43 (+26.47%)
Mutual labels:  dom
PHPUnit-Polyfills
Set of polyfills for changed PHPUnit functionality to allow for creating PHPUnit cross-version compatible tests
Stars: ✭ 147 (+332.35%)
Mutual labels:  polyfill
crab
JavaScript library for building user interfaces with Custom Elements, Shadow DOM and React like API
Stars: ✭ 22 (-35.29%)
Mutual labels:  dom
workers-react-pwa-example
No description or website provided.
Stars: ✭ 80 (+135.29%)
Mutual labels:  cloudflare-workers
reader
get a reader mode sharable url for any url - built with cloudflare workers https://reader.tuananh.net
Stars: ✭ 24 (-29.41%)
Mutual labels:  cloudflare-workers
picoCSS
picoCSS - really small JavaScript Framework
Stars: ✭ 62 (+82.35%)
Mutual labels:  dom

Parsed HTML Rewriter

A DOM-based implementation of Cloudflare Worker's HTMLRewriter.


UPDATE: While this module works just fine, I've made a new verison that is WASM/streaming based for much better performance.


Unlike the original, this implementation parses the entire DOM (provided by linkedom), and runs selectors against this representation. As a result, it is slower, more memory intensive, and can't process streaming data.

Note that this approach was chosen to quickly implement the functionality of HTMLRewriter, as there is currently no JS implementation available. A better implementation would replicate the streaming approach of lol-html, or even use a WebAssembly version of it. Update: Now available here.

However, this implementation should run in most JS contexts (including Web Workers, Service Workers and Deno) without modification and handle many, if not most, use cases of HTMLRewriter. It should be good enough for testing and offline Workers development.

Usage

This module can be used in two ways.

As a standalone module:

import { ParsedHTMLRewriter } from '@worker-tools/parsed-html-rewriter'

await new ParsedHTMLRewriter()
  .transform(new Response('<body></body>'))
  .text();

Or as a polyfill:

import '@worker-tools/parsed-html-rewriter/polyfill'

await new HTMLRewriter() // Will use the native version when running in a Worker
  .transform(new Response('<body></body>'))
  .text();

innerHTML

Unlike the current (March 2021) version on CF Workers, this implementation already supports the proposed innerHTML handler. Note that this feature is unstable and will likely change as the real version materializes.

await new HTMLRewriter()
  .on('body', {
    innerHTML(html) {
      console.log(html) // => '<div id="foo">bar</div>'
    },
  })
  .transform(new Response('<body><div id="foo">bar</div></body>'))
  .text();

Caveats

  • Because this version isn't based on streaming data, the order in which handlers are called can differ. Some measure have been taken to simulate the order, but differences may occur.
  • Texts never arrive in chunks. There is always just one chunk, followed by an empty one with lastInTextNode set to true.

This module is part of the Worker Tools collection

Worker Tools are a collection of TypeScript libraries for writing web servers in Worker Runtimes such as Cloudflare Workers, Deno Deploy and Service Workers in the browser.

If you liked this module, you might also like:

  • 🧭 Worker Router --- Complete routing solution that works across CF Workers, Deno and Service Workers
  • 🔋 Worker Middleware --- A suite of standalone HTTP server-side middleware with TypeScript support
  • 📄 Worker HTML --- HTML templating and streaming response library
  • 📦 Storage Area --- Key-value store abstraction across Cloudflare KV, Deno and browsers.
  • 🆗 Response Creators --- Factory functions for responses with pre-filled status and status text
  • 🎏 Stream Response --- Use async generators to build streaming responses for SSE, etc...
  • 🥏 JSON Fetch --- Drop-in replacements for Fetch API classes with first class support for JSON.
  • 🦑 JSON Stream --- Streaming JSON parser/stingifier with first class support for web streams.

Worker Tools also includes a number of polyfills that help bridge the gap between Worker Runtimes:

Fore more visit workers.tools.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].