{"id":20563815,"url":"https://github.com/worker-tools/parsed-html-rewriter","last_synced_at":"2025-04-14T14:52:23.169Z","repository":{"id":41410078,"uuid":"349633489","full_name":"worker-tools/parsed-html-rewriter","owner":"worker-tools","description":"A DOM-based implementation of Cloudflare Worker's HTMLRewriter.","archived":false,"fork":false,"pushed_at":"2022-05-27T11:02:41.000Z","size":138,"stargazers_count":45,"open_issues_count":0,"forks_count":3,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-28T03:41:33.981Z","etag":null,"topics":["cloudflare-workers","dom","html","html-rewriter","polyfill"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/worker-tools.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-03-20T05:11:57.000Z","updated_at":"2025-02-17T01:59:09.000Z","dependencies_parsed_at":"2022-09-07T06:12:40.609Z","dependency_job_id":null,"html_url":"https://github.com/worker-tools/parsed-html-rewriter","commit_stats":null,"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/worker-tools%2Fparsed-html-rewriter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/worker-tools%2Fparsed-html-rewriter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/worker-tools%2Fparsed-html-rewriter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/worker-tools%2Fparsed-html-rewriter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/worker-tools","download_url":"https://codeload.github.com/worker-tools/parsed-html-rewriter/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248900815,"owners_count":21180289,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cloudflare-workers","dom","html","html-rewriter","polyfill"],"created_at":"2024-11-16T04:21:10.876Z","updated_at":"2025-04-14T14:52:23.136Z","avatar_url":"https://github.com/worker-tools.png","language":"TypeScript","readme":"# Parsed HTML Rewriter\nA DOM-based implementation of [Cloudflare Worker's `HTMLRewriter`](https://developers.cloudflare.com/workers/runtime-apis/html-rewriter).\n\n***\n\n___UPDATE: While this module works just fine, I've made [a new verison](https://github.com/worker-tools/html-rewriter) that is WASM/streaming based for much better performance.___\n\n***\n\nUnlike the original, this implementation parses the entire DOM (provided by [`linkedom`](https://github.com/WebReflection/linkedom)),\nand runs selectors against this representation. As a result, it is slower, more memory intensive, and can't process streaming data.\n\nNote that this approach was chosen to quickly implement the functionality of `HTMLRewriter`, as there is currently no JS implementation available.\nA better implementation would replicate the streaming approach of [`lol-html`](https://github.com/cloudflare/lol-html), or even use a WebAssembly version of it. _Update: [Now available here](https://github.com/worker-tools/html-rewriter)_.\n\nHowever, 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`. \nIt should be good enough for testing and offline Workers development.\n\n## Usage\nThis module can be used in two ways. \n\nAs a standalone module: \n\n```ts\nimport { ParsedHTMLRewriter } from '@worker-tools/parsed-html-rewriter'\n\nawait new ParsedHTMLRewriter()\n  .transform(new Response('\u003cbody\u003e\u003c/body\u003e'))\n  .text();\n```\n\nOr as a polyfill:\n\n```ts\nimport '@worker-tools/parsed-html-rewriter/polyfill'\n\nawait new HTMLRewriter() // Will use the native version when running in a Worker\n  .transform(new Response('\u003cbody\u003e\u003c/body\u003e'))\n  .text();\n```\n\n### innerHTML\nUnlike the current (March 2021) version on CF Workers, this implementation already supports the [proposed `innerHTML` handler](https://github.com/cloudflare/lol-html/issues/40#issuecomment-567126687). \nNote that this feature is unstable and will likely change as the real version materializes.\n\n```ts\nawait new HTMLRewriter()\n  .on('body', {\n    innerHTML(html) {\n      console.log(html) // =\u003e '\u003cdiv id=\"foo\"\u003ebar\u003c/div\u003e'\n    },\n  })\n  .transform(new Response('\u003cbody\u003e\u003cdiv id=\"foo\"\u003ebar\u003c/div\u003e\u003c/body\u003e'))\n  .text();\n```\n\n## Caveats\n- 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.\n- Texts never arrive in chunks. There is always just one chunk, followed by an empty one with `lastInTextNode` set to `true`.\n\n--------\n\n\u003cp align=\"center\"\u003e\u003ca href=\"https://workers.tools\"\u003e\u003cimg src=\"https://workers.tools/assets/img/logo.svg\" width=\"100\" height=\"100\" /\u003e\u003c/a\u003e\n\u003cp align=\"center\"\u003eThis module is part of the Worker Tools collection\u003cbr/\u003e⁕\n\n[Worker Tools](https://workers.tools) are a collection of TypeScript libraries for writing web servers in [Worker Runtimes](https://workers.js.org) such as Cloudflare Workers, Deno Deploy and Service Workers in the browser. \n\nIf you liked this module, you might also like:\n\n- 🧭 [__Worker Router__][router] --- Complete routing solution that works across CF Workers, Deno and Service Workers\n- 🔋 [__Worker Middleware__][middleware] --- A suite of standalone HTTP server-side middleware with TypeScript support\n- 📄 [__Worker HTML__][html] --- HTML templating and streaming response library\n- 📦 [__Storage Area__][kv-storage] --- Key-value store abstraction across [Cloudflare KV][cloudflare-kv-storage], [Deno][deno-kv-storage] and browsers.\n- 🆗 [__Response Creators__][response-creators] --- Factory functions for responses with pre-filled status and status text\n- 🎏 [__Stream Response__][stream-response] --- Use async generators to build streaming responses for SSE, etc...\n- 🥏 [__JSON Fetch__][json-fetch] --- Drop-in replacements for Fetch API classes with first class support for JSON.\n- 🦑 [__JSON Stream__][json-stream] --- Streaming JSON parser/stingifier with first class support for web streams.\n\nWorker Tools also includes a number of polyfills that help bridge the gap between Worker Runtimes:\n- ✏️ [__HTML Rewriter__][html-rewriter] --- Cloudflare's HTML Rewriter for use in Deno, browsers, etc...\n- 📍 [__Location Polyfill__][location-polyfill] --- A `Location` polyfill for Cloudflare Workers.\n- 🦕 [__Deno Fetch Event Adapter__][deno-fetch-event-adapter] --- Dispatches global `fetch` events using Deno’s native HTTP server.\n\n[router]: https://workers.tools/router\n[middleware]: https://workers.tools/middleware\n[html]: https://workers.tools/html\n[kv-storage]: https://workers.tools/kv-storage\n[cloudflare-kv-storage]: https://workers.tools/cloudflare-kv-storage\n[deno-kv-storage]: https://workers.tools/deno-kv-storage\n[kv-storage-polyfill]: https://workers.tools/kv-storage-polyfill\n[response-creators]: https://workers.tools/response-creators\n[stream-response]: https://workers.tools/stream-response\n[json-fetch]: https://workers.tools/json-fetch\n[json-stream]: https://workers.tools/json-stream\n[request-cookie-store]: https://workers.tools/request-cookie-store\n[extendable-promise]: https://workers.tools/extendable-promise\n[html-rewriter]: https://workers.tools/html-rewriter\n[location-polyfill]: https://workers.tools/location-polyfill\n[deno-fetch-event-adapter]: https://workers.tools/deno-fetch-event-adapter\n\nFore more visit [workers.tools](https://workers.tools).\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fworker-tools%2Fparsed-html-rewriter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fworker-tools%2Fparsed-html-rewriter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fworker-tools%2Fparsed-html-rewriter/lists"}