Registry / http-networking / htmlparser2-svelte

htmlparser2-svelte

JSON →
library4.1.1jsnpmunverified

htmlparser2-svelte is a robust and forgiving HTML, Svelte, XML, and RSS parser for JavaScript environments, currently at version 4.1.1. It enhances the core `htmlparser2` library by adding native support for Svelte-specific syntax, particularly JavaScript expressions within tag attributes (e.g., `on:click={() => ...}`). The library offers both a synchronous `Parser` interface and an asynchronous `WritableStream` for processing streaming input, making it suitable for a wide range of parsing tasks. Its design emphasizes speed and fault tolerance, making it ideal for parsing real-world, potentially malformed HTML, and especially beneficial for tooling that needs to understand Svelte component structures. Releases appear to be driven by feature additions and maintenance, with 4.1.0 introducing support for JS expressions in attributes.

npm install htmlparser2-svelte
INSTALL
IMPORT
SIG · HTMLPARSER2-SVELTE
H
htmlparser2-svelte
http-networkingjavascriptv4.1.1
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Parser
import { Parser } from 'htmlparser2-svelte';
const htmlparser2 = require('htmlparser2-svelte'); const parser = new htmlparser2.Parser(...);
For modern ESM projects, import `Parser` as a named export. The CommonJS pattern `htmlparser2.Parser` is shown in older examples but can lead to issues in ESM environments.
WritableStream
import { WritableStream } from 'htmlparser2-svelte';
const htmlparser2 = require('htmlparser2-svelte'); const stream = new htmlparser2.WritableStream(...);
Use `WritableStream` for processing streaming HTML content, similar to Node.js streams. Ensure it's imported as a named export.
parseFeed
import { parseFeed } from 'htmlparser2-svelte';
const htmlparser2 = require('htmlparser2-svelte'); const feed = htmlparser2.parseFeed(content);
Directly import `parseFeed` for RSS/Atom/RDF feed parsing. While convenient, consider dedicated libraries like `node-feedparser` for more robust feed handling.
ParserOptions
import type { ParserOptions } from 'htmlparser2-svelte';
Import types explicitly when working with TypeScript for better type safety and autocompletion for parser options and handlers.

Demonstrates parsing a Svelte component template, including JavaScript expressions in attributes, using the callback API to log parsing events to the console.

import { Parser } from 'htmlparser2-svelte'; const svelteCode = ` <script lang="ts"> let count = 0; function handleClick() { count += 1; console.log('Button clicked:', count); } </script> <div class="app"> <h1>Svelte Parser Example</h1> <button type="button" on:click={() => handleClick()} title={"Click count is " + count}> Click me {count} times! </button> <p>This paragraph has some <span>inline</span> text.</p> </div> `; const parser = new Parser( { onopentag(name, attribs) { console.log(`Opened tag: <${name}> with attributes:`, attribs); }, ontext(text) { const trimmedText = text.trim(); if (trimmedText.length > 0) { console.log(`Text content: "${trimmedText}"`); } }, onclosetag(tagname) { console.log(`Closed tag: </${tagname}>`); }, onprocessinginstruction(name, data) { console.log(`Processing instruction: ${name} -> ${data}`); } }, { decodeEntities: true, curlyBracesInAttributes: true, // Crucial for Svelte support xmlMode: false // Parse as HTML } ); parser.write(svelteCode); parser.end();
Debug
Known issues
gotchaTo correctly parse Svelte-specific syntax, such as JavaScript expressions within tag attributes (e.g., `on:click={() => ...}`), you *must* enable the `curlyBracesInAttributes: true` option in the parser configuration. Without this, Svelte expressions will be treated as regular attribute values or malformed HTML, leading to incorrect parsing.
fix
Always pass `{ curlyBracesInAttributes: true }` as the second argument to `new Parser()` or `new WritableStream()` when parsing Svelte templates.
affects: >=4.1.0
deprecatedThe `DomHandler` utility, which provides DOM tree construction, was originally bundled but has since been moved to its own dedicated `domhandler` package. While `htmlparser2-svelte` might still bundle an older version for compatibility, relying on the separate `domhandler` package directly is recommended for the latest features and maintenance.
fix
Install `domhandler` and `domutils` separately via `npm install domhandler domutils` and import them directly to build and manipulate the DOM tree.
affects: >=3.x
gotchaWhile `htmlparser2-svelte` offers a `parseFeed` utility, the documentation notes that more robust and actively maintained alternatives like `danmactough/node-feedparser` exist. For critical applications requiring comprehensive RSS/Atom/RDF feed parsing, consider using specialized libraries for better compliance and feature sets.
fix
For advanced feed parsing, evaluate dedicated libraries like `node-feedparser` instead of the built-in `parseFeed` method.
affects: *
gotchaMany examples for `htmlparser2-svelte` (and its base `htmlparser2`) use CommonJS `require()` syntax (e.g., `const htmlparser2 = require('htmlparser2-svelte');`). In modern JavaScript environments or when using bundlers that support ESM, prefer named imports like `import { Parser } from 'htmlparser2-svelte';` to leverage tree-shaking and standard module resolution.
fix
Update `require('pkg')` statements to `import { Symbol } from 'pkg'` for better compatibility with ESM.
affects: *
Errors
Common errors & fixes
TypeError: htmlparser2.Parser is not a constructor
Attempting to instantiate `Parser` using a CommonJS `require` that doesn't correctly export named members, or in an ESM context where `htmlparser2` is imported as a default object.
fix
For CommonJS, use `const { Parser } = require('htmlparser2-svelte');`. For ESM, use `import { Parser } from 'htmlparser2-svelte';`.
Error: Unknown option: curlyBracesInAttributes
This error occurs when the `curlyBracesInAttributes` option is passed to the original `htmlparser2` package, which does not support Svelte-specific syntax, instead of `htmlparser2-svelte`.
fix
Ensure `htmlparser2-svelte` is correctly installed and imported, not `htmlparser2`, when using Svelte-specific parsing options.
Svelte component attributes with `{@expression}` or `{expression}` are not correctly parsed.
The `curlyBracesInAttributes` option, essential for Svelte syntax, was not enabled when initializing the parser.
fix
Include `{ curlyBracesInAttributes: true }` in the options object when creating a `Parser` or `WritableStream` instance for Svelte content.
TypeError: Cannot read properties of undefined (reading 'pipe')
This typically happens when trying to use Node.js `Stream.pipe()` functionality on a `Parser` instance, which is for direct string input, rather than a `WritableStream` instance, or if `WritableStream` was not imported correctly.
fix
Ensure you are importing and instantiating `WritableStream` for streaming operations (`import { WritableStream } from 'htmlparser2-svelte'; new WritableStream(...)`), as `Parser` does not directly support `pipe()`.
Upgrade
Version history
4.1.1latest on npm
Audit
Dependencies
domhandleroptionalUsed to construct a DOM tree from the parsed HTML events. While historically bundled, it's recommended to install and use it separately for the latest features and maintenance.
domutilsrequiredProvides utility functions for manipulating the DOM tree generated by `domhandler`. Recommended alongside `domhandler`.
Agent activity
4 hits · last 30 days
node
4
Resources