taoqf / node-html-parser

A very fast HTML parser, generating a simplified DOM, with basic element query support.
MIT License
1.12k stars 112 forks source link

Is there a way to add an ending slash to self-closing tags? #178

Closed JESUSrosetolife closed 2 years ago

JESUSrosetolife commented 2 years ago

Minimal example:

import { parse } from 'node-html-parser';
let parsed = parse('<input />');
// Output: <input>
console.log(parsed.toString())

How do I get the output to be <input /> instead of <input>?

nonara commented 2 years ago

Hi Jared. At the moment, we don't store whether or not it is self-closed on the AST.

You can use the range property to get the full original source text for a node, however.


const { parse } = require('node-html-parser');
const htmlStr = '<input />';
const parsed = parse(htmlStr);

const inputNode = parsed.firstChild;

console.log(htmlStr.slice(...inputNode.range));