Registry / http-networking / prescribe

prescribe

JSON →
library0.5.0jsnpmunverified

Prescribe is a small, forgiving JavaScript HTML parser that originated from John Resig's parsing code and was hardened through use in Krux's postscribe library. It is currently at version 1.1.3, released in 2017. The project appears to be abandoned, with no active development or maintenance since then, indicating a stalled release cadence. Its key differentiator is its tolerance for real-world, often malformed HTML, particularly in contexts like ad serving, rather than aiming for strict W3C compliance. This makes it suitable for environments where robust error handling of imperfect HTML is preferred over strict validation. It boasts broad browser compatibility, including support for older browsers like Internet Explorer 8+.

npm install prescribe
INSTALL
IMPORT
SIG · PRESCRIBE
P
prescribe
http-networkingjavascriptv0.5.0
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.

HtmlParser
import HtmlParser from 'prescribe';
import { HtmlParser } from 'prescribe';
HtmlParser is the default export in ES6 modules.
HtmlParser
const HtmlParser = require('prescribe');
const { HtmlParser } = require('prescribe');
In CommonJS, the module exports HtmlParser as its default.
HtmlParser (AMD)
define(['prescribe'], function(HtmlParser) { /* use HtmlParser */ });
For Asynchronous Module Definition (AMD) loaders like RequireJS.

Demonstrates how to parse an HTML string using Prescribe's SAX-like API, logging the sequence of parsed elements, text, and comments.

import HtmlParser from 'prescribe'; const htmlString = ` <div> <p>Hello <span>World!</span></p> <!-- This is a comment --> <script>console.log('inline script');</script> </div> `; const parsedElements = []; const parser = new HtmlParser(htmlString, { start: function(tag, attrs, unary) { parsedElements.push({ type: 'start', tag, attrs, unary }); // console.log(`Start tag: <${tag}>, Attributes: ${JSON.stringify(attrs)}, Unary: ${unary}`); }, end: function(tag) { parsedElements.push({ type: 'end', tag }); // console.log(`End tag: </${tag}>`); }, chars: function(text) { const trimmedText = text.trim(); if (trimmedText.length > 0) { parsedElements.push({ type: 'text', text: trimmedText }); // console.log(`Text: "${trimmedText}"`); } }, comment: function(text) { parsedElements.push({ type: 'comment', text }); // console.log(`Comment: <!--${text}-->`); } }); parser.parse(); console.log('Parsed HTML Structure:'); parsedElements.forEach(el => console.log(el)); /* Example of expected output for a start tag: { "type": "start", "tag": "p", "attrs": {}, "unary": false } */
Debug
Known issues
breakingVersion 1.1.1 changed the behavior for unclosed script and style tags, causing the parser to return `null` instead of potentially an empty string or other value for their content. This could affect code expecting a non-null value.
fix
Ensure your code explicitly handles `null` returns when parsing content within unclosed `<script>` or `<style>` tags.
affects: >=1.1.1
gotchaThis project is currently abandoned, with its last commit in June 2017. It is not actively maintained, which means it will not receive updates for new web standards, security vulnerabilities, or modern browser compatibility issues.
fix
Consider using alternative, actively maintained HTML parsing libraries for new projects or if strict security and modern standard compliance are critical.
affects: >=1.1.3
gotchaPrescribe is designed as a 'forgiving' HTML parser, prioritizing robust handling of malformed or non-compliant HTML, commonly found in ad serving or user-generated content, over strict W3C compliance. This may lead to unexpected parsing results if strict adherence to HTML specifications is required.
fix
If strict HTML compliance is necessary, evaluate if Prescribe's forgiving nature meets your requirements, or consider a different parser built for strict validation.
affects: All versions
gotchaThe library was developed with compatibility for older browsers (e.g., IE8+) in mind. While this offers broad support, its internal mechanisms or output might not be optimized for, or fully compliant with, modern JavaScript environments or current HTML/DOM standards.
fix
Thoroughly test its behavior in your specific target environments, especially modern Node.js or evergreen browsers, and be prepared for potential polyfill requirements or compatibility layers.
affects: All versions
Errors
Common errors & fixes
TypeError: HtmlParser is not a constructor
This typically occurs in CommonJS environments if you are trying to destructure the `require` call, or in an ESM environment if you're attempting to use a named import where only a default export exists.
fix
For CommonJS, use `const HtmlParser = require('prescribe');`. For ESM, use `import HtmlParser from 'prescribe';`.
parser.parse is not a function
This error means that the `HtmlParser` object was not correctly instantiated, or the `parse` method is missing, usually due to an incorrect import or usage pattern.
fix
Ensure you are calling `new HtmlParser(htmlString, options)` to create an instance before calling `.parse()` on it.
Unexpected HTML structure or missing elements in output
Prescribe's 'forgiving' parsing philosophy might produce a different document structure than a strict parser, especially for malformed HTML or certain edge cases.
fix
Review the input HTML and Prescribe's SAX-like output carefully. If the issue is due to intentional 'forgiveness', adjust your expectations or consider a different parser for strict validation.
Upgrade
Version history
0.5.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
2 hits · last 30 days
node
2
Resources
prescribe — npm install prescribe · libregistry