Angular HTML Parser
raw JSON →angular-html-parser is a JavaScript HTML parser directly extracted from the Angular framework's internal parsing engine, maintained by the Prettier organization. Currently at version 10.5.0, it receives frequent updates, typically syncing with changes made to the HTML parser within the main Angular repository. Its primary purpose is to provide a robust, Angular-compatible HTML parsing mechanism, often leveraged by tools like Prettier for formatting Angular templates. Key differentiators include added support for CDATA and DocType nodes, enhanced source span tracking for elements and attributes, and modifications to handle specific HTML syntax like bogus comments, going beyond a strict 1:1 replica of Angular's upstream parser. It supports Node.js environments version 14 and above.
Common errors
error TypeError: Cannot read properties of undefined (reading 'type') ↓
node.type with node.kind in your code. error ERR_REQUIRE_ESM: Must use import to load ES Module: .../node_modules/angular-html-parser/index.js ↓
require() statements to import statements (e.g., import { parse } from 'angular-html-parser';) and ensure your project is configured for ES Modules. error SyntaxError: Unexpected token 'export' ↓
nvm install 16 and nvm use 16 to manage Node.js versions. Warnings
breaking In version 10.0.0, the `type` property on parsed `Node` objects was renamed to `kind` for consistency. Code accessing `node.type` will now fail. ↓
gotcha This package requires Node.js version 14 or higher. Running it on older Node.js versions may lead to syntax errors or unexpected behavior due to ESM module usage and other modern JavaScript features. ↓
gotcha The parser frequently syncs with upstream changes from Angular's internal HTML parser. While this keeps it up-to-date, it means parsing behavior might subtly change across minor versions of `angular-html-parser` without an explicit 'breaking change' announcement if the upstream Angular parser was modified. ↓
gotcha Although extracted from Angular, `angular-html-parser` includes its own set of modifications (e.g., support for CDATA, DocType, nameSpan, bogus comments). This means its behavior is not an exact 1:1 replica of Angular's internal parser. ↓
Install
npm install angular-html-parser yarn add angular-html-parser pnpm add angular-html-parser Imports
- parse wrong
const { parse } = require('angular-html-parser');correctimport { parse } from 'angular-html-parser'; - Options wrong
import { Options } from 'angular-html-parser';correctimport type { Options } from 'angular-html-parser'; - ParseTreeResult (and other Angular types)
import type { ParseTreeResult } from '@angular/compiler';
Quickstart
import { parse } from "angular-html-parser";
const htmlString = `
<!DOCTYPE html>
<html>
<head>
<title>Hello world!</title>
</head>
<body>
<div>Hello world!</div>
</body>
</html>
`;
const { rootNodes, errors } = parse(htmlString, {
canSelfClose: false, // Example option
isTagNameCaseSensitive: false // Example option
});
if (errors.length > 0) {
console.error("Parsing errors:", errors);
} else {
console.log("Parsed successfully. Root nodes:", rootNodes.length);
// Example: access the first root node (<html>)
if (rootNodes.length > 0 && rootNodes[0].kind === 'element') {
console.log('First element tag name:', rootNodes[0].name);
}
}