Simple html parser compatible with single tags without /
.
Usage
import { parse, evaluate } from '@rustle/html-parse';
const built = parse(`
<div>
<br>
<a href="xxx"/>
</div>
`)
const ast = evaluate(built, (tag, props, ...children) => {
return { tag, props, children };
})
console.log(ast); // [{ tag: 'div', props: null, children: [...] }]
// Get all `a` tags.
const a = [];
evaluate(built, (tag, props, ...children) => {
if (tag === 'a') {
a.push({ tag, props, children });
}
})
CDN
<!DOCTYPE html>
<html lang='en'>
<body>
<script src='https://unpkg.com/@rustle/html-parse/dist/htmlParse.umd.js'></script>
<script>
const { parse, evaluate } = FetchNpmPackage;
// ...
</script>
</body>
</html>
Caveats
-
The special
tag
is marked with uppercase, because thehtml
tags are all in lowercase, to avoid conflicts, the specialtag
is marked with uppercase.- comment node:
COMMENT
.
- comment node:
-
For the wrong
html
syntax, it is impossible to achieve the same behavior as the browser. To be precise, this is more similar to anxml
parser, so write standardhtml
syntax as much as possible.